diff options
| -rw-r--r-- | R/calibration.R | 21 | ||||
| -rw-r--r-- | R/mlpdb.R | 20 |
2 files changed, 25 insertions, 16 deletions
diff --git a/R/calibration.R b/R/calibration.R index 5c1a2c79..86ca5065 100644 --- a/R/calibration.R +++ b/R/calibration.R @@ -8,11 +8,11 @@ source(file.path(root.dir, "code", "R", "cds_functions_generic.R")) buildSC <- function(quote, cs, cdsdates){ SC <- new("creditcurve", - recovery=quote$recovery/100, + recovery=quote$recovery, 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)) + issuer=quote$ticker) + quotes <- data.frame(maturity=cdsdates, upfront = quote$upfront, + running=quote$running) SC@curve <- cdshazardrate(quotes, SC@recovery, tradedate, cs) return( SC ) } @@ -27,13 +27,16 @@ get.cdsSchedule <- function(tradedate){ } set.singlenamesdata <- function(index, tradedate){ - 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,] cds.cs <- get.cdsSchedule(tradedate) + quotes <- get.indexquotes(index$name, tradedate) + tenor <- paste0(1:5, "y") index$portfolio <- list() - for(i in 1:nrow(nondefaulted)){ - index$portfolio <- c(index$portfolio, buildSC(nondefaulted[i,], cds.cs$cs, cds.cs$cdsdates)) + for(i in seq_along(quotes$tickers)){ + quote <- list(ticker = quotes$ticker[i], + running = quotes$spread_curve[i, tenor] * 1e-4, + upfront = quotes$upfront_curve[i, tenor] * 0.01, + recovery = quotes$recovery[i,tenor][1]) + index$portfolio <- c(index$portfolio, buildSC(quote, cds.cs$cs, cds.cs$cdsdates)) } index$issuerweights <- rep(1/length(index$portfolio), length(index$portfolio)) index$recov <- sapply(index$portfolio, attr, "recovery") @@ -3,7 +3,7 @@ drv <- dbDriver("PostgreSQL") mlpdbCon <- dbConnect(drv, dbname="mlpdb", user="mlpdb_user", password="Serenitas1", host="debian") -nameToBasketID <- function(name, date){ +nameToBasketID <- function(name, date=Sys.Date()){ sqlstr <- "SELECT * from nametobasketid('%s', '%s')" r <- dbGetQuery(mlpdbCon, sprintf(sqlstr, name, date)) return(as.integer(r)) @@ -17,19 +17,25 @@ load.index <- function(name, date=Sys.Date(), tenor="5yr"){ loss=r$cumulativeloss/100, recovery=0.4, name=name)) } -cdslist <- function(indexname, date){ +cdslist <- function(indexname, date=Sys.Date()){ basketid <- nameToBasketID(indexname, date) sqlstr <- "select * from CDS_Issuers where index_list @> '{%s}'" return( dbGetQuery(mlpdbCon, sprintf(sqlstr, basketid))) } arr.convert <- function(arr){ - return(as.numeric(unlist(lapply(arr, function(x)strsplit(substr(x,2,nchar(x)-1),",",fixed=TRUE))))) + arr <- unlist(lapply(arr, function(x)strsplit(substr(x,2,nchar(x)-1),",",fixed=TRUE))) + arr[arr=="NULL"] <- NA + arr <- matrix(as.numeric(arr), nrow=length(arr)/8, ncol=8, byrow=T) + colnames(arr) <- c("6m", "1y", "2y", "3y", "4y", "5y", "7y", "10y") + return(arr) } -get.indexquotes <- function(indexname, date){ +get.indexquotes <- function(indexname, date=Sys.Date()){ r <- dbGetQuery(mlpdbCon, sprintf("select * from curve_quotes('%s', '%s')", indexname, date)) - r <- data.frame(r[,1], matrix(arr.convert(r[,2]), nrow(r), 8, byrow=T)) - colnames(r) <- c("ticker", "6m", "1y", "2y", "3y", "4y", "5y", "7y", "10y") - return( r ) + quotes <- list(tickers=r[,1], + spread_curve = arr.convert(r$spread_curve), + upfront_curve =arr.convert(r$upfront_curve), + recovery_curve = arr.convert(r$recovery_curve)) + return( quotes ) } |
