aboutsummaryrefslogtreecommitdiffstats
path: root/R
diff options
context:
space:
mode:
Diffstat (limited to 'R')
-rw-r--r--R/script_calibrate_tranches.R64
1 files changed, 18 insertions, 46 deletions
diff --git a/R/script_calibrate_tranches.R b/R/script_calibrate_tranches.R
index 5313ebb8..95a9082e 100644
--- a/R/script_calibrate_tranches.R
+++ b/R/script_calibrate_tranches.R
@@ -13,7 +13,7 @@ if(.Platform$OS.type == "unix"){
code.dir <- Sys.getenv("CODE_DIR")
if(code.dir==""){
- code.dir<-root.dir
+ code.dir<-root.dir
}
source(file.path(code.dir, "code", "R", "yieldcurve.R"))
source(file.path(code.dir, "code", "R", "optimization.R"))
@@ -27,52 +27,43 @@ if(length(args) >= 1){
tradedate <- addBusDay(Sys.Date(), -1)
}
-index <- load.index("hy21", date=tradedate)
exportYC(tradedate)
## calibrate HY21
## calibrate the single names curves
+n.int <- 250
+list2env(GHquad(n.int), envir=parent.frame())
+Ngrid <- 201
+index <- load.index("hy21", date=tradedate, "5yr", Z, w, Ngrid)
index <- set.singlenamesdata(index, tradedate)
index <- set.tranchedata(index, tradedate)
## load tranche data
n.credit <- length(index$portfolio)
-Ngrid <- 2 * n.credit + 1
-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(index$portfolio)
-errvec <- c()
-quadrature <- GHquad(n.int)
-w <- quadrature$w
-Z <- quadrature$Z
-w.mod <- w
+
+index$w.mod <- w
p <- index$defaultprob
rho <- rep(0.45, n.credit)
result <- matrix(0, 4, n.int)
err <- Inf
+errvec <- c()
while(err >0.01){
- 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(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(index$defaultprob)))
- R <- array(0, dim=c(n.int, Ngrid, ncol(index$defaultprob)))
+ Rstoch <- MFrecovery(index, p)
+ L <- array(0, dim=c(index$N, n.int, ncol(index$defaultprob)))
+ R <- array(0, dim=c(index$N, n.int, ncol(index$defaultprob)))
for(t in 1:ncol(index$defaultprob)){
S <- 1 - Rstoch[,,t]
- 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))
+ L[,,t] <- lossdistCZ(p[,t], index$issuerweights, S, index$N, 0, rho, index$Z)
+ R[,,t] <- lossdistCZ(p[,t], index$issuerweights, 1-S, index$N, 0, rho, index$Z)
}
for(i in 1:n.int){
- result[,i] <- tranche.pvvec(index$K, L[i,,], R[i,,], index$cs) - acc
+ result[,i] <- tranche.pvvec(index$K, L[,i,], R[,i,], index$cs)
}
## solve the optimization problem
- program <- KLfit(100*(result[bottomup,]+1), w, index$tranche.upf[bottomup])
+ program <- KLfit(-result[bottomup,], w, index$tranche.quotes[bottomup])
err <- 0
for(i in 1:n.credit){
@@ -86,32 +77,13 @@ while(err >0.01){
p <- MFupdate.probC(Z, program$weight, rho, index$defaultprob)
errvec <- c(errvec, err)
- w.mod <- program$weight
+ index$w.mod <- program$weight
cat(err,"\n")
}
-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(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(index$defaultprob))
-R <- matrix(0, Ngrid, ncol(index$defaultprob))
-for(t in 1:ncol(index$defaultprob)){
- S <- 1 - Rstoch[,,t]
- 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
-}
-
-dist <- list(L=L, R=R)
+dist <- MFdist(index)
-write.table(data.frame(Z=Z, w=w.mod),
+write.table(data.frame(Z=Z, w=index$w.mod),
file=file.path(root.dir, "Scenarios", "Calibration",
paste0("calibration-", tradedate, ".csv")),
col.names=T, row.names=F, sep=",")