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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2007, 2008 Ferdinando Ametrano
Copyright (C) 2007 Franois du Vignaud
Copyright (C) 2007 Katiuscia Manzoni
Copyright (C) 2007 Giorgio Facchinetti
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/termstructures/volatility/optionlet/optionletstripper1.hpp>
#include <ql/instruments/makecapfloor.hpp>
#include <ql/pricingengines/capfloor/blackcapfloorengine.hpp>
#include <ql/pricingengines/blackformula.hpp>
#include <ql/indexes/iborindex.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/utilities/dataformatters.hpp>
using boost::shared_ptr;
namespace QuantLib {
OptionletStripper1::OptionletStripper1(
const shared_ptr<CapFloorTermVolSurface>& termVolSurface,
const shared_ptr<IborIndex>& index,
Rate switchStrike,
Real accuracy,
Natural maxIter,
const Handle<YieldTermStructure>& discount)
: OptionletStripper(termVolSurface, index, discount),
volQuotes_(nOptionletTenors_,
std::vector<shared_ptr<SimpleQuote> >(nStrikes_)),
floatingSwitchStrike_(switchStrike==Null<Rate>() ? true : false),
capFlooMatrixNotInitialized_(true),
switchStrike_(switchStrike),
accuracy_(accuracy), maxIter_(maxIter) {
capFloorPrices_ = Matrix(nOptionletTenors_, nStrikes_);
optionletPrices_ = Matrix(nOptionletTenors_, nStrikes_);
capFloorVols_ = Matrix(nOptionletTenors_, nStrikes_);
Real firstGuess = 0.14;
optionletStDevs_ = Matrix(nOptionletTenors_, nStrikes_, firstGuess);
capFloors_ = CapFloorMatrix(nOptionletTenors_);
}
void OptionletStripper1::performCalculations() const {
// update dates
const Date& referenceDate = termVolSurface_->referenceDate();
const DayCounter& dc = termVolSurface_->dayCounter();
shared_ptr<BlackCapFloorEngine> dummy(new
BlackCapFloorEngine(// discounting does not matter here
iborIndex_->forwardingTermStructure(),
0.20, dc));
for (Size i=0; i<nOptionletTenors_; ++i) {
CapFloor temp = MakeCapFloor(CapFloor::Cap,
capFloorLengths_[i],
iborIndex_,
0.04, // dummy strike
0*Days)
.withPricingEngine(dummy);
shared_ptr<FloatingRateCoupon> lFRC =
temp.lastFloatingRateCoupon();
optionletDates_[i] = lFRC->fixingDate();
optionletPaymentDates_[i] = lFRC->date();
optionletAccrualPeriods_[i] = lFRC->accrualPeriod();
optionletTimes_[i] = dc.yearFraction(referenceDate,
optionletDates_[i]);
atmOptionletRate_[i] = iborIndex_->fixing(optionletDates_[i]);
}
if (floatingSwitchStrike_ && capFlooMatrixNotInitialized_) {
Rate averageAtmOptionletRate = 0.0;
for (Size i=0; i<nOptionletTenors_; ++i) {
averageAtmOptionletRate += atmOptionletRate_[i];
}
switchStrike_ = averageAtmOptionletRate / nOptionletTenors_;
}
const Handle<YieldTermStructure>& discountCurve =
discount_.empty() ?
iborIndex_->forwardingTermStructure() :
discount_;
const std::vector<Rate>& strikes = termVolSurface_->strikes();
// initialize CapFloorMatrix
if (capFlooMatrixNotInitialized_) {
for (Size i=0; i<nOptionletTenors_; ++i)
capFloors_[i].resize(nStrikes_);
// construction might go here
for (Size j=0; j<nStrikes_; ++j) {
// using out-of-the-money options
CapFloor::Type capFloorType = strikes[j] < switchStrike_ ?
CapFloor::Floor : CapFloor::Cap;
for (Size i=0; i<nOptionletTenors_; ++i) {
volQuotes_[i][j] = shared_ptr<SimpleQuote>(new
SimpleQuote());
shared_ptr<BlackCapFloorEngine> engine(new
BlackCapFloorEngine(
discountCurve,
Handle<Quote>(volQuotes_[i][j]),
dc));
capFloors_[i][j] = MakeCapFloor(capFloorType,
capFloorLengths_[i], iborIndex_,
strikes[j], 0*Days)
.withPricingEngine(engine);
}
}
capFlooMatrixNotInitialized_ = false;
}
for (Size j=0; j<nStrikes_; ++j) {
Option::Type optionletType = strikes[j] < switchStrike_ ?
Option::Put : Option::Call;
Real previousCapFloorPrice = 0.0;
for (Size i=0; i<nOptionletTenors_; ++i) {
capFloorVols_[i][j] = termVolSurface_->volatility(
capFloorLengths_[i], strikes[j], true);
volQuotes_[i][j]->setValue(capFloorVols_[i][j]);
capFloorPrices_[i][j] = capFloors_[i][j]->NPV();
optionletPrices_[i][j] = capFloorPrices_[i][j] -
previousCapFloorPrice;
previousCapFloorPrice = capFloorPrices_[i][j];
DiscountFactor d =
discountCurve->discount(optionletPaymentDates_[i]);
DiscountFactor optionletAnnuity=optionletAccrualPeriods_[i]*d;
try {
optionletStDevs_[i][j] =
blackFormulaImpliedStdDev(optionletType,
strikes[j],
atmOptionletRate_[i],
optionletPrices_[i][j],
optionletAnnuity, 0.0,
optionletStDevs_[i][j],
accuracy_, maxIter_);
} catch (std::exception& e) {
QL_FAIL("could not bootstrap optionlet:"
"\n type: " << optionletType <<
"\n strike: " << io::rate(strikes[j]) <<
"\n atm: " << io::rate(atmOptionletRate_[i]) <<
"\n price: " << optionletPrices_[i][j] <<
"\n annuity: " << optionletAnnuity <<
"\n expiry: " << optionletDates_[i] <<
"\n error: " << e.what());
}
optionletVolatilities_[i][j] = optionletStDevs_[i][j] /
std::sqrt(optionletTimes_[i]);
}
}
}
const Matrix& OptionletStripper1::capFloorPrices() const {
calculate();
return capFloorPrices_;
}
const Matrix& OptionletStripper1::capFloorVolatilities() const {
calculate();
return capFloorVols_;
}
const Matrix& OptionletStripper1::optionletPrices() const {
calculate();
return optionletPrices_;
}
Rate OptionletStripper1::switchStrike() const {
if (floatingSwitchStrike_)
calculate();
return switchStrike_;
}
}
|