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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
library("RQuantLib")
library("parallel")
root.dir <- "//WDSENTINEL/share/CorpCDOs"
source(file.path(root.dir, "code", "R", "intex_deal_functions.R"))
source(file.path(root.dir, "code", "R", "index_definitions.R"))
MarkitData <- getMarkitIRData()
L1m <- buildMarkitYC(MarkitData, dt = 1/12)
L2m <- buildMarkitYC(MarkitData, dt = 1/6)
L3m <- buildMarkitYC(MarkitData)
L6m <- buildMarkitYC(MarkitData, dt = 1/2)
L12m <- buildMarkitYC(MarkitData, dt = 1)
setEvaluationDate(as.Date(MarkitData$effectiveasof))
today <- function(){
return(as.Date(MarkitData$effectiveasof))
}
bps <- 1e-4
global.params <- list()
global.params$recovery.assumptions <- list("Loan"=0.7,
"Senior Notes"=0.7,
"SecondLien"=0.3,
"Bond"=0.4,
"Mezzanine"=0.15,
"Adj_Covlite"=0.1,
"Credit Default Swap"=0)
global.params$cdoprices <- list("Aaa"=90,
"Aa"=80,
"A"=70,
"Baa"=60,
"Ba"=50,
"B"=40,
"NR"=40)
#reinvest in 7 years assets
global.params$rollingmaturity <- 7 * 365
global.params$defaultedlag <- 90
global.params$defaultcorr <- 0.4
global.params$defaultbondhazardrate <- 1000 * bps
global.params$defaultloanhazardrate <- 1000 * bps
global.params$alpha <- 0.25
global.params$beta <- 15
global.params$shape <- function(T)0.25+(1-exp(-T/5))
dealnames <- listdealnames()
dealnames <- c("abcl071", "ammcclo5", "atr4cdo", "atr5cdo", "blumt3", "callid6", "goldl5", "limes", "oceant2", "symph4")
calibration.date <- Sys.Date()
r <- c()
for(deal in dealnames){
load.portfolio(deal)
r <- rbind(r, c(sum(deal.portfolio$notional), crossprod(deal.portfolio$notional, deal.portfolio$price)/100))
}
cusipdata <- cusip.data()
do <- function(deal.name){
deal.data <- getdealdata(deal.name)
if(deal.data$"Curr Collat Bal" < 1 ||is.na(deal.data$maturity)||deal.data$maturity<=today()+90){
return()
}
deal.portfolio <- buildSC.portfolio(deal.name, deal.data, cusipdata, global.params)
A <- SPmatrix2(deal.portfolio$SC, deal.data, freq="3 months")
S <- 1 - sapply(deal.portfolio$SC, attr, "recov")
deal.weights <- deal.portfolio$notional/sum(deal.portfolio$notional)
deal.dates <- getdealschedule(deal.data)
deal.spread <- portfoliospread(deal.portfolio, hy19$maturity)
save.dir <- file.path(root.dir, "Scenarios", paste("Portfolios", calibration.date, sep="_"))
if(!file.exists(save.dir)){
dir.create(save.dir)
}
save(deal.portfolio, A, S, deal.weights, deal.dates, deal.spread,
file=file.path(save.dir, paste0(deal.name, ".RData")))
cat(deal.name, "... done\n")
}
load.portfolio <- function(dealname){
load(file.path(root.dir, "Scenarios", paste("Portfolios", calibration.date, sep="_"),
paste0(dealname, ".RData")), .GlobalEnv)
}
## build portfolio data
for(deal.name in dealnames){
do(deal.name)
}
calibration <- read.table(file.path(root.dir, "Scenarios", "Calibration",
paste0("calibration-", calibration.date,".csv")), sep=",", header=T)
Z <- calibration$Z
w <- calibration$w
rho <- 0.45
Ngrid <- 201
cl <- makeCluster(6)
clusterExport(cl, list("root.dir", "shockprob", "lossdistC.prepay.joint", "lossrecovdist.joint.term", "lossdistC.joint"))
support <- seq(0, 1, length=Ngrid)
useCluster <- TRUE
for(deal.name in dealnames[10]){
load(file.path(root.dir, "Scenarios", paste("Portfolios", calibration.date, sep="_"), paste(deal.name, "RData", sep=".")))
dp <- A$DP
pp <- A$PP
dpmod <- MFupdate.prob(Z, w, rho, dp)
ppmod <- MFupdate.prob(-Z, w, rho, pp)
## dist <- MFlossrecovdist.prepay(w, Z, rho, dp, dpmod, pp, ppmod, deal.weights, 1-S, Ngrid, TRUE)
if(useCluster){
clusterExport(cl, list("deal.weights"))
dist.joint <- MFlossdist.prepay.joint(cl, w, Z, rho, dp, dpmod,
pp, ppmod, deal.weights, 1-S, Ngrid, FALSE, n.chunks=4)
clusterCall(cl, gc)
gc()
}else{
## if don't want to use cluster (use less memory) but slower
dist.joint <- MFlossdist.prepay.joint(NULL, w, Z, rho, dp, dpmod,
pp, ppmod, deal.weights, 1-S, Ngrid, FALSE)
}
## two ways to compute the joint (D, R) distribution
## first using the actual function (numerically instable)
## dist.joint2 <- MFlossdist.prepay.joint(cl, w, Z, rho, dp, dpmod,
## pp, ppmod, issuerweights, 1-S, Ngrid=201, TRUE)
## second, by doing a change of variable seems to work better for now
distDR <- dist.transform(dist.joint)
## compute E(R|D)
R <- matrix(0, Ngrid, ncol(dp))
for(t in 1:ncol(dp)){
R[,t] <- (sweep(distDR[t,,], 1, rowSums(distDR[t,,]), "/") %*% support)/support
}
R[1,] <- 0
n.scenarios <- 100
percentiles <- (seq(0, 1, length=n.scenarios+1)[-1]+
seq(0, 1, length=n.scenarios+1)[-(n.scenarios+1)])/2
## compute scenariosd
scenariosd <- matrix(0, n.scenarios, ncol(dp))
for(t in 1:ncol(dp)){
D <- rowSums(distDR[t,,])
D <- D/sum(D)
Dfun <- splinefun(c(0, cumsum(D)), c(0, support), "monoH.FC")
## dvallow <- floor(Dfun(percentiles)*(Ngrid-1))
## dvalup <- ceil(Dfun(percentiles)*(Ngrid-1))
scenariosd[,t] <- Dfun(percentiles)
}
## compute scenariosr
scenariosr <- matrix(0, n.scenarios, ncol(dp))
for(t in 1:ncol(dp)){
Rfun <- approxfun(support, R[,t], rule=2)
scenariosr[,t] <- Rfun(scenariosd[,t])
}
cdr <- cdrfromscenarios(scenariosd, deal.dates)
intexrecov <- recoveryfromscenarios(scenariosd, scenariosr)
## linear approximation for monthly scenarios
deal.data <- getdealdata(deal.name)
deal.datesmonthly <- getdealschedule(deal.data, "1 month")
## compute reinvestment price
reinvloanprice <- rep(0, length(deal.datesmonthly))
reinvbondprice <- rep(0, length(deal.datesmonthly))
for(i in 1:length(deal.datesmonthly)){
reinvloanprice[i] <- forwardportfolioprice(deal.portfolio, deal.datesmonthly[i], global.params$rollingmaturity, "FLOAT", 0.025)
reinvbondprice[i] <- forwardportfolioprice(deal.portfolio, deal.datesmonthly[i], global.params$rollingmaturity, "FIXED", 0.07)
}
cdrmonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
recoverymonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
for(i in 1:n.scenarios){
cdrmonthly[i,] <- approx(deal.dates, cdr[i,], deal.datesmonthly, rule=2)$y
recoverymonthly[i,] <- approx(deal.dates, intexrecov[i,], deal.datesmonthly, rule=2)$y
}
save.dir <- file.path(root.dir, "Scenarios", paste("Intex curves", calibration.date, sep="_"), "csv")
if(!file.exists(save.dir)){
dir.create(save.dir, recursive = T)
}
write.table(cdrmonthly,
file= file.path(save.dir, paste0(deal.name,"-cdr.csv")),
row.names=F, col.names=F, sep=",")
write.table(recoverymonthly * 100,
file=file.path(save.dir, paste0(deal.name,"-recovery.csv")),
row.names=F, col.names=F, sep=",")
write.table(rbind(100*reinvloanprice, 100*reinvbondprice),
file = file.path(save.dir, paste0(deal.name,"-reinvprices.csv")),
row.names=F, col.names=F, sep=",")
cat("generated scenarios for:", deal.name, "\n")
}
|