forked from xuruilong100/QuantLibPythonExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcms.py
More file actions
358 lines (314 loc) · 13 KB
/
Copy pathcms.py
File metadata and controls
358 lines (314 loc) · 13 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import unittest
import numpy as np
from QuantLib import *
from utilities import *
class CommonVars(object):
def __init__(self):
self.calendar = TARGET()
self.referenceDate = self.calendar.adjust(knownGoodDefault)
Settings.instance().evaluationDate = self.referenceDate
self.termStructure = RelinkableYieldTermStructureHandle()
self.termStructure.linkTo(
flatRate(self.referenceDate, 0.05, Actual365Fixed()))
self.backup = SavedSettings()
atmOptionTenors = [
Period(1, Months), Period(6, Months), Period(1, Years),
Period(5, Years), Period(10, Years), Period(30, Years)]
atmSwapTenors = [
Period(1, Years), Period(5, Years), Period(10, Years), Period(30, Years)]
m = Matrix(len(atmOptionTenors), len(atmSwapTenors))
m[0][0] = 0.1300
m[0][1] = 0.1560
m[0][2] = 0.1390
m[0][3] = 0.1220
m[1][0] = 0.1440
m[1][1] = 0.1580
m[1][2] = 0.1460
m[1][3] = 0.1260
m[2][0] = 0.1600
m[2][1] = 0.1590
m[2][2] = 0.1470
m[2][3] = 0.1290
m[3][0] = 0.1640
m[3][1] = 0.1470
m[3][2] = 0.1370
m[3][3] = 0.1220
m[4][0] = 0.1400
m[4][1] = 0.1300
m[4][2] = 0.1250
m[4][3] = 0.1100
m[5][0] = 0.1130
m[5][1] = 0.1090
m[5][2] = 0.1070
m[5][3] = 0.0930
self.atmVol = SwaptionVolatilityStructureHandle(
SwaptionVolatilityMatrix(
self.calendar,
Following,
atmOptionTenors,
atmSwapTenors,
m, Actual365Fixed()))
optionTenors = [Period(1, Years), Period(10, Years), Period(30, Years)]
swapTenors = [Period(2, Years), Period(10, Years), Period(30, Years)]
strikeSpreads = [-0.020, -0.005, 0.000, 0.005, 0.020]
nRows = len(optionTenors) * len(swapTenors)
nCols = len(strikeSpreads)
volSpreadsMatrix = Matrix(nRows, nCols)
volSpreadsMatrix[0][0] = 0.0599
volSpreadsMatrix[0][1] = 0.0049
volSpreadsMatrix[0][2] = 0.0000
volSpreadsMatrix[0][3] = -0.0001
volSpreadsMatrix[0][4] = 0.0127
volSpreadsMatrix[1][0] = 0.0729
volSpreadsMatrix[1][1] = 0.0086
volSpreadsMatrix[1][2] = 0.0000
volSpreadsMatrix[1][3] = -0.0024
volSpreadsMatrix[1][4] = 0.0098
volSpreadsMatrix[2][0] = 0.0738
volSpreadsMatrix[2][1] = 0.0102
volSpreadsMatrix[2][2] = 0.0000
volSpreadsMatrix[2][3] = -0.0039
volSpreadsMatrix[2][4] = 0.0065
volSpreadsMatrix[3][0] = 0.0465
volSpreadsMatrix[3][1] = 0.0063
volSpreadsMatrix[3][2] = 0.0000
volSpreadsMatrix[3][3] = -0.0032
volSpreadsMatrix[3][4] = -0.0010
volSpreadsMatrix[4][0] = 0.0558
volSpreadsMatrix[4][1] = 0.0084
volSpreadsMatrix[4][2] = 0.0000
volSpreadsMatrix[4][3] = -0.0050
volSpreadsMatrix[4][4] = -0.0057
volSpreadsMatrix[5][0] = 0.0576
volSpreadsMatrix[5][1] = 0.0083
volSpreadsMatrix[5][2] = 0.0000
volSpreadsMatrix[5][3] = -0.0043
volSpreadsMatrix[5][4] = -0.0014
volSpreadsMatrix[6][0] = 0.0437
volSpreadsMatrix[6][1] = 0.0059
volSpreadsMatrix[6][2] = 0.0000
volSpreadsMatrix[6][3] = -0.0030
volSpreadsMatrix[6][4] = -0.0006
volSpreadsMatrix[7][0] = 0.0533
volSpreadsMatrix[7][1] = 0.0078
volSpreadsMatrix[7][2] = 0.0000
volSpreadsMatrix[7][3] = -0.0045
volSpreadsMatrix[7][4] = -0.0046
volSpreadsMatrix[8][0] = 0.0545
volSpreadsMatrix[8][1] = 0.0079
volSpreadsMatrix[8][2] = 0.0000
volSpreadsMatrix[8][3] = -0.0042
volSpreadsMatrix[8][4] = -0.0020
volSpreads = QuoteHandleVectorVector(nRows)
for i in range(nRows):
temp = QuoteHandleVector()
for j in range(nCols):
temp.append(
QuoteHandle(
SimpleQuote(volSpreadsMatrix[i][j])))
volSpreads[i] = temp
self.iborIndex = Euribor6M(self.termStructure)
swapIndexBase = EuriborSwapIsdaFixA(Period(10, Years), self.termStructure)
shortSwapIndexBase = EuriborSwapIsdaFixA(Period(2, Years), self.termStructure)
vegaWeightedSmileFit = false
self.SabrVolCube2 = SwaptionVolatilityStructureHandle(
SwaptionVolCube2(
self.atmVol,
optionTenors,
swapTenors,
strikeSpreads,
volSpreads,
swapIndexBase,
shortSwapIndexBase,
vegaWeightedSmileFit))
self.SabrVolCube2.currentLink().enableExtrapolation()
guess = QuoteHandleVectorVector(nRows)
for i in range(nRows):
temp = QuoteHandleVector()
temp.append(QuoteHandle(SimpleQuote(0.2)))
temp.append(QuoteHandle(SimpleQuote(0.5)))
temp.append(QuoteHandle(SimpleQuote(0.4)))
temp.append(QuoteHandle(SimpleQuote(0.0)))
guess[i] = temp
isParameterFixed = BoolVector(4, false)
isParameterFixed[1] = true
isAtmCalibrated = false
self.SabrVolCube1 = SwaptionVolatilityStructureHandle(
SwaptionVolCube1(
self.atmVol,
optionTenors,
swapTenors,
strikeSpreads,
volSpreads,
swapIndexBase,
shortSwapIndexBase,
vegaWeightedSmileFit,
guess,
isParameterFixed,
isAtmCalibrated))
self.SabrVolCube1.currentLink().enableExtrapolation()
self.yieldCurveModels = [
GFunctionFactory.Standard,
GFunctionFactory.ExactYield,
GFunctionFactory.ParallelShifts,
GFunctionFactory.NonParallelShifts,
GFunctionFactory.NonParallelShifts]
zeroMeanRev = QuoteHandle(SimpleQuote(0.0))
self.numericalPricers = []
self.analyticPricers = []
for j in range(len(self.yieldCurveModels)):
if j < len(self.yieldCurveModels) - 1:
self.numericalPricers.append(
NumericHaganPricer(
self.atmVol, self.yieldCurveModels[j], zeroMeanRev))
else:
self.numericalPricers.append(
LinearTsrPricer(self.atmVol, zeroMeanRev))
self.analyticPricers.append(
AnalyticHaganPricer(
self.atmVol, self.yieldCurveModels[j], zeroMeanRev))
class CmsTest(unittest.TestCase):
def testFairRate(self):
TEST_MESSAGE(
"Testing Hagan-pricer flat-vol equivalence for coupons...")
vars = CommonVars()
swapIndex = SwapIndex(
"EuriborSwapIsdaFixA",
Period(10, Years),
vars.iborIndex.fixingDays(),
vars.iborIndex.currency(),
vars.iborIndex.fixingCalendar(),
Period(1, Years),
Unadjusted,
vars.iborIndex.dayCounter(),
vars.iborIndex)
startDate = vars.termStructure.referenceDate() + Period(20, Years)
paymentDate = startDate + Period(1, Years)
endDate = paymentDate
nominal = 1.0
infiniteCap = NullReal()
infiniteFloor = NullReal()
gearing = 1.0
spread = 0.0
coupon = CappedFlooredCmsCoupon(
paymentDate, nominal,
startDate, endDate,
swapIndex.fixingDays(), swapIndex,
gearing, spread,
infiniteCap, infiniteFloor,
startDate, endDate,
vars.iborIndex.dayCounter())
for j in range(len(vars.yieldCurveModels)):
vars.numericalPricers[j].setSwaptionVolatility(vars.atmVol)
coupon.setPricer(vars.numericalPricers[j])
rate0 = coupon.rate()
vars.analyticPricers[j].setSwaptionVolatility(vars.atmVol)
coupon.setPricer(vars.analyticPricers[j])
rate1 = coupon.rate()
difference = abs(rate1 - rate0)
tol = 2.0e-4
linearTsr = j == len(vars.yieldCurveModels) - 1
self.assertFalse(difference > tol)
def testParity(self):
TEST_MESSAGE(
"Testing put-call parity for capped-floored CMS coupons...")
vars = CommonVars()
swaptionVols = [
vars.atmVol, vars.SabrVolCube1, vars.SabrVolCube2]
swapIndex = EuriborSwapIsdaFixA(
Period(10, Years), vars.iborIndex.forwardingTermStructure())
startDate = vars.termStructure.referenceDate() + Period(20, Years)
paymentDate = startDate + Period(1, Years)
endDate = paymentDate
nominal = 1.0
infiniteCap = NullReal()
infiniteFloor = NullReal()
gearing = 1.0
spread = 0.0
discount = vars.termStructure.discount(paymentDate)
swaplet = CappedFlooredCmsCoupon(
paymentDate, nominal,
startDate, endDate,
swapIndex.fixingDays(),
swapIndex,
gearing, spread,
infiniteCap, infiniteFloor,
startDate, endDate,
vars.iborIndex.dayCounter())
for strike in np.arange(.02, .12, 0.05):
caplet = CappedFlooredCmsCoupon(
paymentDate, nominal,
startDate, endDate,
swapIndex.fixingDays(),
swapIndex,
gearing, spread,
strike, infiniteFloor,
startDate, endDate,
vars.iborIndex.dayCounter())
floorlet = CappedFlooredCmsCoupon(
paymentDate, nominal,
startDate, endDate,
swapIndex.fixingDays(),
swapIndex,
gearing, spread,
infiniteCap, strike,
startDate, endDate,
vars.iborIndex.dayCounter())
for swaptionVol in swaptionVols:
for j in range(len(vars.yieldCurveModels)):
vars.numericalPricers[j].setSwaptionVolatility(swaptionVol)
vars.analyticPricers[j].setSwaptionVolatility(swaptionVol)
pricers = []
pricers.append(vars.numericalPricers[j])
pricers.append(vars.analyticPricers[j])
for k in range(len(pricers)):
swaplet.setPricer(pricers[k])
caplet.setPricer(pricers[k])
floorlet.setPricer(pricers[k])
swapletPrice = swaplet.price(vars.termStructure) + \
nominal * swaplet.accrualPeriod() * strike * discount
capletPrice = caplet.price(vars.termStructure)
floorletPrice = floorlet.price(vars.termStructure)
difference = abs(capletPrice + floorletPrice - swapletPrice)
tol = 2.0e-5
linearTsr = k == 0 and j == len(vars.yieldCurveModels) - 1
if linearTsr:
tol = 1.0e-7
self.assertFalse(difference > tol)
def testCmsSwap(self):
TEST_MESSAGE(
"Testing Hagan-pricer flat-vol equivalence for swaps...")
vars = CommonVars()
swapIndex = SwapIndex(
"EuriborSwapIsdaFixA",
Period(10, Years),
vars.iborIndex.fixingDays(),
vars.iborIndex.currency(),
vars.iborIndex.fixingCalendar(),
Period(1, Years),
Unadjusted,
vars.iborIndex.dayCounter(),
vars.iborIndex)
spread = 0.0
swapLengths = [1, 5, 6, 10]
n = len(swapLengths)
cms = []
for i in range(n):
cms.append(
MakeCms(
Period(swapLengths[i], Years),
swapIndex,
vars.iborIndex, spread,
Period(10, Days)).makeCms())
for j in range(len(vars.yieldCurveModels)):
vars.numericalPricers[j].setSwaptionVolatility(vars.atmVol)
vars.analyticPricers[j].setSwaptionVolatility(vars.atmVol)
for sl in range(n):
setCouponPricer(cms[sl].leg(0), vars.numericalPricers[j])
priceNum = cms[sl].NPV()
setCouponPricer(cms[sl].leg(0), vars.analyticPricers[j])
priceAn = cms[sl].NPV()
difference = abs(priceNum - priceAn)
tol = 2.0e-4
linearTsr = j == len(vars.yieldCurveModels) - 1
self.assertFalse(difference > tol)