aboutsummaryrefslogtreecommitdiffstats
path: root/R/build_SC.R
blob: 11e2ec7b5ce78378ebde26ea7588849c5fb22108 (plain)
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
library("RQuantLib")
library("parallel")
root = "//WDSENTINEL/share/CorpCDOs"
source(file.path(root, "R", "intex_deals_functions.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,
                                           "SecondLien"=0.3,
                                           "Bond"=0.4,
                                           "Mezzanine"=0.15,
                                           "Adj_Covlite"=0.1)

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 <- 500 * bps
global.params$defaultloanhazardrate <- 400 * bps
global.params$alpha <- 0.25
global.params$beta <- 15
global.params$shape <- function(T)0.25+(1-exp(-T/5))

## dealnames <- c("stonln1", "babs072", "symph4", "flags5", "cent11", "wasatl", "oceant2", "acacl071", "limes")
dealnames <- c("acacl071", "limes")
calibration.date <- "2012-11-08"
calibration <- read.table(file.path(root, "Scenarios", 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("shockprob", "lossdistC.prepay.joint", "lossrecovdist.joint.term", "lossdistC.joint"))
support <- seq(0, 1, length=Ngrid)
for(deal.name in dealnames){
    deal.portfolio <- buildSC.portfolio(deal.name, global.params)
    deal.data <- getdealdata(deal.name)
    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)
    clusterExport(cl, list("deal.weights"))

    deal.dates <- getdealschedule(deal.data)
    ## compute reinvestment price
    reinvloanprice <- rep(0, length(deal.dates))
    reinvmezzprice <- rep(0, length(deal.dates))
    for(i in 1:length(deal.dates)){
        reinvloanprice[i] <- forwardportfolioprice(deal.portfolio, deal.dates[i], global.params$rollingmaturity, "FLOAT", 0.0235)
        reinvmezzprice[i] <- forwardportfolioprice(deal.portfolio, deal.dates[i], global.params$rollingmaturity, "FLOAT", 0.0385)
    }

    dp <- A$DP
    pp <- A$PP
    Smat <- matrix(S, length(deal.weights), ncol(dp))
    ##no correlation setup
    ##test <- lossrecovdist.joint.term(dp, pp, issuerweights, Smat, Ngrid)

    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)
    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()
    ## if don't want to use cluster (use less memory)
    ## dist.joint <- MFlossdist.prepay.joint(NULL, w, Z, rho, dp, dpmod,
    ##                                       pp, ppmod, issuerweights, 1-S, Ngrid=201, 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.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
    }

    write.table(cdrmonthly, file=file.path(root, "Scenarios", paste0(deal.name,"-cdr.csv")), row.names=F, col.names=F, sep=",")
    write.table(recoverymonthly * 100, file=file.path(root, "Scenarios", paste0(deal.name,"-recovery.csv")), row.names=F, col.names=F, sep=",")
    cat("generated scenarios for:", deal.name, "\n")
}