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
|
if(.Platform$OS.type == "unix"){
root.dir <- "/home/share/CorpCDOs"
}else{
root.dir <- "//WDSENTINEL/share/CorpCDOs"
}
options(stringsAsFactors=FALSE)
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", "yieldcurve.R"))
source(file.path(root.dir, "code", "R", "optimization.R"))
library(lossdistrib)
load.index("hy21")
load.index("hy19")
n.int <- 250
bps <- 1e-4
attach(GHquad(n.int))
tradedate <- as.Date("2014-05-05")
exportYC(tradedate)
cdsdates <- as.Date(character(0))
for(tenor in paste0(1:5, "y")){
cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate))
}
cds.cs <- couponSchedule(IMMDate(tradedate), cdsdates[length(cdsdates)], "Q", "FIXED",
1, tradedate, IMMDate(tradedate, "prev"))
##build portfolio
buildSC <- function(quote, cs){
SC <- new("creditcurve",
recovery=quote$recovery/100,
startdate=tradedate,
issuer=as.character(quote$ticker))
quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(quote[4:8]) * 0.01,
running=rep(quote$running*1e-4, 5))
SC@curve <- cdshazardrate(quotes, SC@recovery, tradedate, cs)
return( SC )
}
set.singlenamesdata <- function(index, tradedate){
index.name <- deparse(substitute(index))
singlenames.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
paste0(index.name, "_singlenames_", tradedate, ".csv")))
nondefaulted <- singlenames.data[!singlenames.data$ticker %in% index$defaulted,]
index$portfolio <- c()
for(i in 1:nrow(nondefaulted)){
index$portfolio <- c(index$portfolio, buildSC(nondefaulted[i,], cds.cs))
}
index$issuerweights <- rep(1/length(index$portfolio), length(index$portfolio))
index$recov <- sapply(index$portfolio, attr, "recovery")
assign(index.name, index, envir=parent.env(environment()))
}
## load all the single names data
## calibrate the single names curves
set.singlenamesdata(hy21, tradedate)
set.singlenamesdata(hy19, tradedate)
## load tranche data
set.tranchedata <- function(index, tradedate){
index.name <- deparse(substitute(index))
index$tranche.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
paste0(index.name, "_tranches_", tradedate, ".csv")), header=TRUE)
index$indexref <- index$tranche.data$bidRefPrice[1]/100
index$portfolio.tweaked <- tweakcurves(index$portfolio, index, tradedate)$portfolio
index$cs <- couponSchedule(IMMDate(tradedate), index$maturity,"Q", "FIXED", 0.05, 0, tradedate,
IMMDate(tradedate, "prev"))
index$defaultprob <- 1-SPmatrix(index$portfolio.tweaked, length(index$cs$dates))
K <- c(0, 0.15, 0.25, 0.35, 1)
index$K <- adjust.attachments(K, index$loss, index$factor)
index$tranche.upf <- index$tranche.data$Mid
index$tranche.running <- index$tranche.data$Coupon
##convert the quotes
## - we convert to protection terms x->1-x/100
## - we remove accrued x->x-acc
## - we weight it by the size of the tranche
## - we take the cumsum to convert to 0-5, 0-10, 0-15 quotes, etc...
## calibrate the tranches using base correlation
index$quotes <- cumsum(diff(index$K) *
(1-index$tranche.upf/100-cdsAccrued(tradedate, index$tranche.running)))
assign(index.name, index, envir=parent.env(environment()))
}
set.tranchedata(hy19, tradedate)
set.tranchedata(hy21, tradedate)
## load common parameters
Ngrid <- 201
f <- function(rho, index, N, i){
temp <- with(index,
BClossdistC(defaultprob, issuerweights, recov, rho, Z, w, N))
return(abs(tranche.pv(temp$L, temp$R, index$cs, 0, index$K[i+1]) + index$quotes[i]))
}
rhovec <- c()
for(i in 1:3){
rho <- optimize(f, interval=c(0,1), index=hy21, N=Ngrid, i=i)$minimum
rhovec <- c(rhovec, rho)
}
rhovec <- c(0, rhovec)
K <- c(0, 0.15, 0.25, 0.35, 1)
rhofun <- approxfun(K[-5], rhovec, rule=2)
Kmapped <- rep(0, 3)
for(i in 2:4){
Kmapped[i-1] <- skewmapping(hy21, rhofun, hy19, K[i], Z, w, 201)$minimum
}
|