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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
import unittest
from quantlib.time.api import (
Date,
Period,
Quarterly,
Schedule,
DateGeneration,
WeekendsOnly,
Following,
Unadjusted,
)
from quantlib.settings import Settings
from serenitas.analytics.yieldcurve import (
YC,
USDMarkitYieldCurve,
USDIsdaYieldCurve,
ql_to_jp,
)
import datetime
class TestYieldCurve(unittest.TestCase):
def assertListAlmostEqual(self, list1, list2, places=7):
self.assertEqual(len(list1), len(list2))
for a, b in zip(list1, list2):
self.assertAlmostEqual(a, b, places)
def test_bloomberg(self):
discounts = [
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,
]
settings = Settings()
settings.evaluation_date = Date(23, 6, 2016)
curve = YC()
term_date = Date(20, 6, 2021)
cds_schedule = Schedule.from_rule(
settings.evaluation_date,
term_date,
Period(Quarterly),
WeekendsOnly(),
Following,
Unadjusted,
DateGeneration.CDS2015,
)
curve_df = [curve.discount(d) for d in cds_schedule[1:-1]]
# last date is term_date+1
curve_df.append(curve.discount(term_date + 1))
self.assertListAlmostEqual(discounts, curve_df)
def test_new_yieldcurve(self):
evaluation_date = datetime.date(2021, 7, 14)
old_curve = YC(evaluation_date=evaluation_date)
new_curve = USDMarkitYieldCurve(evaluation_date)
for d, df in zip(old_curve.dates, old_curve.data):
self.assertEqual(df, new_curve._yc.discount(d))
def test_ois_yieldcurve(self):
evaluation_date = datetime.date(2023, 6, 20)
old_curve = YC(evaluation_date=evaluation_date, curve_type="OIS")
new_curve = USDIsdaYieldCurve(evaluation_date)
for d, df in zip(old_curve.dates, old_curve.data):
self.assertEqual(df, new_curve._yc.discount(d))
def test_sofr_curve(self):
yc = YC(evaluation_date=datetime.date(2022, 5, 9), curve_type="OIS")
jp_yc = ql_to_jp(yc)
self.assertEqual(hash(jp_yc), -7819183656360586799)
if __name__ == "__main__":
unittest.main()
|