aboutsummaryrefslogtreecommitdiffstats
path: root/R/calibration.R
blob: 73479fbdb2eab879fe715c0468087c35c6bac2f4 (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
if(.Platform$OS.type == "unix"){
  root.dir <- "/home/share/CorpCDOs"
}else{
  root.dir <- "//WDSENTINEL/share/CorpCDOs"
}
source(file.path(root.dir, "code", "R", "cds_utils.R"))
source(file.path(root.dir, "code", "R", "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), 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)){
        quote <- list(ticker = quotes$ticker[i],
                       running = quotes$spread_curve[i, tenor] * 1e-4,
                       upfront = quotes$upfront_curve[i, tenor] * 0.01,
                       recovery = as.double(quotes$recovery[i,tenor][1]))
        index$portfolio <- c(index$portfolio, buildSC(quote, cds.cs$cs, cds.cs$cdsdates))
    }
    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, tradedate)
    if(!is.na(index$indexrefprice[1])){
        index$indexrefprice <- temp$indexrefprice[1]/100
    }else{
        index$indexrefprice <- 1
    }
    index$indexrefspread <- temp$indexrefspread*1e-4
    index$cs <- couponSchedule(IMMDate(tradedate), index$maturity,"Q", "FIXED", index$indexrefspread,
                               0, tradedate, IMMDate(tradedate, "prev"))
    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,]]]@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$upfront
    index$tranche.running <- temp$running*1e-4

    ## convert the quotes
    ## - we convert to protection terms x->1-x/100
    ## - we remove accrued x->x-acc
    ## - we weight it by the size of the tranche
    ## - we take the cumsum to convert to 0-5, 0-10, 0-15 quotes, etc...
    ## calibrate the tranches using base correlation
    index$quotes <- cumsum(diff(index$K) *
                           (1-index$tranche.upf/100-cdsAccrued(tradedate, index$tranche.running)))
    return(index)
}