aboutsummaryrefslogtreecommitdiffstats
path: root/R/tests/test_yieldcurve.R
blob: f5de7a9a4da4d8df184600dc09f5fb1acbe7ea60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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))