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
|
if(.Platform$OS.type == "unix"){
root.dir <- "/home/share/CorpCDOs"
}else{
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"))
workdate <- as.Date('2013-01-24')
calibration.date <- as.Date('2013-01-24')
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])
support <- seq(0, 1, length = Ngrid)
for(deal.name in dealnames){
load(file.path(root.dir, "Scenarios", paste("Portfolios", calibration.date, 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, 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)){
## reinvmaturity <- min(deal.datesmonthly[i]+global.params$rollingmaturity, deal.dates[length(deal.dates)])
## if(deal.datesmonthly[i]>reinvmaturity-45){
## reinvloanprice <- 1
## reinvbondprice <- 1
## }else{
## reinvloanprice[i] <- forwardportfolioprice(deal.portfolio, deal.datesmonthly[i], reinvmaturity, "FLOAT", 0.025)
## reinvbondprice[i] <- forwardportfolioprice(deal.portfolio, deal.datesmonthly[i], reinvmaturity, "FIXED", 0.07)
## }
## }
reinvloanprice <- 0.965
reinvbondprice <- 0.965
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)
}
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(rbind(100*reinvloanprice, 100*reinvbondprice),
file = file.path(save.dir, paste0(deal.name,"-reinvprices.csv")),
row.names=F, col.names=F, sep=",")
save(scenariosd, scenariosr, file=file.path(save.dir, paste0(deal.name, ".RData")))
cat("generated scenarios for:", deal.name, "\n")
}
|