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
|
args <- commandArgs(trailingOnly=TRUE)
if(.Platform$OS.type == "unix"){
root.dir <- "/home/share/CorpCDOs"
}else{
root.dir <- "//WDSENTINEL/share/CorpCDOs"
}
if(length(args) >= 1){
workdate <- as.Date(args[1])
}else{
workdate <- Sys.Date()
}
if(length(args) >=2){
dealnames <- args[2:length(args)]
}else{
dealnames <- read.table(file.path(root.dir, "scripts", "scenarios.txt"))$V1
unlink(file.path(root.dir, "scripts", "scenarios.txt"))
}
source(file.path(root.dir, "code", "R", "intex_deal_functions.R"))
source(file.path(root.dir, "code", "R", "index_definitions.R"))
prevBusDay <- function(workdate = Sys.Date()){
i <- 1
while(!isBusinessDay(calendar = "UnitedStates/GovernmentBond",
workdate - i)){
i <- i+1
}
return( workdate - i )
}
compute.reinvprices <- function(DC, cdrmonthly, recoverymonthly, spread, fixedrate, rollingmat){
floatbp <- c()
fixedbp <- c()
for(i in 0:(ncol(cdrmonthly)-1)){
index <- (i+1):min(i+rollingmat, ncol(cdrmonthly))
if(i==0){
df <- DC$discounts[index]
}else{
df <- DC$discounts[index]/DC$discounts[i]
}
floatcoupon <- (DC$forwards[index]+ spread)/12
fixedcoupon <- fixedrate/12
currbalance <- 1 - cdrmonthly[,index]/100/12
browser()
if(i < ncol(cdrmonthly)-1){
currbalance <- t(apply(currbalance, 1, cumprod))
recov <- -t(apply(cbind(1, currbalance), 1, diff)) *
recoverymonthly[,index]
fixedcl <- cbind(1,currbalance[,-length(index)])%*%(fixedcoupon*df)
floatcl <- cbind(1,currbalance[,-length(index)])%*%(floatcoupon*df)
pl <- currbalance[,dim(currbalance)[2]]*df[length(df)] +
recov %*% df
}else{
fixedcl <- currbalance*fixedcoupon*df
floatcl <- currbalance*floatcoupon*df
pl <- currbalance*df
}
floatbp <- cbind(floatbp, pl+floatcl)
fixedbp <- cbind(fixedbp, pl+fixedcl)
}
return( list(loan=floatbp, bond=fixedbp) )
}
calibration.date <- prevBusDay(workdate)
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
## dealnames <- list.files(file.path(root.dir, "Scenarios", paste0("Portfolios_", calibration.date)),
## pattern="*.RData")
## dealnames <- sapply(strsplit(dealnames, "\\."), function(x)x[1])
MarkitData <- getMarkitIRData(calibration.date)
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))
support <- seq(0, 1, length = Ngrid)
for(deal.name in dealnames){
load(file.path(root.dir, "Scenarios", paste("Portfolios", workdate, sep="_"),
paste(deal.name, "RData", sep=".")))
dp <- A$DP
pp <- A$PP
dpmod <- MFupdate.probC(Z, w, rho, dp)
ppmod <- MFupdate.probC(-Z, w, rho, pp)
dist.joint <- MFlossdist.prepay.joint2(w, Z, rho, dp, dpmod, pp, ppmod,
deal.weights, 1-S, Ngrid)
distDR <- dist.transform(dist.joint)
## compute E(R|D)
R <- matrix(0, Ngrid, dim(distDR)[1])
for(t in 1:dim(distDR)[1]){
R[,t] <- sweep(distDR[t,,], 1, rowSums(distDR[t,,]), "/") %*% support
R[1,t] <- 0
if(t >= 2){
R[,t] <- pmax(R[,t], R[,t-1])
}
}
n.scenarios <- 100
## compute scenariosd
scenariosd <- matrix(0, n.scenarios, dim(distDR)[1])
scenariosr <- matrix(0, n.scenarios, dim(distDR)[1])
percentiles <- seq(0, 1, 1/n.scenarios)
for(t in 1:dim(distDR)[1]){
D <- rowSums(distDR[t,,])
Dfun <- splinefun(c(0, cumsum(D)), c(0, support), "monoH.FC")
Rfun <- approxfun(support, R[,t], rule=2)
for(i in 1:n.scenarios){
## this is roughtly E(D|D is in ith percentile)
## using trapezoidal approximation
scenariosd[i,t] <- 0.5 * (Dfun((i-1)*0.01)+Dfun(i*0.01))
if(t>=2 && scenariosd[i,t] < scenariosd[i,t-1]){
scenariosd[i,t] <- scenariosd[i,t-1]
}
scenariosr[i,t] <- Rfun(scenariosd[i,t])
if(t>=2 && scenariosr[i,t] < scenariosr[i,t-1]){
scenariosr[i,t] <- scenariosr[i,t-1]
}
}
}
intexrecov <- matrix(0, n.scenarios, dim(distDR)[1])
for(i in 1:dim(distDR)[1]){
if(i==1){
intexrecov[,i] <- (scenariosr[,i]/scenariosd[,1])
}else{
intexrecov[,i] <- (scenariosr[,i]-scenariosr[,i-1])/(scenariosd[,i]-scenariosd[,i-1])
}
}
cdr <- cdrfromscenarios(scenariosd, deal.dates)
## linear approximation for monthly scenarios
deal.data <- getdealdata(deal.name)
deal.datesmonthly <- getdealschedule(deal.data, "1 month")
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
}
## compute reinvestment price
DC <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, deal.datesmonthly))
reinvprices <- compute.reinvprices(DC, cdrmonthly, recoverymonthly, 0.025, 0.07, 84)
save.dir <- file.path(root.dir, "Scenarios", paste("Intex curves", workdate, sep="_"), "csv")
if(!file.exists(save.dir)){
dir.create(save.dir, recursive = T)
}
recoverymonthly <- pmin(recoverymonthly,1)
write.table(cdrmonthly,
file= file.path(save.dir, paste0(deal.name,"-cdr.csv")),
row.names=F, col.names=F, sep=",")
write.table(100 * recoverymonthly,
file=file.path(save.dir, paste0(deal.name,"-recovery.csv")),
row.names=F, col.names=F, sep=",")
write.table(100 * reinvprices$loan,
file = file.path(save.dir, paste0(deal.name, "-floatreinvprices.csv")),
row.names=F, col.names=F, sep=",")
write.table(100 * reinvprices$bond,
file = file.path(save.dir, paste0(deal.name, "-fixedreinvprices.csv")),
row.names=F, col.names=F, sep=",")
save(scenariosd, scenariosr, dist.joint, file=file.path(save.dir, paste0(deal.name, ".RData")))
cat("generated scenarios for:", deal.name, "\n")
}
|