aboutsummaryrefslogtreecommitdiffstats
path: root/R/calibrate_tranches_BC.R
blob: b8e13f7daaa3777029a4292638fae14dc9568dad (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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"))
load.index("hy21")
library(lossdistrib)

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

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
    singlenames.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
                                           paste0("hy21_singlenames_", tradedate, ".csv")))
    nondefaulted <- singlenames.data[!singlenames.data$ticker %in% hy21$defaulted,]
    bps <- 1e-4

    cdsdates <- as.Date(character(0))
    for(tenor in paste0(1:5, "y")){
        cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate))
    }

    hy21portfolio <- c()
    cs <- couponSchedule(IMMDate(tradedate), cdsdates[length(cdsdates)], "Q", "FIXED",
                         1, tradedate, IMMDate(tradedate, "prev"))
    for(i in 1:nrow(nondefaulted)){
        SC <- new("creditcurve",
                  recovery=nondefaulted$recovery[i]/100,
                  startdate=tradedate,
                  issuer=as.character(nondefaulted$ticker[i]))
        quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(nondefaulted[i,4:8])*0.01,
                             running=rep(nondefaulted$running[i]*bps, 5))
        SC@curve <- cdshazardrate(quotes, nondefaulted$recovery[i]/100, tradedate, cs)
        hy21portfolio <- c(hy21portfolio, SC)
    }
    n.credit <- length(hy21portfolio)
    issuerweights <- rep(1/n.credit, n.credit)
    tranche.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
                                       paste0("hy21_tranches_", tradedate, ".csv")), header=TRUE)
    hy21$indexref <- tranche.data$bidRefPrice[1]/100

    temp <- tweakcurves(hy21portfolio, hy21, tradedate)
    hy21portfolio.tweaked <- temp$portfolio
    cs <- couponSchedule(IMMDate(tradedate), hy21$maturity,"Q", "FIXED", 0.05, 0, tradedate,
                         IMMDate(tradedate, "prev"))
    SurvProb <- SPmatrix(hy21portfolio.tweaked, length(cs$dates))
    ## load common parameters
    K <- c(0, 0.15, 0.25, 0.35, 1)
    Kmodified <- adjust.attachments(K, hy21$loss, hy21$factor)
    tranche.upf <- tranche.data$Mid
    tranche.running <- tranche.data$Coupon

    Ngrid <- 2 * nrow(nondefaulted) + 1
    recov <- sapply(hy21portfolio.tweaked, attr, "recovery")

    ##calibrate tranches using base correlation
    defaultprob <- 1 - SurvProb

    dK <- diff(Kmodified)
    acc <- cdsAccrued(tradedate, tranche.running)

    ##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...
    quotes <- cumsum(dK * (1-tranche.upf/100-acc))

    ## calibrate the tranches using base correlation
    rhovec <- c()
    f <- function(rho, ...){
        temp <- BClossdistC(defaultprob, issuerweights, recov, rho, Z, w, Ngrid)
        return(abs(tranche.pv(temp$L, temp$R, cs, 0, Kmodified[i]) + q))
    }

    rhovec <- c()
    for(i in 2:length(Kmodified)){
        q <- quotes[i-1]
        rho <- optimize(f, interval=c(0,1),
                        defaultprob, issuerweights, recov, Ngrid, Kmodified, cs, q)$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(K)-1)
    for(i in seq_along(K[-1])){
        deltas[i] <- BCtranche.delta(hy21portfolio.tweaked, hy21, 0.05, K[i], K[i+1], rhovec[i], rhovec[i+1], Z, w, Ngrid, tradedate)
    }
    deltasmat <- rbind(deltasmat, deltas)
    rhomat <- rbind(rhomat, rhovec)
}