context("Discount Factors") root.dir <- "~/projects" 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) 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 = "CDS2015") 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, tolerance = 1e-4)) 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))