aboutsummaryrefslogtreecommitdiffstats
path: root/R/calibrate_tranches_BC.R
blob: cd0d85fc843c0d7e3e55abb1e7126f969bf7c866 (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
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"))
load.index("hy21", "index_definitions_bt.yml")
library(lossdistrib)

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

alldates <- seq(as.Date("2014-01-01"), as.Date("2014-05-05"), by="1 day")
rhomat <- c()
deltasmat <- c()
bus.dates <- as.Date(names(which(isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates))))
for(i in seq_along(bus.dates)){
    tradedate <- bus.dates[i]
    exportYC(tradedate)
    ## calibrate HY21
    ## calibrate the single names curves
    set.singlenamesdata(hy21, tradedate)
    set.tranchedata(hy21, tradedate)

    ## calibrate the tranches using base correlation
    f <- function(rho, index, N, i){
        temp <- with(index,
                     BClossdistC(defaultprob, issuerweights, recov, rho, Z, w, N))
        return(abs(tranche.pv(temp$L, temp$R, index$cs, 0, index$K[i+1]) + index$quotes[i]))
    }
    rhovec <- c()
    for(i in 1:(length(hy21$K)-1)){
        q <- quotes[i-1]
        rho <- optimize(f, interval=c(0,1), index=hy21, N=Ngrid, i=i)$minimum
        rhovec <- c(rhovec, rho)
    }
    rhovec <- c(0, rhovec)

    ##compute deltas by blipping the curves
    ## portfolioplus <- tweakportfolio(hy21portfolio.tweaked, 1e-4)
    ## defaultprobplus <- 1 - SPmatrix(portfolioplus, length(cs$dates))
    ## portfoliominus <- tweakportfolio(hy21portfolio.tweaked, -1e-4)
    ## defaultprobminus <- 1 - SPmatrix(portfoliominus, length(cs$dates))
    ## test <- matrix(0, 6, 2)
    ## for(i in 2:7){
    ##     tempminus <- BClossdistC(defaultprobminus, issuerweights, recov, rhovec[i], Z, w, Ngrid)
    ##     tempplus <- BClossdistC(defaultprobplus, issuerweights, recov, rhovec[i], Z, w, Ngrid)
    ##     test[i-1,1] <- tranche.pv(tempminus$L, tempminus$R, cs, 0, Kmodified[i])
    ##     test[i-1,2] <- tranche.pv(tempplus$L, tempplus$R, cs, 0, Kmodified[i])
    ## }
    ## dPVtranche <- diff(c(0, test[,1]))/dK - diff(c(0, test[,2]))/dK
    ## dPVindex <- indexpv(portfoliominus, hy21, tradedate=tradedate, clean=FALSE)$bp-
    ##     indexpv(portfolioplus, hy21, tradedate=tradedate, clean=FALSE)$bp
    ## deltas <- dPVtranche/dPVindex

    ##use BCtranche.delta function
    deltas <- rep(0, length(hy21$K)-1)
    for(i in seq_along(hy21$K[-1])){
        deltas[i] <- BCtranche.delta(hy21, hy21$K[i], hy21$K[i+1], rhovec[i], rhovec[i+1], Z, w, Ngrid, tradedate)
    }
    deltasmat <- rbind(deltasmat, deltas)
    rhomat <- rbind(rhomat, rhovec)
}