aboutsummaryrefslogtreecommitdiffstats
path: root/calibrate_tranches.R
diff options
context:
space:
mode:
Diffstat (limited to 'calibrate_tranches.R')
-rw-r--r--calibrate_tranches.R88
1 files changed, 78 insertions, 10 deletions
diff --git a/calibrate_tranches.R b/calibrate_tranches.R
index c1f44a58..d0a52022 100644
--- a/calibrate_tranches.R
+++ b/calibrate_tranches.R
@@ -52,7 +52,7 @@ for(i in 1:nrow(nondefaulted)){
recovery=nondefaulted$recovery[i]/100,
startdate=today(),
issuer=as.character(nondefaulted$ticker[i]))
- quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(nondefaulted[i,5:9])*0.01,
+ quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(nondefaulted[i,4:8])*0.01,
running=rep(nondefaulted$running[i]*bps, 5))
SC@curve <- cdshazardrate(quotes, nondefaulted$recovery[i]/100)
hy17portfolio <- c(hy17portfolio, SC)
@@ -150,13 +150,9 @@ for(l in 1:100){
w.mod <- program$weight
cat(err,"\n")
}
-clusterExport(cl, list("shockprob", "issuerweights", "rho", "Z", "lossrecovdist.term",
- "lossrecovdist", "lossdistribC", "Ngrid",
- "tranche.pvvec", "tranche.pv", "tranche.pl", "tranche.cl",
- "trancheloss", "trancherecov", "pos", "Kmodified", "cs"))
MFtranche.pv <- function(cl, cs, w, rho, defaultprob, p, issuerweights,
- Ngrid=length(issuerweights)+1, Kmodified, n.int=100){
+ Kmodified, Ngrid=length(issuerweights)+1, n.int=100){
## computes the tranches pv using the modified factor distribution
## p is the modified probability so that
n.credit <- length(issuerweights)
@@ -174,10 +170,36 @@ MFtranche.pv <- function(cl, cs, w, rho, defaultprob, p, issuerweights,
}
clusterExport(cl, list("Rstoch", "p"))
result <- parSapply(cl, 1:n.int, parf)
- return( 100*(1+result%*%w.mod) )
+ return( list(pv=100*(1+result%*%w), pv.w=result))
}
-## computes deltas
+MFlossdistrib <- function(cl, cs, w, rho, defaultprob, p, issuerweights,
+ Kmodified, Ngrid=2*length(issuerweights)+1, n.int=100){
+ ## computes the tranches pv using the modified factor distribution
+ ## p is the modified probability so that
+ n.credit <- length(issuerweights)
+ Rstoch <- array(0, dim=c(n.int, n.credit, ncol(defaultprob)))
+ for(t in 1:ncol(defaultprob)){
+ for(i in 1:n.credit){
+ Rstoch[,i,t] <- stochasticrecov(recov[i], 0, Z, w, rho, defaultprob[i,t], p[i,t])
+ }
+ }
+ parf <- function(i){
+ pshocked <- apply(p, 2, shockprob, rho=rho, Z=Z[i])
+ S <- 1 - Rstoch[i,,]
+ dist <- lossrecovdist.term(pshocked, 0, issuerweights, S, Ngrid)
+ }
+ L <- matrix(0, Ngrid, ncol(defaultprob))
+ R <- matrix(0, Ngrid, ncol(defaultprob))
+ for(i in 1:n.int){
+ dist <- parf(i)
+ L <- L + dist$L * w[i]
+ R <- R + dist$R * w[i]
+ }
+ return( list(L=L, R=R) )
+}
+
+## computes MFdeltas
newportf <- hy17portfolio.tweaked
eps <- 1e-4
for(i in 1:length(newportf)){
@@ -190,5 +212,51 @@ dPVindex <- indexpv(newportf, hy17)-indexpv(hy17portfolio.tweaked, hy17)
MFdeltas <- dPVtranches/dPVindex
#global deltas
-PVtranches <- MFtranche.pv(cl, cs, w.mod, rho, defaultprob, p, issuerweights)
-PVindex <-
+PVtranches <- MFtranche.pv(cl, cs, w.mod, rho, 1-SurvProb, p, issuerweights, Kmodified)
+pnlindex <- 1+t(PVtranches$pv.w)%*%diff(Kmodified)-hy17$indexref
+plot(1-cumsum(w.mod),PVindex, main="pnl of going long the index", xlab="Market factor cumulative probability distribution", type="l")
+pnltranches <- sweep(1+t(PVtranches$pv.w), 2, tranche.upf/100)
+matplot(1-cumsum(w.mod), pnltranches, 2, tranche.upf/100, main="pnl of going long the tranches", xlab="Market factor cumulative probability distribution", type="l")
+global.deltas <- rep(0,4)
+for(i in 1:4){
+ global.deltas[i] <- lm(pnltranches[,i]~0+pnlindex, weights=w.mod)$coef
+}
+pnlhedged.model <- pnltranches-pnlindex%*%t(global.deltas)
+pnlhedged.market <- pnltranches-pnlindex%*%t(market.deltas)
+## generate a bunch of plots
+postscript("hedged tranches global.eps")
+matplot(1-cumsum(w.mod), pnlhedged.model, type="l", ylab="pnl of the hedged package (global deltas)", xlab="Market factor cumulative probability distribution")
+legend(0.35, y=0.75, c("0-15","15-25","25-35","35-100"), col=1:4, lty=1:4)
+dev.off()
+postscript("hedged tranches market.eps")
+matplot(1-cumsum(w.mod), pnlhedged.market, type="l", ylab="Pnl", main="pnl of the hedged package (market deltas)", xlab="Market factor cumulative probability distribution")
+legend(0.15, y=1.15, c("0-15","15-25","25-35","35-100"), col=1:4, lty=1:4)
+dev.off()
+postscript("0-15 hedged.eps")
+matplot(1-cumsum(w.mod), cbind(pnlhedged.model[,1], pnlhedged.market[,1]), type="l", ylab="Pnl", main="Pnl of the hedged package (market vs global deltas)\n 0-15 tranche", xlab="Market factor cumulative probability distribution")
+dev.off()
+postscript("15-25 hedged.eps")
+matplot(1-cumsum(w.mod), cbind(pnlhedged.model[,2], pnlhedged.market[,2]), type="l", ylab="Pnl", main="Pnl of the hedged package (market vs global deltas)\n 15-25 tranche", xlab="Market factor cumulative probability distribution")
+dev.off()
+postscript("25-35 hedged.eps")
+matplot(1-cumsum(w.mod), cbind(pnlhedged.model[,3], pnlhedged.market[,3]), type="l", ylab="Pnl", main="Pnl of the hedged package (market vs global deltas)\n 25-35 tranche", xlab="Market factor cumulative probability distribution")
+dev.off()
+postscript("35-100 hedged.eps")
+matplot(1-cumsum(w.mod), cbind(pnlhedged.model[,4], pnlhedged.market[,4]), type="l", ylab="Pnl", main="Pnl of the hedged package (market vs global deltas)\n 35-100 tranche", xlab="Market factor cumulative probability distribution")
+dev.off()
+
+## scenario based pricing
+## generate the scenarios by finding the quantiles of the loss and recovery distributions
+l <- matrix(0, 17, 99)
+r <- matrix(0, 17, 99)
+d1 <- seq(0.01, 0.99, 0.01)
+Ngrid <- 5*96+1
+MFdist <- MFlossdistrib(cl, cs, w.mod, rho, defaultprob, p, issuerweights, Kmodified, Ngrid)
+for(i in 1:17){
+ Lfun <- splinefun(c(0, cumsum(MFdist$L[,i])),c(0, seq(0, 1, length=Ngrid)), "monoH.FC")
+ Rfun <- splinefun(c(0, cumsum(MFdist$R[,i])),c(0, seq(0, 1, length=Ngrid)), "monoH.FC")
+ for(j in 1:99){
+ l[i, j] <- Lfun(d1[j])
+ r[i, j] <- Rfun(d1[j])
+ }
+}