aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/backtest.R69
1 files changed, 47 insertions, 22 deletions
diff --git a/R/backtest.R b/R/backtest.R
index cef602cf..493ffbc5 100644
--- a/R/backtest.R
+++ b/R/backtest.R
@@ -5,6 +5,10 @@ if(.Platform$OS.type == "unix"){
root.dir <- "//WDSENTINEL/share/CorpCDOs"
}
library(ggplot2)
+library(lubridate)
+library(doParallel)
+
+registerDoParallel(8)
source(file.path(root.dir, "code", "R", "serenitasdb.R"))
source(file.path(root.dir, "code", "R", "cds_functions_generic.R"))
@@ -31,43 +35,58 @@ get.indexmaturity <- function(index, series){
}
fastduration <- function(sc, cs, tradedate, maturities){
+ r <- rep(NA, length(maturities))
+ if(is.null(sc)){
+ return( r )
+ }
startdate <- tradedate+1
acc <- cdsAccrued(tradedate, 1)
- r <- numeric(length(maturities))
for(i in seq_along(maturities)){
if(startdate>maturities[i]){
r[i] <- NA
}else{
r[i] <- couponleg(cs[cs$unadj.dates<=maturities[i],], sc,
- startdate, accruedondefault=TRUE)
+ startdate, accruedondefault=TRUE)-acc
}
}
- r <- r-acc
return( r )
}
fasttheta <- function(sc, cs, recov, tradedate, maturities, quotes){
+ r <- rep(NA, length(maturities))
+ if(is.null(sc)){
+ return(r)
+ }
startdate <- tradedate+1
acc <- cdsAccrued(tradedate, 1)
- r <- numeric(length(maturities))
+
newmaturities <- maturities+years(-1)
for(i in seq_along(newmaturities)){
- if(startdate>newmaturities[i]){
- r[i] <- NA
+ ## never extrapolate, and do not attempt to compute theta if within 1 year
+ if(startdate>newmaturities[i] || is.na(quotes[i])){
+ next
}else{
newcs <- cs[cs$unadj.dates<=newmaturities[i],]
- upfront <- (couponleg(newcs, sc, startdate, accruedondefault=TRUE)-acc)*0.05-
- defaultleg(newcs, sc, recov, startdate)
- r[i] <- upfront+quotes[i]+0.05
+ upfront <- defaultleg(newcs, sc, recov, startdate) -
+ (couponleg(newcs, sc, startdate, accruedondefault=TRUE)-acc)*0.05
+ r[i] <- quotes[i]-upfront+0.05
}
}
return( r )
}
+convertNA <- function(x){
+ if(is.na(x)){
+ return( "Null" )
+ }else{
+ return( x )
+ }
+}
+
+
index <- 'HY'
tenors <- c("3yr", "5yr", "7yr")
recov <- 0.3
-colnames(df) <- c("date", tenors)
sqlstr <- paste("UPDATE index_quotes set duration=%s, theta=%s where date='%s' and index='%s'",
"and series=%s and tenor='%s'")
@@ -77,33 +96,39 @@ for(series in c(10, 11, 13, 15, 17, 19, 21, 23)){
maturities <- maturities[maturities$tenor %in% tenors,]
durations <- matrix(0, nrow(indexquotes), length(tenors))
thetas <- matrix(0, nrow(indexquotes), length(tenors))
- for (i in 1:nrow(indexquotes)){
+ maturity <- maturities[nrow(maturities), "maturity"]
+ durandthetas <- foreach(i = 1:nrow(indexquotes), .combine='rbind') %dopar% {
tradedate <- indexquotes[i, "date"]
exportYC(tradedate)
- maturity <- maturities[nrow(maturities), "maturity"]
cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE), maturity,"Q", "FIXED", 1,
0, tradedate, IMMDate(tradedate, "prev"))
quotes <- data.frame(upfront=(100-as.numeric(indexquotes[i,-1]))/100, maturities)
sc <- cdshazardrate(quotes, recov, tradedate, cs)
- durations[i,] <- fastduration(sc, cs, tradedate, maturities$maturity)
- thetas[i,] <- fasttheta(sc, cs, recov, tradedate, maturities$maturity, quotes$upfront)
+ c(fastduration(sc, cs, tradedate, maturities$maturity),
+ fasttheta(sc, cs, recov, tradedate, maturities$maturity, quotes$upfront))
}
- df.durations <- data.frame(date=indexquotes$date, durations)
- df.thetas <- data.frame(date=indexquotes$date, thetas)
+ df.durations <- data.frame(date=indexquotes$date, durandthetas[,1:3])
+ df.thetas <- data.frame(date=indexquotes$date, durandthetas[,4:6])
colnames(df.durations) <- c("date", tenors)
colnames(df.thetas) <- c("date", tenors)
for(i in 1:nrow(df.durations)){
for(tenor in tenors){
- if(!is.na(df[i,tenor])){
- stmt <- sprintf(sqlstr, df.durations[i,tenor], df.thetas[i, tenor],
- df[i,"date"], index, series, tenor)
- dbSendQuery(serenitasdb, stmt)
- }
+ stmt <- sprintf(sqlstr, convertNA(df.durations[i,tenor]), convertNA(df.thetas[i, tenor]),
+ df.durations[i,"date"], index, series, tenor)
+ dbSendQuery(serenitasdb, stmt)
}
}
## nice plot, now I'm just showing off
- ggplot(df, aes(x=date))+geom_line(aes(y=`3yr`, colour="3yr"))+
+ ggplot(df.durations, aes(x=date))+geom_line(aes(y=`3yr`, colour="3yr"))+
geom_line(aes(y=`5yr`, colour="5yr"))+
geom_line(aes(y=`7yr`, colour="7yr"))+ylab("duration")+labs(colour="tenor")
ggsave(filename=paste0("HY", series, " durations.png"))
+ ## plot thetas
+ ggplot(df.thetas, aes(x=date))+geom_line(aes(y=`3yr`, colour="3yr"))+
+ geom_line(aes(y=`5yr`, colour="5yr"))+
+ geom_line(aes(y=`7yr`, colour="7yr"))+ylab("theta")+labs(colour="tenor")
+ ggsave(filename=paste0("HY", series, " thetas.png"))
}
+
+alldates <- seq(begin.date, as.Date(Sys.Date()), by="1 day")
+bus.dates <- alldates[isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates)]