aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/thetas-durations.R66
1 files changed, 33 insertions, 33 deletions
diff --git a/R/thetas-durations.R b/R/thetas-durations.R
index e4ea3110..ec95d2de 100644
--- a/R/thetas-durations.R
+++ b/R/thetas-durations.R
@@ -7,47 +7,47 @@ library(tidyr)
hostname <- system("hostname", intern=TRUE)
registerDoParallel(if(hostname=="debian") 4 else 8)
-source("serenitasdb.R")
+source("db.R")
source("cds_functions_generic.R")
source("yieldcurve.R")
+serenitasdb <- dbConn("serenitasdb")
+
get.indexquotes.table <- function(indextype, series, tenors=c("3yr", "5yr", "7yr"),
onlymissing=TRUE) {
- stmt <- str_c("SELECT date, tenor, closeprice FROM index_quotes ",
- "WHERE index=$1 AND series=$2",
- if(onlymissing) " AND duration is NULL" else NULL,
- " ORDER BY date")
- df <- dbGetQuery(serenitasdb, stmt, params = list(indextype, series))
- df <- df %>%
- filter(tenor %in% tenors) %>%
- mutate(tenor = factor(tenor, levels=tenors))
+ df <- serenitasdb %>%
+ tbl("index_quotes") %>%
+ filter(index == !!indextype, series == !!series, tenor %in% !!tenors) %>%
+ {if(onlymissing) filter(., is.null(duration)) else .} %>%
+ arrange(date) %>% select(date, tenor, closeprice) %>% collect()
if(nrow(df) == 0) {
return( df )
} else {
- return( df %>% spread(tenor, closeprice))
+ return( df %>% spread(tenor, closeprice) )
}
}
get.indexmaturity <- function(index, series){
- sqlstr <- paste("select maturity, coupon/cast(10000 as float)as running, tenor",
- "from index_maturity where index=$1",
- "and series=$2 order by maturity")
- df <- dbGetQuery(serenitasdb, sqlstr, params=list(index, series))
- df$maturity <- as.Date(df$maturity)
+ index.maturity <- tbl(serenitasdb, "index_maturity")
+ df <- index.maturity %>%
+ filter(index == !!index, series == !!series) %>%
+ mutate(running = coupon/1000) %>%
+ select(maturity, running, tenor) %>%
+ collect()
return( df )
}
fastduration <- function(sc, cs, tradedate, maturities){
r <- rep(NA, length(maturities))
- if(is.null(sc)){
+ if(is.null(sc)) {
return( r )
}
- startdate <- tradedate+1
+ startdate <- tradedate + 1
acc <- cdsAccrued(tradedate, 1)
- for(i in seq_along(maturities)){
- if(startdate <= maturities[i]){
+ for(i in seq_along(maturities)) {
+ if(startdate <= maturities[i]) {
r[i] <- couponleg(cs[cs$unadj.dates<=maturities[i],], sc,
- startdate, accruedondefault=TRUE)-acc
+ startdate, accruedondefault=TRUE) - acc
}
}
return( r )
@@ -55,22 +55,22 @@ fastduration <- function(sc, cs, tradedate, maturities){
fasttheta <- function(sc, cs, recov, tradedate, maturities, quotes, fixedrate=0.05){
r <- rep(NA, length(maturities))
- if(is.null(sc)){
+ if(is.null(sc)) {
return(r)
}
- startdate <- tradedate+1
+ startdate <- tradedate + 1
acc <- cdsAccrued(tradedate, 1)
- newmaturities <- maturities+years(-1)
+ newmaturities <- maturities + years(-1)
for(i in seq_along(newmaturities)){
## never extrapolate, and do not attempt to compute theta if within 1 year
- if(startdate>newmaturities[i] || is.na(quotes[i])){
+ if(startdate > newmaturities[i] || is.na(quotes[i])) {
next
- }else{
+ } else {
newcs <- cs[cs$unadj.dates<=newmaturities[i],]
upfront <- defaultleg(newcs, sc, recov, startdate) -
(couponleg(newcs, sc, startdate, accruedondefault=TRUE)-acc)*fixedrate
- r[i] <- quotes[i]-upfront+fixedrate
+ r[i] <- quotes[i] - upfront + fixedrate
}
}
return( r )
@@ -86,19 +86,19 @@ for(index in c('IG', 'HY')) {
tenors <- if(index=='IG') c("3yr", "5yr", "7yr", "10yr") else c("3yr", "5yr", "7yr")
for(series in 18:28) {
indexquotes <- get.indexquotes.table(index, series, tenors)
- if(nrow(indexquotes)==0) {
+ if(nrow(indexquotes) == 0) {
next
}
maturities <- get.indexmaturity(index, series)
maturities <- maturities[maturities$tenor %in% colnames(indexquotes)[-1],]
durations <- matrix(0, nrow(indexquotes), nrow(maturities))
thetas <- matrix(0, nrow(indexquotes), nrow(maturities))
- last_maturity <- maturities[nrow(maturities), "maturity"]
+ last_maturity <- maturities %>% slice(n()) %>% pull(maturity)
durandthetas <- foreach(i = 1:nrow(indexquotes), .combine='rbind') %dopar% {
- tradedate <- indexquotes[i, "date"]
+ tradedate <- indexquotes %>% slice(i) %>% pull(date)
exportYC(tradedate)
- cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE), last_maturity,"Q", "FIXED", 1,
- 0, tradedate, IMMDate(tradedate, "prev"))
+ cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE), last_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)
c(fastduration(sc, cs, tradedate, maturities$maturity),
@@ -107,7 +107,7 @@ for(index in c('IG', 'HY')) {
## ## non parallel version for easier debugging
## durandthetas <- c()
## for(i in 1:nrow(indexquotes)) {
- ## tradedate <- indexquotes[i, "date"]
+ ## tradedate <- indexquotes %>% slice(i) %>% pull(date)
## exportYC(tradedate)
## cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE), last_maturity,"Q", "FIXED", 1,
## 0, tradedate, IMMDate(tradedate, "prev"))
@@ -125,7 +125,7 @@ for(index in c('IG', 'HY')) {
colnames(df.durations) <- c("date", maturities$tenor)
colnames(df.thetas) <- c("date", maturities$tenor)
for(i in 1:nrow(df.durations)) {
- for(tenor in maturities$tenor){
+ for(tenor in maturities$tenor) {
if(!is.na(df.durations[i, tenor])) {
r <- dbSendQuery(serenitasdb, sqlstr.duration,
params = list(df.durations[i, tenor],