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
|
#!/usr/bin/Rscript
require(methods)
library(lossdistrib)
options(warn=2)
args <- commandArgs(trailingOnly=TRUE)
if(.Platform$OS.type == "unix"){
root.dir <- "/home/share/CorpCDOs"
}else{
root.dir <- "//WDSENTINEL/share/CorpCDOs"
}
code.dir <- Sys.getenv("CODE_DIR")
if(code.dir==""){
code.dir<-root.dir
}
source(file.path(code.dir, "code", "R", "yieldcurve.R"))
source(file.path(code.dir, "code", "R", "optimization.R"))
source(file.path(code.dir, "code", "R", "calibration.R"), chdir=TRUE)
source(file.path(code.dir, "code", "R", "mlpdb.R"))
##figure out the tradedate
if(length(args) >= 1){
tradedate <- as.Date(args[1])
}else{
tradedate <- addBusDay(Sys.Date(), -1)
}
index <- load.index("hy21", date=tradedate)
exportYC(tradedate)
## calibrate HY21
## calibrate the single names curves
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
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(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)))
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))
}
for(i in 1:n.int){
result[,i] <- tranche.pvvec(index$K, L[i,,], R[i,,], index$cs) - acc
}
## solve the optimization problem
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) - index$defaultprob[i,j])
}
}
errvec <- c(errvec, err)
## update the new probabilities
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(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)
write.table(data.frame(Z=Z, w=w.mod),
file=file.path(root.dir, "Scenarios", "Calibration",
paste0("calibration-", tradedate, ".csv")),
col.names=T, row.names=F, sep=",")
save(index, dist, file = file.path(root.dir, "Scenarios", "Calibration",
paste0("marketdata-", tradedate, ".RData")), compress="xz")
|