-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
conundrumpricer.hpp
329 lines (281 loc) · 11.7 KB
/
conundrumpricer.hpp
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
/*
Copyright (C) 2006 Giorgio Facchinetti
Copyright (C) 2006 Mario Pucci
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. */
/*! \file conundrumpricer.hpp
\brief CMS-coupon pricer
*/
#ifndef quantlib_conundrum_pricer_hpp
#define quantlib_conundrum_pricer_hpp
#include <ql/cashflows/couponpricer.hpp>
#include <ql/instruments/payoffs.hpp>
namespace QuantLib {
class CmsCoupon;
class YieldTermStructure;
class Quote;
class VanillaOptionPricer {
public:
virtual ~VanillaOptionPricer() = default;
virtual Real operator()(Real strike,
Option::Type optionType,
Real deflator) const = 0;
};
class BlackVanillaOptionPricer : public VanillaOptionPricer {
public:
BlackVanillaOptionPricer(
Rate forwardValue,
Date expiryDate,
const Period& swapTenor,
const ext::shared_ptr<SwaptionVolatilityStructure>&
volatilityStructure);
Real operator()(Real strike, Option::Type optionType, Real deflator) const override;
private:
Rate forwardValue_;
Date expiryDate_;
Period swapTenor_;
ext::shared_ptr<SwaptionVolatilityStructure> volatilityStructure_;
ext::shared_ptr<SmileSection> smile_;
};
class GFunction {
public:
virtual ~GFunction() = default;
virtual Real operator()(Real x) = 0;
virtual Real firstDerivative(Real x) = 0;
virtual Real secondDerivative(Real x) = 0;
};
class GFunctionFactory {
public:
enum YieldCurveModel { Standard,
ExactYield,
ParallelShifts,
NonParallelShifts
};
static ext::shared_ptr<GFunction>
newGFunctionStandard(Size q,
Real delta,
Size swapLength);
static ext::shared_ptr<GFunction>
newGFunctionExactYield(const CmsCoupon& coupon);
static ext::shared_ptr<GFunction>
newGFunctionWithShifts(const CmsCoupon& coupon,
const Handle<Quote>& meanReversion);
private:
GFunctionFactory() = delete;
class GFunctionStandard : public GFunction {
public:
GFunctionStandard(Size q,
Real delta,
Size swapLength)
: q_(q), delta_(delta), swapLength_(swapLength) {}
Real operator()(Real x) override;
Real firstDerivative(Real x) override;
Real secondDerivative(Real x) override;
protected:
/* number of period per year */
const int q_;
/* fraction of a period between the swap start date and
the pay date */
Real delta_;
/* length of swap*/
Size swapLength_;
};
class GFunctionExactYield : public GFunction {
public:
GFunctionExactYield(const CmsCoupon& coupon);
Real operator()(Real x) override;
Real firstDerivative(Real x) override;
Real secondDerivative(Real x) override;
protected:
/* fraction of a period between the swap start date and
the pay date */
Real delta_;
/* accruals fraction*/
std::vector<Time> accruals_;
};
class GFunctionWithShifts : public GFunction {
Time swapStartTime_;
Time shapedPaymentTime_;
std::vector<Time> shapedSwapPaymentTimes_;
std::vector<Time> accruals_;
std::vector<Real> swapPaymentDiscounts_;
Real discountAtStart_, discountRatio_;
Real swapRateValue_;
Handle<Quote> meanReversion_;
Real calibratedShift_, tmpRs_;
const Real accuracy_;
//* function describing the non-parallel shape of the curve shift*/
Real shapeOfShift(Real s) const;
//* calibration of shift*/
Real calibrationOfShift(Real Rs);
Real functionZ(Real x);
Real derRs_derX(Real x);
Real derZ_derX(Real x);
Real der2Rs_derX2(Real x);
Real der2Z_derX2(Real x);
class ObjectiveFunction;
friend class ObjectiveFunction;
class ObjectiveFunction {
const GFunctionWithShifts& o_;
Real Rs_;
mutable Real derivative_;
public:
virtual ~ObjectiveFunction() = default;
ObjectiveFunction(const GFunctionWithShifts& o, const Real Rs) : o_(o), Rs_(Rs) {}
virtual Real operator()(const Real& x) const;
Real derivative(const Real& x) const;
void setSwapRateValue(Real x);
const GFunctionWithShifts& gFunctionWithShifts() const { return o_; }
};
ext::shared_ptr<ObjectiveFunction> objectiveFunction_;
public:
GFunctionWithShifts(const CmsCoupon& coupon, Handle<Quote> meanReversion);
Real operator()(Real x) override;
Real firstDerivative(Real x) override;
Real secondDerivative(Real x) override;
};
};
inline std::ostream& operator<<(std::ostream& out,
GFunctionFactory::YieldCurveModel type) {
switch (type) {
case GFunctionFactory::Standard:
return out << "Standard";
case GFunctionFactory::ExactYield:
return out << "ExactYield";
case GFunctionFactory::ParallelShifts:
return out << "ParallelShifts";
case GFunctionFactory::NonParallelShifts:
return out << "NonParallelShifts";
default:
QL_FAIL("unknown option type");
}
}
//! CMS-coupon pricer
/*! Base class for the pricing of a CMS coupon via static replication
as in Hagan's "Conundrums..." article
*/
class HaganPricer: public CmsCouponPricer, public MeanRevertingPricer {
public:
/* */
Real swapletPrice() const override = 0;
Rate swapletRate() const override;
Real capletPrice(Rate effectiveCap) const override;
Rate capletRate(Rate effectiveCap) const override;
Real floorletPrice(Rate effectiveFloor) const override;
Rate floorletRate(Rate effectiveFloor) const override;
/* */
Real meanReversion() const override;
void setMeanReversion(const Handle<Quote>& meanReversion) override {
unregisterWith(meanReversion_);
meanReversion_ = meanReversion;
registerWith(meanReversion_);
update();
};
protected:
HaganPricer(const Handle<SwaptionVolatilityStructure>& swaptionVol,
GFunctionFactory::YieldCurveModel modelOfYieldCurve,
Handle<Quote> meanReversion);
void initialize(const FloatingRateCoupon& coupon) override;
virtual Real optionletPrice(Option::Type optionType,
Real strike) const = 0;
ext::shared_ptr<YieldTermStructure> rateCurve_;
GFunctionFactory::YieldCurveModel modelOfYieldCurve_;
ext::shared_ptr<GFunction> gFunction_;
const CmsCoupon* coupon_;
Date paymentDate_, fixingDate_;
Rate swapRateValue_;
DiscountFactor discount_;
Real annuity_;
Real gearing_;
Spread spread_;
Real spreadLegValue_;
Rate cutoffForCaplet_, cutoffForFloorlet_;
Handle<Quote> meanReversion_;
Period swapTenor_;
ext::shared_ptr<VanillaOptionPricer> vanillaOptionPricer_;
};
//! CMS-coupon pricer
/*! Prices a cms coupon via static replication as in Hagan's
"Conundrums..." article via numerical integration based on
prices of vanilla swaptions
*/
class NumericHaganPricer : public HaganPricer {
public:
NumericHaganPricer(
const Handle<SwaptionVolatilityStructure>& swaptionVol,
GFunctionFactory::YieldCurveModel modelOfYieldCurve,
const Handle<Quote>& meanReversion,
Rate lowerLimit = 0.0,
Rate upperLimit = 1.0,
Real precision = 1.0e-6,
Real hardUpperLimit = QL_MAX_REAL);
Real upperLimit() const { return upperLimit_; }
Real stdDeviations() const { return stdDeviationsForUpperLimit_; }
// private:
class Function {
public:
typedef Real argument_type;
typedef Real result_type;
virtual ~Function() = default;
virtual Real operator()(Real x) const = 0;
};
class ConundrumIntegrand : public Function {
friend class NumericHaganPricer;
public:
ConundrumIntegrand(ext::shared_ptr<VanillaOptionPricer> o,
const ext::shared_ptr<YieldTermStructure>& rateCurve,
ext::shared_ptr<GFunction> gFunction,
Date fixingDate,
Date paymentDate,
Real annuity,
Real forwardValue,
Real strike,
Option::Type optionType);
Real operator()(Real x) const override;
protected:
Real functionF(Real x) const;
Real firstDerivativeOfF(Real x) const;
Real secondDerivativeOfF(Real x) const;
Real strike() const;
Real annuity() const;
Date fixingDate() const;
void setStrike(Real strike);
const ext::shared_ptr<VanillaOptionPricer> vanillaOptionPricer_;
const Real forwardValue_, annuity_;
const Date fixingDate_, paymentDate_;
Real strike_;
const Option::Type optionType_;
ext::shared_ptr<GFunction> gFunction_;
};
Real integrate(Real a,
Real b,
const ConundrumIntegrand& Integrand) const;
Real optionletPrice(Option::Type optionType, Rate strike) const override;
Real swapletPrice() const override;
Real resetUpperLimit(Real stdDeviationsForUpperLimit) const;
Real refineIntegration(Real integralValue, const ConundrumIntegrand& integrand) const;
mutable Real upperLimit_, stdDeviationsForUpperLimit_;
const Real lowerLimit_, requiredStdDeviations_, precision_, refiningIntegrationTolerance_;
const Real hardUpperLimit_;
};
//! CMS-coupon pricer
class AnalyticHaganPricer : public HaganPricer {
public:
AnalyticHaganPricer(
const Handle<SwaptionVolatilityStructure>& swaptionVol,
GFunctionFactory::YieldCurveModel modelOfYieldCurve,
const Handle<Quote>& meanReversion);
protected:
Real optionletPrice(Option::Type optionType, Real strike) const override;
Real swapletPrice() const override;
};
}
#endif