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
|
library(logging)
source("yieldcurve.R")
source("optimization.R")
source("calibration.R")
source("serenitasdb.R")
source("creditIndex.R")
source("tranche_functions.R")
zerorecov.pv <- function(portfolio, cs, tradedate=Sys.Date()) {
startdate <- tradedate + 1
zerorecov.portfolio <- lapply(portfolio, function(x) {x@recovery=0;return(x)})
pl.list <- vapply(zerorecov.portfolio, function(x) {
pl <- defaultleg(cs, x@curve, x@recovery, startdate, TRUE)
if(is.na(pl)) {
cat("couldn't compute single name protection leg for", x@issuer, "\n")
return( NA )
}
return( pl )
}, numeric(1))
return( mean(pl.list) )
}
delta <- function(index, tradedate=Sys.Date()) {
old.pv.zerorecov <- zerorecov.pv(index$portfolio, index$cs, tradedate)
old.pv.index <- indexpv(index, tradedate=tradedate)$bp
new.pv.zerorecov <- zerorecov.pv(tweakportfolio(index$portfolio, 0.01),
index$cs, tradedate)
new.pv.index <- indexpv(index, 0.01, tradedate=tradedate)$bp
return( -(new.pv.zerorecov-old.pv.zerorecov) / (new.pv.index - old.pv.index) )
}
##HY
tradedate <- as.Date("2017-04-04")
exportYC(tradedate, "USD")
index <- creditIndex("HY28", "5yr")
index <- set.index.desc(index, tradedate)
index$cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE), index$maturity, "Q", "FIXED", 1,
0, tradedate, IMMDate(tradedate, "prev"))
index$quotes <- data.frame(maturity=as.Date("2022-06-20"), spread=0.05, price=1.0716)
upfront <- c()
for(R in seq(0.2, 0.45, 0.01)) {
index <- set.singlenamesdata(index, tradedate, R)
tweak <- tweakcurves(index)
upfront <- c(upfront, zerorecov.pv(tweak$portfolio, index$cs, tradedate))
}
##ITRX
tradedate <- as.Date("2017-04-05")
exportYC(tradedate, "EUR")
index <- creditIndex("XO27", "5yr")
index <- set.index.desc(index, tradedate)
index$cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE), index$maturity, "Q", "FIXED", 1,
0, tradedate, IMMDate(tradedate, "prev"))
index$quotes <- data.frame(maturity=as.Date("2022-06-20"), spread=0.05, price=1.095)
upfront <- c()
for(R in seq(0.2, 0.45, 0.01)) {
index <- set.singlenamesdata(index, tradedate, R)
tweak <- tweakcurves(index)
upfront <- c(upfront, zerorecov.pv(tweak$portfolio, index$cs, tradedate))
}
plot(seq(0.2, 0.45, 0.01), upfront, type="l", xlab="recovery")
|