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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2014, 2016 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
<https://www.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 lineartsrpricer.cpp
*/
#include <ql/cashflows/cmscoupon.hpp>
#include <ql/cashflows/fixedratecoupon.hpp>
#include <ql/cashflows/iborcoupon.hpp>
#include <ql/cashflows/lineartsrpricer.hpp>
#include <ql/indexes/iborindex.hpp>
#include <ql/instruments/vanillaswap.hpp>
#include <ql/instruments/overnightindexedswap.hpp>
#include <ql/math/integrals/kronrodintegral.hpp>
#include <ql/math/solvers1d/brent.hpp>
#include <ql/pricingengines/blackformula.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/termstructures/volatility/atmsmilesection.hpp>
#include <ql/termstructures/yieldtermstructure.hpp>
#include <ql/time/schedule.hpp>
#include <utility>
namespace QuantLib {
class LinearTsrPricer::integrand_f {
const LinearTsrPricer* pricer;
public:
explicit integrand_f(const LinearTsrPricer* pricer) : pricer(pricer) {}
Real operator()(Real x) const {
return pricer->integrand(x);
}
};
const Real LinearTsrPricer::defaultLowerBound = 0.0001,
LinearTsrPricer::defaultUpperBound = 2.0000;
LinearTsrPricer::LinearTsrPricer(const Handle<SwaptionVolatilityStructure>& swaptionVol,
Handle<Quote> meanReversion,
Handle<YieldTermStructure> couponDiscountCurve,
const Settings& settings,
ext::shared_ptr<Integrator> integrator)
: CmsCouponPricer(swaptionVol), meanReversion_(std::move(meanReversion)),
couponDiscountCurve_(std::move(couponDiscountCurve)), settings_(settings),
volDayCounter_(swaptionVol->dayCounter()), integrator_(std::move(integrator)) {
if (!couponDiscountCurve_.empty())
registerWith(couponDiscountCurve_);
if (integrator_ == nullptr)
integrator_ =
ext::make_shared<GaussKronrodNonAdaptive>(1E-10, 5000, 1E-10);
}
Real LinearTsrPricer::GsrG(const Date &d) const {
Real yf = volDayCounter_.yearFraction(fixingDate_, d);
if (std::fabs(meanReversion_->value()) < 1.0E-4)
return yf;
else
return (1.0 - std::exp(-meanReversion_->value() * yf)) /
meanReversion_->value();
}
Real LinearTsrPricer::singularTerms(const Option::Type type,
const Real strike) const {
Real omega = (type == Option::Call ? 1.0 : -1.0);
Real s1 = std::max(omega * (swapRateValue_ - strike), 0.0) *
(a_ * swapRateValue_ + b_);
Real s2 = (a_ * strike + b_) *
smileSection_->optionPrice(strike, strike < swapRateValue_
? Option::Put
: Option::Call);
return s1 + s2;
}
Real LinearTsrPricer::integrand(const Real strike) const {
return 2.0 * a_ * smileSection_->optionPrice(
strike, strike < swapRateValue_ ? Option::Put
: Option::Call);
}
void LinearTsrPricer::initialize(const FloatingRateCoupon &coupon) {
coupon_ = dynamic_cast<const CmsCoupon *>(&coupon);
QL_REQUIRE(coupon_, "CMS coupon needed");
gearing_ = coupon_->gearing();
spread_ = coupon_->spread();
fixingDate_ = coupon_->fixingDate();
paymentDate_ = coupon_->date();
swapIndex_ = coupon_->swapIndex();
forwardCurve_ = swapIndex_->forwardingTermStructure();
if (swapIndex_->exogenousDiscount())
discountCurve_ = swapIndex_->discountingTermStructure();
else
discountCurve_ = forwardCurve_;
// if no coupon discount curve is given just use the discounting curve
// from the swap index. for rate calculation this curve cancels out in
// the computation, so e.g. the discounting swap engine will produce
// correct results, even if the couponDiscountCurve is not set here.
// only the price member function in this class will be dependent on the
// coupon discount curve.
today_ = QuantLib::Settings::instance().evaluationDate();
Real couponCurvePaymentDiscount;
if (!couponDiscountCurve_.empty() && paymentDate_ > couponDiscountCurve_->referenceDate()) {
couponCurvePaymentDiscount = couponDiscountCurve_->discount(paymentDate_);
} else {
couponCurvePaymentDiscount = 1.0;
}
if (paymentDate_ > discountCurve_->referenceDate()) {
discountCurvePaymentDiscount_ = discountCurve_->discount(paymentDate_);
} else {
discountCurvePaymentDiscount_ = 1.0;
}
couponDiscountRatio_ = couponCurvePaymentDiscount / discountCurvePaymentDiscount_;
spreadLegValue_ = spread_ * coupon_->accrualPeriod() * discountCurvePaymentDiscount_ *
couponDiscountRatio_;
if (fixingDate_ > today_) {
swapTenor_ = swapIndex_->tenor();
if (auto on = ext::dynamic_pointer_cast<OvernightIndexedSwapIndex>(swapIndex_)) {
swap_ = on->underlyingSwap(fixingDate_);
} else {
swap_ = swapIndex_->underlyingSwap(fixingDate_);
}
swapRateValue_ = swap_->fairRate();
annuity_ = 1.0E4 * std::fabs(swap_->fixedLegBPS());
Leg swapFixedLeg = swap_->fixedLeg();
ext::shared_ptr<SmileSection> sectionTmp =
swaptionVolatility()->smileSection(fixingDate_, swapTenor_);
adjustedLowerBound_ = settings_.lowerRateBound_;
adjustedUpperBound_ = settings_.upperRateBound_;
if(sectionTmp->volatilityType() == Normal) {
// adjust lower bound if it was not set explicitly
if(settings_.defaultBounds_)
adjustedLowerBound_ = std::min(adjustedLowerBound_, -adjustedUpperBound_);
} else {
// adjust bounds by section's shift
adjustedLowerBound_ -= sectionTmp->shift();
adjustedUpperBound_ -= sectionTmp->shift();
}
// if the section does not provide an atm level, we enhance it to
// have one, no need to exit with an exception ...
if (sectionTmp->atmLevel() == Null<Real>())
smileSection_ = ext::make_shared<AtmSmileSection>(
sectionTmp, swapRateValue_);
else
smileSection_ = sectionTmp;
// compute linear model's parameters
Real gx = 0.0, gy = 0.0;
for (const auto& i : swapFixedLeg) {
ext::shared_ptr<Coupon> c = ext::dynamic_pointer_cast<Coupon>(i);
Real yf = c->accrualPeriod();
Date d = c->date();
Real pv = yf * discountCurve_->discount(d);
gx += pv * GsrG(d);
gy += pv;
}
Real gamma = gx / gy;
Date lastd = swapFixedLeg.back()->date();
a_ = discountCurve_->discount(paymentDate_) *
(gamma - GsrG(paymentDate_)) /
(discountCurve_->discount(lastd) * GsrG(lastd) +
swapRateValue_ * gy * gamma);
b_ = discountCurve_->discount(paymentDate_) / gy -
a_ * swapRateValue_;
}
}
Real LinearTsrPricer::strikeFromVegaRatio(Real ratio,
Option::Type optionType,
Real referenceStrike) const {
Real a, b, min, max, k;
if (optionType == Option::Call) {
a = swapRateValue_;
min = referenceStrike;
// NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
b = max = k =
std::min(smileSection_->maxStrike(), adjustedUpperBound_);
} else {
// NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
a = min = k =
std::max(smileSection_->minStrike(), adjustedLowerBound_);
b = swapRateValue_;
max = referenceStrike;
}
VegaRatioHelper h(&*smileSection_,
smileSection_->vega(swapRateValue_) * ratio);
Brent solver;
try {
k = solver.solve(h, 1.0E-5, (a + b) / 2.0, a, b);
}
catch (...) {
// use default value set above
}
return std::min(std::max(k, min), max);
}
Real LinearTsrPricer::strikeFromPrice(Real price, Option::Type optionType,
Real referenceStrike) const {
Real a, b, min, max, k;
if (optionType == Option::Call) {
a = swapRateValue_;
min = referenceStrike;
// NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
b = max = k =
std::min(smileSection_->maxStrike(), adjustedUpperBound_);
} else {
// NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
a = min = k =
std::max(smileSection_->minStrike(), adjustedLowerBound_);
b = swapRateValue_;
max = referenceStrike;
}
PriceHelper h(&*smileSection_, optionType, price);
Brent solver;
try {
k = solver.solve(h, 1.0E-5, swapRateValue_, a, b);
}
catch (...) {
// use default value set above
}
return std::min(std::max(k, min), max);
}
Real LinearTsrPricer::optionletPrice(Option::Type optionType,
Real strike) const {
if (optionType == Option::Call && strike >= adjustedUpperBound_)
return 0.0;
if (optionType == Option::Put && strike <= adjustedLowerBound_)
return 0.0;
// determine lower or upper integration bound (depending on option type)
Real lower = strike, upper = strike;
switch (settings_.strategy_) {
case Settings::RateBound: {
if (optionType == Option::Call)
upper = adjustedUpperBound_;
else
lower = adjustedLowerBound_;
break;
}
case Settings::VegaRatio: {
// strikeFromVegaRatio ensures that returned strike is on the
// expected side of strike
Real bound =
strikeFromVegaRatio(settings_.vegaRatio_, optionType, strike);
if (optionType == Option::Call)
upper = std::min(bound, adjustedUpperBound_);
else
lower = std::max(bound, adjustedLowerBound_);
break;
}
case Settings::PriceThreshold: {
// strikeFromPrice ensures that returned strike is on the expected
// side of strike
Real bound =
strikeFromPrice(settings_.vegaRatio_, optionType, strike);
if (optionType == Option::Call)
upper = std::min(bound, adjustedUpperBound_);
else
lower = std::max(bound, adjustedLowerBound_);
break;
}
case Settings::BSStdDevs : {
Real atm = smileSection_->atmLevel();
Real atmVol = smileSection_->volatility(atm);
Real shift = smileSection_->shift();
Real lowerTmp, upperTmp;
if (smileSection_->volatilityType() == ShiftedLognormal) {
upperTmp = (atm + shift) *
std::exp(settings_.stdDevs_ * atmVol -
0.5 * atmVol * atmVol *
smileSection_->exerciseTime()) -
shift;
lowerTmp = (atm + shift) *
std::exp(-settings_.stdDevs_ * atmVol -
0.5 * atmVol * atmVol *
smileSection_->exerciseTime()) -
shift;
} else {
Real tmp = settings_.stdDevs_ * atmVol *
std::sqrt(smileSection_->exerciseTime());
upperTmp = atm + tmp;
lowerTmp = atm - tmp;
}
upper = std::min(upperTmp - shift, adjustedUpperBound_);
lower = std::max(lowerTmp - shift, adjustedLowerBound_);
break;
}
default:
QL_FAIL("Unknown strategy (" << settings_.strategy_ << ")");
}
// compute the relevant integral
Real result = 0.0;
Real tmpBound;
if (upper > lower) {
tmpBound = std::min(upper, swapRateValue_);
if (tmpBound > lower) {
result += (*integrator_)(integrand_f(this),
lower, tmpBound);
}
tmpBound = std::max(lower, swapRateValue_);
if (upper > tmpBound) {
result += (*integrator_)(integrand_f(this),
tmpBound, upper);
}
result *= (optionType == Option::Call ? 1.0 : -1.0);
}
result += singularTerms(optionType, strike);
return annuity_ * result * couponDiscountRatio_ *
coupon_->accrualPeriod();
}
Real LinearTsrPricer::meanReversion() const { return meanReversion_->value(); }
Rate LinearTsrPricer::swapletRate() const {
return swapletPrice() /
(coupon_->accrualPeriod() * discountCurvePaymentDiscount_ * couponDiscountRatio_);
}
Real LinearTsrPricer::capletPrice(Rate effectiveCap) const {
// caplet is equivalent to call option on fixing
if (fixingDate_ <= today_) {
// the fixing is determined
const Rate Rs = std::max(
coupon_->swapIndex()->fixing(fixingDate_) - effectiveCap, 0.);
Rate price = (gearing_ * Rs) * (coupon_->accrualPeriod() *
discountCurvePaymentDiscount_ * couponDiscountRatio_);
return price;
} else {
Real capletPrice = optionletPrice(Option::Call, effectiveCap);
return gearing_ * capletPrice;
}
}
Rate LinearTsrPricer::capletRate(Rate effectiveCap) const {
return capletPrice(effectiveCap) /
(coupon_->accrualPeriod() * discountCurvePaymentDiscount_ * couponDiscountRatio_);
}
Real LinearTsrPricer::floorletPrice(Rate effectiveFloor) const {
// floorlet is equivalent to put option on fixing
if (fixingDate_ <= today_) {
// the fixing is determined
const Rate Rs = std::max(
effectiveFloor - coupon_->swapIndex()->fixing(fixingDate_), 0.);
Rate price = (gearing_ * Rs) * (coupon_->accrualPeriod() *
discountCurvePaymentDiscount_ * couponDiscountRatio_);
return price;
} else {
Real floorletPrice = optionletPrice(Option::Put, effectiveFloor);
return gearing_ * floorletPrice;
}
}
Rate LinearTsrPricer::floorletRate(Rate effectiveFloor) const {
return floorletPrice(effectiveFloor) /
(coupon_->accrualPeriod() * discountCurvePaymentDiscount_ * couponDiscountRatio_);
}
Real LinearTsrPricer::swapletPrice() const {
if (fixingDate_ <= today_) {
// the fixing is determined
const Rate Rs = coupon_->swapIndex()->fixing(fixingDate_);
Rate price =
(gearing_ * Rs + spread_) *
(coupon_->accrualPeriod() * discountCurvePaymentDiscount_ * couponDiscountRatio_);
return price;
} else {
Real atmCapletPrice = optionletPrice(Option::Call, swapRateValue_);
Real atmFloorletPrice = optionletPrice(Option::Put, swapRateValue_);
return gearing_ * (coupon_->accrualPeriod() * discountCurvePaymentDiscount_ *
swapRateValue_ * couponDiscountRatio_ +
atmCapletPrice - atmFloorletPrice) +
spreadLegValue_;
}
}
}
|