aboutsummaryrefslogtreecommitdiffstats
path: root/R/script_calibrate_tranches.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/script_calibrate_tranches.R')
-rw-r--r--R/script_calibrate_tranches.R59
1 files changed, 30 insertions, 29 deletions
diff --git a/R/script_calibrate_tranches.R b/R/script_calibrate_tranches.R
index e98e8004..fdb0fa65 100644
--- a/R/script_calibrate_tranches.R
+++ b/R/script_calibrate_tranches.R
@@ -25,83 +25,84 @@ if(length(args) >= 1){
tradedate <- Sys.Date()
}
-hy21 <- load.index("hy21", date=tradedate)
+index <- load.index("hy21", date=tradedate)
exportYC(tradedate)
## calibrate HY21
## calibrate the single names curves
-hy21 <- set.singlenamesdata(hy21, tradedate)
-hy21 <- set.tranchedata(hy21, tradedate)
+index <- set.singlenamesdata(index, tradedate)
+index <- set.tranchedata(index, tradedate)
## load tranche data
-n.credit <- length(hy21$portfolio)
+n.credit <- length(index$portfolio)
Ngrid <- 2 * n.credit + 1
-acc <- cdsAccrued(tradedate, 0.05)
+acc <- cdsAccrued(tradedate, index$tranche.running[1])
+index$cs$coupons <- index$cs$coupons*0.05
##calibrate by modifying the factor distribution
bottomup <- 1:3
topdown <- 2:4
n.int <- 500
-n.credit <- length(hy21$portfolio)
+n.credit <- length(index$portfolio)
errvec <- c()
quadrature <- GHquad(n.int)
w <- quadrature$w
Z <- quadrature$Z
w.mod <- w
-p <- hy21$defaultprob
+p <- index$defaultprob
rho <- rep(0.45, n.credit)
result <- matrix(0, 4, n.int)
err <- Inf
while(err >0.01){
- Rstoch <- array(0, dim=c(n.credit, n.int, ncol(hy21$defaultprob)))
- for(t in 1:ncol(hy21$defaultprob)){
+ Rstoch <- array(0, dim=c(n.credit, n.int, ncol(index$defaultprob)))
+ for(t in 1:ncol(index$defaultprob)){
for(i in 1:n.credit){
- Rstoch[i,,t] <- stochasticrecovC(hy21$recov[i], 0, Z, w.mod, rho[i],
- hy21$defaultprob[i,t], p[i,t])
+ Rstoch[i,,t] <- stochasticrecovC(index$recov[i], 0, Z, w.mod, rho[i],
+ index$defaultprob[i,t], p[i,t])
}
}
- L <- array(0, dim=c(n.int, Ngrid, ncol(hy21$defaultprob)))
- R <- array(0, dim=c(n.int, Ngrid, ncol(hy21$defaultprob)))
- for(t in 1:ncol(hy21$defaultprob)){
+ L <- array(0, dim=c(n.int, Ngrid, ncol(index$defaultprob)))
+ R <- array(0, dim=c(n.int, Ngrid, ncol(index$defaultprob)))
+ for(t in 1:ncol(index$defaultprob)){
S <- 1 - Rstoch[,,t]
- L[,,t] <- t(lossdistCZ(p[,t], hy21$issuerweights, S, Ngrid, 0, rho, Z))
- R[,,t] <- t(lossdistCZ(p[,t], hy21$issuerweights, 1-S, Ngrid, 0, rho, Z))
+ L[,,t] <- t(lossdistCZ(p[,t], index$issuerweights, S, Ngrid, 0, rho, Z))
+ R[,,t] <- t(lossdistCZ(p[,t], index$issuerweights, 1-S, Ngrid, 0, rho, Z))
}
for(i in 1:n.int){
- result[,i] <- tranche.pvvec(hy21$K, L[i,,], R[i,,], hy21$cs) - acc
+ result[,i] <- tranche.pvvec(index$K, L[i,,], R[i,,], index$cs) - acc
}
## solve the optimization problem
- program <- KLfit(100*(result[bottomup,]+1), w, hy21$tranche.upf[bottomup])
+ program <- KLfit(100*(result[bottomup,]+1), w, index$tranche.upf[bottomup])
err <- 0
for(i in 1:n.credit){
for(j in 1:ncol(p)){
- err <- err + abs(crossprod(shockprob(p[i,j], rho[i], Z), program$weight) - hy21$defaultprob[i,j])
+ err <- err + abs(crossprod(shockprob(p[i,j], rho[i], Z), program$weight) - index$defaultprob[i,j])
}
}
errvec <- c(errvec, err)
## update the new probabilities
- p <- MFupdate.probC(Z, program$weight, rho, hy21$defaultprob)
+ p <- MFupdate.probC(Z, program$weight, rho, index$defaultprob)
errvec <- c(errvec, err)
w.mod <- program$weight
cat(err,"\n")
}
-Rstoch <- array(0, dim=c(n.credit, n.int, ncol(hy21$defaultprob)))
-for(t in 1:ncol(hy21$defaultprob)){
+Rstoch <- array(0, dim=c(n.credit, n.int, ncol(index$defaultprob)))
+for(t in 1:ncol(index$defaultprob)){
for(i in 1:n.credit){
- Rstoch[i,,t] <- stochasticrecovC(hy21$recov[i], 0, Z, w.mod, rho[i], hy21$defaultprob[i,t], p[i,t])
+ Rstoch[i,,t] <- stochasticrecovC(index$recov[i], 0, Z, w.mod, rho[i], index$defaultprob[i,t], p[i,t])
}
}
Lw <- matrix(0, Ngrid, n.int)
Rw <- matrix(0, Ngrid, n.int)
-L <- matrix(0, Ngrid, ncol(hy21$defaultprob))
-R <- matrix(0, Ngrid, ncol(hy21$defaultprob))
-for(t in 1:ncol(hy21$defaultprob)){
+L <- matrix(0, Ngrid, ncol(index$defaultprob))
+R <- matrix(0, Ngrid, ncol(index$defaultprob))
+for(t in 1:ncol(index$defaultprob)){
S <- 1 - Rstoch[,,t]
- Lw <- lossdistCZ(p[,t], hy21$issuerweights, S, Ngrid, 0, rho, Z)
- Rw <- lossdistCZ(p[,t], hy21$issuerweights, 1-S, Ngrid, 0, rho, Z)
+ Lw <- lossdistCZ(p[,t], index$issuerweights, S, Ngrid, 0, rho, Z)
+ Rw <- lossdistCZ(p[,t], index$issuerweights, 1-S, Ngrid, 0, rho, Z)
L[,t] <- Lw%*%w.mod
R[,t] <- Rw%*%w.mod
}
@@ -113,5 +114,5 @@ write.table(data.frame(Z=Z, w=w.mod),
paste0("calibration-", tradedate, ".csv")),
col.names=T, row.names=F, sep=",")
-save(hy21, dist, file = file.path(root.dir, "Scenarios", "Calibration",
+save(index, dist, file = file.path(root.dir, "Scenarios", "Calibration",
paste0("marketdata-", tradedate, ".RData")), compress="xz")