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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
#!/usr/bin/Rscript
require(methods)
options(warn=2)
args <- commandArgs(trailingOnly=TRUE)
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", "index_definitions.R"))
source(file.path(root.dir, "code", "R", "tranche_functions.R"))
source(file.path(root.dir, "code", "R", "yieldcurve.R"))
source(file.path(root.dir, "code", "R", "optimization.R"))
##figure out the workdate
if(length(args) >= 1){
workdate <- as.Date(args[1])
}else{
workdate <- Sys.Date()
}
futurequotes <- read.csv(file.path(root.dir, "data", "Yield Curves",
sprintf("futures-%s.csv", workdate)), header=F)
#retrieve yield curve data
MarkitData <- getMarkitIRData(workdate)
L1m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/12)
L2m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/6)
L3m <- buildMarkitYC(MarkitData, futurequotes[,2])
L6m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/2)
setEvaluationDate(as.Date(MarkitData$effectiveasof))
setCalendarContext("UnitedStates/GovernmentBond")
## calibrate HY21
## calibrate the single names curves
singlenames.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
paste0("hy21_singlenames_", workdate, ".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=workdate+1))
}
hy21portfolio <- c()
for(i in 1:nrow(nondefaulted)){
SC <- new("creditcurve",
recovery=nondefaulted$recovery[i]/100,
startdate=workdate,
issuer=as.character(nondefaulted$ticker[i]))
quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(nondefaulted[i,4:8]) /100,
running=rep(nondefaulted$running[i] * bps, 5))
SC@curve <- cdshazardrate(quotes, nondefaulted$recovery[i]/100, workdate)
hy21portfolio <- c(hy21portfolio, SC)
}
issuerweights <- rep(1/length(hy21portfolio), length(hy21portfolio))
## load tranche data
K <- c(0, 0.15, 0.25, 0.35, 1)
Kmodified <- adjust.attachments(K, hy21$loss, hy21$factor)
markit.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
paste0("hy21_tranches_", workdate, ".csv")))
tranche.upf <- markit.data$Mid
tranche.running <- c(0.05, 0.05, 0.05, 0.05)
# get the index ref
hy21$indexref <- markit.data$bidRefPrice[1]/100
hy21portfolio.tweaked <- tweakcurves(hy21portfolio, hy21, workdate)
SurvProb <- SPmatrix(hy21portfolio.tweaked, hy21)
Ngrid <- 2 * nrow(nondefaulted) + 1
recov <- sapply(hy21portfolio.tweaked, attr, "recovery")
cs <- couponSchedule(nextIMMDate(workdate), hy21$maturity,"Q", "FIXED", 0.05, 0)
##calibrate by modifying the factor distribution
bottomup <- 1:3
topdown <- 2:4
n.int <- 500
n.credit <- length(hy21portfolio)
errvec <- c()
quadrature <- gauss.quad.prob(n.int, "normal")
w <- quadrature$weights
Z <- quadrature$nodes
w.mod <- w
defaultprob <- 1 - SurvProb
p <- defaultprob
rho <- rep(0.45, n.credit)
result <- matrix(0, 4, n.int)
for(l in 1:150){
Rstoch <- array(0, dim=c(n.credit, n.int, ncol(SurvProb)))
for(t in 1:ncol(SurvProb)){
for(i in 1:n.credit){
Rstoch[i,,t] <- stochasticrecovC(recov[i], 0, Z, w.mod, rho[i], defaultprob[i,t], p[i,t])
}
}
L <- array(0, dim=c(n.int, Ngrid, ncol(defaultprob)))
R <- array(0, dim=c(n.int, Ngrid, ncol(defaultprob)))
for(t in 1:ncol(defaultprob)){
S <- 1 - Rstoch[,,t]
L[,,t] <- t(lossdistCZ(p[,t], issuerweights, S, Ngrid, 0, rho, Z))
R[,,t] <- t(lossdistCZ(p[,t], issuerweights, 1-S, Ngrid, 0, rho, Z))
}
for(i in 1:n.int){
result[,i] <- tranche.pvvec(Kmodified, L[i,,], R[i,,], cs)
}
## solve the optimization problem
program <- KLfit(100*(result[bottomup,]+1), w, 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) - defaultprob[i,j])
}
}
errvec <- c(errvec, err)
## update the new probabilities
p <- MFupdate.probC(Z, program$weight, rho, defaultprob)
errvec <- c(errvec, err)
w.mod <- program$weight
cat(err,"\n")
}
Rstoch <- array(0, dim=c(n.credit, n.int, ncol(SurvProb)))
for(t in 1:ncol(SurvProb)){
for(i in 1:n.credit){
Rstoch[i,,t] <- stochasticrecovC(recov[i], 0, Z, w.mod, rho[i], defaultprob[i,t], p[i,t])
}
}
Lw <- matrix(0, Ngrid, n.int)
Rw <- matrix(0, Ngrid, n.int)
L <- matrix(0, Ngrid, ncol(defaultprob))
R <- matrix(0, Ngrid, ncol(defaultprob))
for(t in 1:ncol(defaultprob)){
S <- 1 - Rstoch[,,t]
Lw <- lossdistCZ(p[,t], issuerweights, S, Ngrid, 0, rho, Z)
Rw <- lossdistCZ(p[,t], 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-", workdate, ".csv")),
col.names=T, row.names=F, sep=",")
save(singlenames.data, hy21, tranche.upf, dist,
file = file.path(root.dir, "Scenarios", "Calibration",
paste0("marketdata-", workdate, ".RData")), compress="xz")
|