/
couponpricer.cpp
412 lines (361 loc) · 17.5 KB
/
couponpricer.cpp
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2007 Giorgio Facchinetti
Copyright (C) 2007 Cristina Duminuco
Copyright (C) 2011 Ferdinando Ametrano
Copyright (C) 2015 Peter Caspers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ql/cashflows/couponpricer.hpp>
#include <ql/cashflows/capflooredcoupon.hpp>
#include <ql/cashflows/digitalcoupon.hpp>
#include <ql/cashflows/digitalcmscoupon.hpp>
#include <ql/cashflows/digitaliborcoupon.hpp>
#include <ql/cashflows/rangeaccrual.hpp>
#include <ql/experimental/coupons/subperiodcoupons.hpp> /* internal */
#include <ql/experimental/coupons/cmsspreadcoupon.hpp> /* internal */
#include <ql/experimental/coupons/digitalcmsspreadcoupon.hpp> /* internal */
#include <ql/pricingengines/blackformula.hpp>
#include <ql/termstructures/yieldtermstructure.hpp>
namespace QuantLib {
//===========================================================================//
// BlackIborCouponPricer //
//===========================================================================//
void BlackIborCouponPricer::initialize(const FloatingRateCoupon& coupon) {
gearing_ = coupon.gearing();
spread_ = coupon.spread();
accrualPeriod_ = coupon.accrualPeriod();
QL_REQUIRE(accrualPeriod_ != 0.0, "null accrual period");
index_ = ext::dynamic_pointer_cast<IborIndex>(coupon.index());
if (!index_) {
// check if the coupon was right
const auto* c = dynamic_cast<const IborCoupon*>(&coupon);
QL_REQUIRE(c, "IborCoupon required");
// coupon was right, index is not
QL_FAIL("IborIndex required");
}
Handle<YieldTermStructure> rateCurve =
index_->forwardingTermStructure();
Date paymentDate = coupon.date();
if (paymentDate > rateCurve->referenceDate())
discount_ = rateCurve->discount(paymentDate);
else
discount_ = 1.0;
spreadLegValue_ = spread_ * accrualPeriod_ * discount_;
coupon_ = &coupon;
}
Real BlackIborCouponPricer::optionletPrice(Option::Type optionType,
Real effStrike) const {
Date fixingDate = coupon_->fixingDate();
if (fixingDate <= Settings::instance().evaluationDate()) {
// the amount is determined
Real a, b;
if (optionType==Option::Call) {
a = coupon_->indexFixing();
b = effStrike;
} else {
a = effStrike;
b = coupon_->indexFixing();
}
return std::max(a - b, 0.0)* accrualPeriod_*discount_;
} else {
// not yet determined, use Black model
QL_REQUIRE(!capletVolatility().empty(),
"missing optionlet volatility");
Real stdDev =
std::sqrt(capletVolatility()->blackVariance(fixingDate,
effStrike));
Real shift = capletVolatility()->displacement();
bool shiftedLn =
capletVolatility()->volatilityType() == ShiftedLognormal;
Rate fixing =
shiftedLn
? blackFormula(optionType, effStrike, adjustedFixing(),
stdDev, 1.0, shift)
: bachelierBlackFormula(optionType, effStrike,
adjustedFixing(), stdDev, 1.0);
return fixing * accrualPeriod_ * discount_;
}
}
Rate BlackIborCouponPricer::adjustedFixing(Rate fixing) const {
if (fixing == Null<Rate>())
fixing = coupon_->indexFixing();
// if the pay date is equal to the index estimation end date
// there is no convexity; in all other cases in principle an
// adjustment has to be applied, but the Black76 method only
// applies the standard in arrears adjustment; the bivariate
// lognormal method is more accurate in this regard.
if ((!coupon_->isInArrears() && timingAdjustment_ == Black76))
return fixing;
Date d1 = coupon_->fixingDate();
Date d2 = index_->valueDate(d1);
Date d3 = index_->maturityDate(d2);
if (coupon_->date() == d3)
return fixing;
QL_REQUIRE(!capletVolatility().empty(),
"missing optionlet volatility");
Date referenceDate = capletVolatility()->referenceDate();
// no variance has accumulated, so the convexity is zero
if (d1 <= referenceDate)
return fixing;
Time tau = index_->dayCounter().yearFraction(d2, d3);
Real variance = capletVolatility()->blackVariance(d1, fixing);
Real shift = capletVolatility()->displacement();
bool shiftedLn =
capletVolatility()->volatilityType() == ShiftedLognormal;
Spread adjustment = shiftedLn
? (fixing + shift) * (fixing + shift) *
variance * tau / (1.0 + fixing * tau)
: variance * tau / (1.0 + fixing * tau);
if (timingAdjustment_ == BivariateLognormal) {
QL_REQUIRE(!correlation_.empty(), "no correlation given");
Date d4 = coupon_->date();
Date d5 = d4 >= d3 ? d3 : d2;
Time tau2 = index_->dayCounter().yearFraction(d5, d4);
if (d4 >= d3)
adjustment = 0.0;
// if d4 < d2 (payment before index start) we just apply the
// Black76 in arrears adjustment
if (tau2 > 0.0) {
Real fixing2 =
(index_->forwardingTermStructure()->discount(d5) /
index_->forwardingTermStructure()->discount(d4) -
1.0) /
tau2;
adjustment -= shiftedLn
? correlation_->value() * tau2 * variance *
(fixing + shift) * (fixing2 + shift) /
(1.0 + fixing2 * tau2)
: correlation_->value() * tau2 * variance /
(1.0 + fixing2 * tau2);
}
}
return fixing + adjustment;
}
//===========================================================================//
// CouponSelectorToSetPricer //
//===========================================================================//
namespace {
class PricerSetter : public AcyclicVisitor,
public Visitor<CashFlow>,
public Visitor<Coupon>,
public Visitor<FloatingRateCoupon>,
public Visitor<CappedFlooredCoupon>,
public Visitor<IborCoupon>,
public Visitor<CmsCoupon>,
public Visitor<CmsSpreadCoupon>,
public Visitor<CappedFlooredIborCoupon>,
public Visitor<CappedFlooredCmsCoupon>,
public Visitor<CappedFlooredCmsSpreadCoupon>,
public Visitor<DigitalIborCoupon>,
public Visitor<DigitalCmsCoupon>,
public Visitor<DigitalCmsSpreadCoupon>,
public Visitor<RangeAccrualFloatersCoupon>,
public Visitor<SubPeriodsCoupon> {
private:
ext::shared_ptr<FloatingRateCouponPricer> pricer_;
public:
explicit PricerSetter(
const ext::shared_ptr<FloatingRateCouponPricer>& pricer)
: pricer_(pricer) {}
void visit(CashFlow& c) override;
void visit(Coupon& c) override;
void visit(FloatingRateCoupon& c) override;
void visit(CappedFlooredCoupon& c) override;
void visit(IborCoupon& c) override;
void visit(CappedFlooredIborCoupon& c) override;
void visit(DigitalIborCoupon& c) override;
void visit(CmsCoupon& c) override;
void visit(CmsSpreadCoupon& c) override;
void visit(CappedFlooredCmsCoupon& c) override;
void visit(CappedFlooredCmsSpreadCoupon& c) override;
void visit(DigitalCmsCoupon& c) override;
void visit(DigitalCmsSpreadCoupon& c) override;
void visit(RangeAccrualFloatersCoupon& c) override;
void visit(SubPeriodsCoupon& c) override;
};
void PricerSetter::visit(CashFlow&) {
// nothing to do
}
void PricerSetter::visit(Coupon&) {
// nothing to do
}
void PricerSetter::visit(FloatingRateCoupon& c) {
c.setPricer(pricer_);
}
void PricerSetter::visit(CappedFlooredCoupon& c) {
// we might end up here because a CappedFlooredCoupon
// was directly constructed; we should then check
// the underlying for consistency with the pricer
if (ext::dynamic_pointer_cast<IborCoupon>(c.underlying()) != nullptr) {
QL_REQUIRE(ext::dynamic_pointer_cast<IborCouponPricer>(pricer_),
"pricer not compatible with Ibor Coupon");
} else if (ext::dynamic_pointer_cast<CmsCoupon>(c.underlying()) != nullptr) {
QL_REQUIRE(ext::dynamic_pointer_cast<CmsCouponPricer>(pricer_),
"pricer not compatible with CMS Coupon");
} else if (ext::dynamic_pointer_cast<CmsSpreadCoupon>(c.underlying()) != nullptr) {
QL_REQUIRE(ext::dynamic_pointer_cast<CmsSpreadCouponPricer>(pricer_),
"pricer not compatible with CMS spread Coupon");
}
c.setPricer(pricer_);
}
void PricerSetter::visit(IborCoupon& c) {
const ext::shared_ptr<IborCouponPricer> iborCouponPricer =
ext::dynamic_pointer_cast<IborCouponPricer>(pricer_);
QL_REQUIRE(iborCouponPricer,
"pricer not compatible with Ibor coupon");
c.setPricer(iborCouponPricer);
}
void PricerSetter::visit(DigitalIborCoupon& c) {
const ext::shared_ptr<IborCouponPricer> iborCouponPricer =
ext::dynamic_pointer_cast<IborCouponPricer>(pricer_);
QL_REQUIRE(iborCouponPricer,
"pricer not compatible with Ibor coupon");
c.setPricer(iborCouponPricer);
}
void PricerSetter::visit(CappedFlooredIborCoupon& c) {
const ext::shared_ptr<IborCouponPricer> iborCouponPricer =
ext::dynamic_pointer_cast<IborCouponPricer>(pricer_);
QL_REQUIRE(iborCouponPricer,
"pricer not compatible with Ibor coupon");
c.setPricer(iborCouponPricer);
}
void PricerSetter::visit(CmsCoupon& c) {
const ext::shared_ptr<CmsCouponPricer> cmsCouponPricer =
ext::dynamic_pointer_cast<CmsCouponPricer>(pricer_);
QL_REQUIRE(cmsCouponPricer,
"pricer not compatible with CMS coupon");
c.setPricer(cmsCouponPricer);
}
void PricerSetter::visit(CmsSpreadCoupon& c) {
const ext::shared_ptr<CmsSpreadCouponPricer> cmsSpreadCouponPricer =
ext::dynamic_pointer_cast<CmsSpreadCouponPricer>(pricer_);
QL_REQUIRE(cmsSpreadCouponPricer,
"pricer not compatible with CMS spread coupon");
c.setPricer(cmsSpreadCouponPricer);
}
void PricerSetter::visit(CappedFlooredCmsCoupon& c) {
const ext::shared_ptr<CmsCouponPricer> cmsCouponPricer =
ext::dynamic_pointer_cast<CmsCouponPricer>(pricer_);
QL_REQUIRE(cmsCouponPricer,
"pricer not compatible with CMS coupon");
c.setPricer(cmsCouponPricer);
}
void PricerSetter::visit(CappedFlooredCmsSpreadCoupon& c) {
const ext::shared_ptr<CmsSpreadCouponPricer> cmsSpreadCouponPricer =
ext::dynamic_pointer_cast<CmsSpreadCouponPricer>(pricer_);
QL_REQUIRE(cmsSpreadCouponPricer,
"pricer not compatible with CMS spread coupon");
c.setPricer(cmsSpreadCouponPricer);
}
void PricerSetter::visit(DigitalCmsCoupon& c) {
const ext::shared_ptr<CmsCouponPricer> cmsCouponPricer =
ext::dynamic_pointer_cast<CmsCouponPricer>(pricer_);
QL_REQUIRE(cmsCouponPricer,
"pricer not compatible with CMS coupon");
c.setPricer(cmsCouponPricer);
}
void PricerSetter::visit(DigitalCmsSpreadCoupon& c) {
const ext::shared_ptr<CmsSpreadCouponPricer> cmsSpreadCouponPricer =
ext::dynamic_pointer_cast<CmsSpreadCouponPricer>(pricer_);
QL_REQUIRE(cmsSpreadCouponPricer,
"pricer not compatible with CMS spread coupon");
c.setPricer(cmsSpreadCouponPricer);
}
void PricerSetter::visit(RangeAccrualFloatersCoupon& c) {
const ext::shared_ptr<RangeAccrualPricer> rangeAccrualPricer =
ext::dynamic_pointer_cast<RangeAccrualPricer>(pricer_);
QL_REQUIRE(rangeAccrualPricer,
"pricer not compatible with range-accrual coupon");
c.setPricer(rangeAccrualPricer);
}
void PricerSetter::visit(SubPeriodsCoupon& c) {
const ext::shared_ptr<SubPeriodsPricer> subPeriodsPricer =
ext::dynamic_pointer_cast<SubPeriodsPricer>(pricer_);
QL_REQUIRE(subPeriodsPricer,
"pricer not compatible with sub-period coupon");
c.setPricer(subPeriodsPricer);
}
void setCouponPricersFirstMatching(const Leg& leg,
const std::vector<ext::shared_ptr<FloatingRateCouponPricer> >& p) {
std::vector<PricerSetter> setter;
for (Size i = 0; i < p.size(); ++i) {
setter.push_back(PricerSetter(p[i]));
}
for (Size i = 0; i < leg.size(); ++i) {
Size j = 0;
do {
try {
leg[i]->accept(setter[j]);
j = p.size();
} catch (...) {
++j;
}
} while (j < p.size());
}
}
} // anonymous namespace
void setCouponPricer(const Leg& leg, const ext::shared_ptr<FloatingRateCouponPricer>& pricer) {
PricerSetter setter(pricer);
for (Size i = 0; i < leg.size(); ++i) {
leg[i]->accept(setter);
}
}
void setCouponPricers(
const Leg& leg,
const std::vector<ext::shared_ptr<FloatingRateCouponPricer> >&
pricers) {
Size nCashFlows = leg.size();
QL_REQUIRE(nCashFlows>0, "no cashflows");
Size nPricers = pricers.size();
QL_REQUIRE(nCashFlows >= nPricers,
"mismatch between leg size (" << nCashFlows <<
") and number of pricers (" << nPricers << ")");
for (Size i=0; i<nCashFlows; ++i) {
PricerSetter setter(i<nPricers ? pricers[i] : pricers[nPricers-1]);
leg[i]->accept(setter);
}
}
void setCouponPricers(
const Leg& leg,
const ext::shared_ptr<FloatingRateCouponPricer>& p1,
const ext::shared_ptr<FloatingRateCouponPricer>& p2) {
std::vector<ext::shared_ptr<FloatingRateCouponPricer> > p;
p.push_back(p1);
p.push_back(p2);
setCouponPricersFirstMatching(leg, p);
}
void setCouponPricers(
const Leg& leg,
const ext::shared_ptr<FloatingRateCouponPricer>& p1,
const ext::shared_ptr<FloatingRateCouponPricer>& p2,
const ext::shared_ptr<FloatingRateCouponPricer>& p3) {
std::vector<ext::shared_ptr<FloatingRateCouponPricer> > p;
p.push_back(p1);
p.push_back(p2);
p.push_back(p3);
setCouponPricersFirstMatching(leg, p);
}
void setCouponPricers(
const Leg& leg,
const ext::shared_ptr<FloatingRateCouponPricer>& p1,
const ext::shared_ptr<FloatingRateCouponPricer>& p2,
const ext::shared_ptr<FloatingRateCouponPricer>& p3,
const ext::shared_ptr<FloatingRateCouponPricer>& p4) {
std::vector<ext::shared_ptr<FloatingRateCouponPricer> > p;
p.push_back(p1);
p.push_back(p2);
p.push_back(p3);
p.push_back(p4);
setCouponPricersFirstMatching(leg, p);
}
}