diff options
| -rw-r--r-- | R/tests/test.cds_functions.R | 6 | ||||
| -rw-r--r-- | R/tests/test_yieldcurve.R | 146 |
2 files changed, 54 insertions, 98 deletions
diff --git a/R/tests/test.cds_functions.R b/R/tests/test.cds_functions.R index c30651ba..1d17ae21 100644 --- a/R/tests/test.cds_functions.R +++ b/R/tests/test.cds_functions.R @@ -1,9 +1,11 @@ library("RUnit")
-root = "//WDSENTINEL/share/CorpCDOs/R"
+root <- "/home/share/CorpCDOs/code/R"
source(file.path(root, "yieldCurve.R"))
-source(file.path(root, "cds_functions.R"))
+source(file.path(root, "cds_functions_generic.R"), chdir = T)
#unit tests
+trade_date <- as.Date("2012-09-28")
+exportYC(trade_date)
cs <- couponSchedule(as.Date("2012-09-28"), as.Date("2016-04-22"),"Q", "FLOAT", 0.075,0.0703)
#test that the curve version gives the same result as the flat version for flat curves
h <- rep(0.05, nrow(cs))
diff --git a/R/tests/test_yieldcurve.R b/R/tests/test_yieldcurve.R index 97a4e95c..e96e5596 100644 --- a/R/tests/test_yieldcurve.R +++ b/R/tests/test_yieldcurve.R @@ -1,99 +1,53 @@ -library(RQuantLib) +root.dir <- "/home/share/CorpCDOs" +source(file.path(root.dir, "code", "R", "yieldcurve.R")) +bbg.discounts.usd <- c(0.99848606, 0.99548212, 0.99167201, 0.98772518, + 0.98629694, 0.98509013, 0.98389804, 0.98268094, + 0.97993369, 0.97709955, 0.97430462, 0.97145583, + 0.96827211, 0.9651054, 0.96194904, 0.95869946, + 0.95499459, 0.95125679, 0.94753361, 0.94382501) -data.dir <- "." +tradedate <- as.Date("2016-06-23") +sched.params <- list(effectiveDate = tradedate, + maturityDate = as.Date("2021-06-20"), + period = "Quarterly", + calendar = "WeekendsOnly", + businessDayConvention = "Following", + terminationDateConvention = "Following", + dateGeneration = "CDS") -getMarkitIRData <- function(date=Sys.Date(), currency="USD") { - ## downloads the latest available interest rates data from Markit - ## before date and returns the parsed file into a list - require(xml2) - i <- 0 - while( TRUE ) { - lastdate <- format(date-i, "%Y%m%d") - filename <- paste("InterestRates", currency, lastdate, sep="_") - filename.ext <- paste0(filename, ".xml") - if( filename.ext %in% dir(file.path(data.dir, "Yield Curves"))){ - return( as_list(read_xml(file.path(data.dir, "Yield Curves", filename.ext))) ) - }else{ - temp <- tempfile(tmpdir = file.path(data.dir, "Yield Curves")) - download.file(paste0("http://www.markit.com/news/", filename, ".zip"), temp, quiet=T) - con <- file(temp, "r") - firstline <- readLines(con, 1, warn=FALSE) - ## Markit returns a plain text file if there is no data. - if(firstline == "Interest Rates not available, please check date entered") { - i <- i + 1 - close(con) - unlink(temp) - } else { - cat("downloaded data for:", lastdate,"\n") - close(con) - ## we unzip it - unzip(temp, exdir = file.path(data.dir, "Yield Curves")) - unlink(temp) - return( as_list(read_xml(file.path(data.dir, "Yield Curves", filename.ext))) ) - } - } - } - return( as_list(read_xml(paste(filename,".xml", sep=""))) ) -} +sched <- Schedule(sched.params) +ir_data <- getMarkitIRData(tradedate) +quotes <- buildMarkitYC(ir_data) +settings <- Settings$new() +settings$EvaluationDate <- tradedate +setCalendarContext(calendar="WeekendsOnly", fixingDays=2, + settleDate=tradedate) +legparams <- list(fixFreq="Semiannual", + floatFreq="Quarterly", + dayCounter="Thirty360") +calendar <- Calendar$new("WeekendsOnly") +dc <- DayCounter$new("Actual365Fixed") +yc <- YieldTermStructure$new("discount", "loglinear", 0, calendar, dc, quotes, legparams) +test_that("matches bloomberg USD discounts", + expect_equal(yc$discount(sched[-1]), bbg.discounts.usd)) -buildMarkitYC <- function(MarkitData, currency=c("USD", "EUR")){ - deposits <- list() - swaps <- list() - keys <- unlist(lapply(MarkitData$deposits[5:length(MarkitData$deposits)], - function(x)paste0("d",tolower(x$tenor))), use.names=FALSE) - for(i in seq_along(keys)){ - deposits[[keys[i]]] <- as.numeric(MarkitData$deposits[i+4]$curvepoint$parrate) - } - if(length(MarkitData$swaps)>8){## swaps quotes probably missing - keys <- unlist(lapply(MarkitData$swaps[8:length(MarkitData$swaps)], - function(x) paste0("s", tolower(x$tenor))), use.names=FALSE) - for(i in seq_along(keys)){ - swaps[[keys[i]]] <- as.numeric(MarkitData$swaps[i+7]$curvepoint$parrate) - } - } - tsQuotes <- c(deposits, swaps) - return( tsQuotes ) -} - -YC <- function(tradedate=Sys.Date(), currency=c("USD", "EUR"){ - ## export the Yield Curve into the environment - MarkitData <- getMarkitIRData(tradedate-1, currency) - evaldate <- as.Date(MarkitData$effectiveasof[[1]]) - setEvaluationDate(tradedate) - if(evaldate!=tradedate){ - stop("wrong date") - } - sDate <- as.Date(MarkitData$deposits$spotdate[[1]]) - setCalendarContext(calendar="WeekendsOnly", fixingDays=2, - settleDate=sDate) - legparams <- switch(currency, - USD = list(fixFreq="Semiannual", - floatFreq="Quarterly", - dayCounter="Thirty360"), - EUR = list(fixFreq="Annual", - floatFreq="Semiannual", - dayCounter="Thirty360")) - params <- list(tradeDate=tradedate, - settleDate=sDate, - interpWhat="discount", - interpHow="loglinear") - quotes <- buildMarkitYC(MarkitData, currency) - return( list(params=params, tsQuotes=quotes, legparams=legparams) ) -} - -dates <- seq(as.Date("2013-01-01"), as.Date("2016-05-16"), by=1) -for(curr in c("USD", "EUR")) { - for(i in 1:length(dates)) { - day <- dates[i] - cal <- switch(curr, - USD = "UnitedStates/GovernmentBond", - EUR = "TARGET") - calibration.date <- advance(calendar=cal, day, -1, 0) - if(isBusinessDay(calendar=cal, day)){ - yc <- YC(calibration.date, currency=curr) - print(day) - test <- DiscountCurve(c(yc$params, list(dt=0.25)), yc$tsQuotes, 1, yc$legparams) - cat(day, test$forwards, "\n") - } - } -} +bbg.discounts.eur <- c(1.00059368, 1.0009714, 1.00103266, 1.00083284, 1.00125876, + 1.00202315, 1.00280512, 1.00358771, 1.0040384, 1.00431052, + 1.00458875, 1.00486707, 1.00494726, 1.00492662, 1.0049053, 1.00488466, + 1.00454702, 1.00406132, 1.00357584, 1.0030906, 1.00218155) +tradedate <- as.Date("2016-10-18") +sched.params$maturityDate <- as.Date("2021-12-20") +sched.params$effectiveDate <- tradedate +sched <- Schedule(sched.params) +ir_data <- getMarkitIRData(tradedate, "EUR") +quotes <- buildMarkitYC(ir_data) +settings <- Settings$new() +settings$EvaluationDate <- tradedate +setCalendarContext(calendar="WeekendsOnly", fixingDays=2, + settleDate=tradedate) +legparams <- list(fixFreq="Annual", + floatFreq="Semiannual", + dayCounter="Thirty360") +yc <- YieldTermStructure$new("discount", "loglinear", 0, calendar, dc, quotes, legparams) +test_that("matches bloomberg EUR discounts", + expect_equal(yc$discount(sched[-1]), bbg.discounts.eur)) |
