aboutsummaryrefslogtreecommitdiffstats
path: root/R/calibration.R
blob: ea3c6d14fb49fbdc6df33ca25f474b5d7d68cd1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
source("cds_functions_generic.R")

buildSC <- function(quote, cs, cdsdates){
    SC <- new("creditcurve",
              recovery=quote$recovery,
              startdate=tradedate,
              issuer=quote$ticker)
    quotes <- data.frame(maturity=cdsdates, upfront = quote$upfront,
                         running=quote$running)
    SC@curve <- cdshazardrate(quotes, SC@recovery, tradedate, cs)
    return( SC )
}

get.cdsSchedule <- function(tradedate, indexmaturity){
    cdsdates <- as.Date(character(0))
    for(tenor in paste0(c(1:5, 7,10), "y")){
        newdate <- cdsMaturity(tenor, date=tradedate)
        cdsdates <- c(cdsdates, newdate)
        if(newdate>=indexmaturity){
            break
        }
    }
    return( list(cs=couponSchedule(IMMDate(tradedate, noadj=TRUE), cdsdates[length(cdsdates)], "Q", "FIXED",
                     1, tradedate, IMMDate(tradedate, "prev")), cdsdates=cdsdates) )
}

set.singlenamesdata <- function(index, tradedate){
    cds.cs <- get.cdsSchedule(tradedate, index$maturity)
    quotes <- get.indexquotes(index$name, tradedate)
    tenor <- names(cds.cs$cdsdates)
    index$portfolio <- list()
    for(i in seq_along(quotes$tickers)){
        sane.quotes <- which(yearFrac(tradedate+2,cds.cs$cdsdates)*quotes$spread_curve[i, tenor]*1e-4+
            quotes$upfront_curve[i, tenor] * 0.01>0)
        quote <- list(ticker = quotes$ticker[i],
                      running = quotes$spread_curve[i, tenor[sane.quotes]] * 1e-4,
                      upfront = quotes$upfront_curve[i, tenor[sane.quotes]] * 0.01,
                      recovery = as.double(quotes$recovery[i,tenor[sane.quotes]][1]))
        index$portfolio <- c(index$portfolio, buildSC(quote, cds.cs$cs, cds.cs$cdsdates[sane.quotes]))
    }
    index$issuerweights <- rep(1/length(index$portfolio), length(index$portfolio))
    index$recov <- sapply(index$portfolio, attr, "recovery")
    return( index )
}

set.tranchedata <- function(index, tradedate){
    temp <- get.tranchequotes(index$name, index$tenor, tradedate)
    index$quotes <- data.frame(spread=temp$indexrefspread[1]*1e-4, maturity=index$maturity)
    if(index$name=="ig19" || index$name=="ig21"){
        index$quotes$spread <- 0.01
    }
    index$cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE), index$maturity,"Q", "FIXED", 1,
                               0, tradedate, IMMDate(tradedate, "prev"))
    if(!is.na(temp$indexrefprice[1])&&temp$indexrefprice[1]!=0){
        index$quotes$price <- temp$indexrefprice[1]/100
    }else{
        ##rewrite as a snac function
        sc <- new("flatcurve", h=temp$indexrefspread[1]*1e-4/(1-index$recovery))
        startdate <- tradedate + 1
        cds.pv <- couponleg(index$cs, sc, startdate)*index$quotes$spread -
            defaultleg(index$cs, sc, index$recovery, startdate)
        index$quotes$price <- 1 + cds.pv - cdsAccrued(tradedate, index$quotes$spread[1])
    }
    index$portfolio <- tweakcurves(index, tradedate)$portfolio
    index$defaultprob <- 1 - SPmatrix(index$portfolio, length(index$cs$dates))
    negprob <- which(index$defaultprob<0, arr.ind=T)
    if(nrow(negprob)>0){
        stop(paste(index$portfolio[[negprob[1,1]]]@issuer, "has negative probability, check single names data"))
    }
    K <- c(0, temp$detach/100)
    index$K <- adjust.attachments(K, index$loss, index$factor)
    index$tranche.upf <- temp$trancheupfront
    index$tranche.running <- temp$trancherunning*1e-4
    ## compute dirty protection price
    if(length(grep("hy", index$name, ignore.case=TRUE))>0){
        index$tranche.quotes <- 1-index$tranche.upf/100-cdsAccrued(tradedate, index$tranche.running)
    }else{
        index$tranche.quotes <- index$tranche.upf/100-cdsAccrued(tradedate, index$tranche.running)
    }
    return( index )
}