aboutsummaryrefslogtreecommitdiffstats
path: root/R/calibrate_tranches_BC.R
blob: 69298f13672c4d65e3c5a92618440eaefb286aa4 (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
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"))
source(file.path(root.dir, "code", "R", "yieldcurve.R"))
source(file.path(root.dir, "code", "R", "optimization.R"))
source(file.path(root.dir, "code", "R", "calibration.R"))
source(file.path(root.dir, "code", "R", "mlpdb.R"))
library(lossdistrib)

n.int <- 250
attach(GHquad(n.int))
Ngrid <- 201

alldates <- seq(as.Date("2014-01-01"), as.Date("2014-01-05"), by="1 day")

aux <- function(rho, index, N, i){
    temp <- BClossdistC(index$defaultprob, index$issuerweights, index$recov, rho, Z, w, N)
    return(abs(tranche.pv(temp$L, temp$R, index$cs, 0, index$K[i+1]) + index$quotes[i]))
}

bus.dates <- as.Date(names(which(isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates))))
for(index.name in  c("hy19", "hy21"){
    rhomat <- c()
    deltasmat <- c()
    gammasmat <- c()
    for(i in seq_along(bus.dates)){
        tradedate <- bus.dates[i]
        exportYC(tradedate)
        index <- load.index(index.name, tradedate)
        ## calibrate HY21 the single names curves
        index <- set.singlenamesdata(index, tradedate)
        index <- set.tranchedata(index, tradedate)

        ## calibrate the tranches using base correlation
        rhovec <- c()
        for(j in 1:(length(index$K)-1)){
            rho <- optimize(aux, interval=c(0,1), index=index, N=Ngrid, i=j)$minimum
            rhovec <- c(rhovec, rho)
        }
        rhovec <- c(0, rhovec)
        index$rho <- rhovec
        temp <- BCtranche.delta(index, Z, w, Ngrid, tradedate)
        deltasmat <- rbind(deltasmat, temp$deltas)
        gammasmat <- rbind(gammasmat, temp$gammas)
        cl <- c()
        for(j in 1:(length(index$K)-1)){
            cl <- c(cl, BCtranche.pv(index, index$K[j], index$K[j+1], rhovec[j], rhovec[j+1], Z, w, Ngrid)$cl)
        }
        duration <- (cl - cdsAccrued(tradedate, index$tranche.running))/index$tranche.running
        rhomat <- rbind(rhomat, rhovec)
        cat(i, "\n")
    }
}
    write.csv(deltasmat, file="delta.csv")
write.csv(rhomat, file="rho.csv")