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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2013 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/experimental/models/basketgeneratingengine.hpp>
#include <ql/experimental/exercise/rebatedexercise.hpp>
#include <ql/math/optimization/levenbergmarquardt.hpp>
#include <ql/math/optimization/simplex.hpp>
#include <ql/models/shortrate/calibrationhelpers/swaptionhelper.hpp>
#include <ql/termstructures/volatility/swaption/swaptionvolcube.hpp>
#include <ql/quotes/simplequote.hpp>
#include <boost/make_shared.hpp>
namespace QuantLib {
Disposable<std::vector<boost::shared_ptr<CalibrationHelper> > >
BasketGeneratingEngine::calibrationBasket(
const boost::shared_ptr<Exercise> &exercise,
boost::shared_ptr<SwapIndex> standardSwapBase,
boost::shared_ptr<SwaptionVolatilityStructure> swaptionVolatility,
const CalibrationBasketType basketType) const {
QL_REQUIRE(
!standardSwapBase->forwardingTermStructure().empty(),
"standard swap base forwarding term structure must not be empty.");
QL_REQUIRE(
!standardSwapBase->exogenousDiscount() ||
!standardSwapBase->discountingTermStructure().empty(),
"standard swap base discounting term structure must not be empty.");
std::vector<boost::shared_ptr<CalibrationHelper> > result;
int minIdxAlive =
std::upper_bound(exercise->dates().begin(), exercise->dates().end(),
Settings::instance().evaluationDate()) -
exercise->dates().begin();
boost::shared_ptr<RebatedExercise> rebEx =
boost::dynamic_pointer_cast<RebatedExercise>(exercise);
for (Size i = minIdxAlive; i < exercise->dates().size(); i++) {
Date expiry = exercise->date(i);
Real rebate = 0.0;
Date rebateDate = expiry;
if (rebEx != NULL) {
rebate = rebEx->rebate(i);
rebateDate = rebEx->rebatePaymentDate(i);
}
boost::shared_ptr<SwaptionHelper> helper;
switch (basketType) {
case Naive: {
Real swapLength = swaptionVolatility->dayCounter().yearFraction(
standardSwapBase->valueDate(expiry), underlyingLastDate());
boost::shared_ptr<SmileSection> sec =
swaptionVolatility->smileSection(
expiry,
static_cast<Size>(swapLength * 12.0 + 0.5) * Months,
true);
Real atmStrike = sec->atmLevel();
Real atmVol;
if (atmStrike == Null<Real>())
atmVol = sec->volatility(0.03);
else
atmVol = sec->volatility(atmStrike);
helper = boost::shared_ptr<SwaptionHelper>(new SwaptionHelper(
expiry, underlyingLastDate(),
Handle<Quote>(boost::make_shared<SimpleQuote>(atmVol)),
standardSwapBase->iborIndex(),
standardSwapBase->fixedLegTenor(),
standardSwapBase->dayCounter(),
standardSwapBase->iborIndex()->dayCounter(),
standardSwapBase->exogenousDiscount()
? standardSwapBase->discountingTermStructure()
: standardSwapBase->forwardingTermStructure(),
CalibrationHelper::RelativePriceError, Null<Real>(), 1.0));
break;
}
case MaturityStrikeByDeltaGamma: {
// determine the npv, first and second order derivatives at
// $y=0$ of the underlying swap
const Real h = 0.0001; // finite difference step in $y$, make
// this a parameter of the engine ?
Real zSpreadDsc =
oas_.empty() ? 1.0
: exp(-oas_->value() *
onefactormodel_->termStructure()
->dayCounter()
.yearFraction(expiry, rebateDate));
Real npvm = underlyingNpv(expiry, -h) +
rebate *
onefactormodel_->zerobond(rebateDate, expiry,
-h, discountCurve_) *
zSpreadDsc;
Real npv = underlyingNpv(expiry, 0.0) +
rebate * onefactormodel_->zerobond(
rebateDate, expiry, 0, discountCurve_) *
zSpreadDsc;
Real npvp = underlyingNpv(expiry, h) +
rebate *
onefactormodel_->zerobond(rebateDate, expiry, h,
discountCurve_) *
zSpreadDsc;
Real delta = (npvp - npvm) / (2.0 * h);
Real gamma = (npvp - 2.0 * npv + npvm) / (h * h);
QL_REQUIRE(npv * npv + delta * delta + gamma * gamma > 0.0,
"(npv,delta,gamma) must have a positive norm");
// debug output
// std::cout << "EXOTIC npv " << npv << " delta " << delta
// << " gamma " << gamma << std::endl;
// Real xtmp = -5.0;
// std::cout
// << "********************************************EXERCISE "
// << expiry << " ******************" << std::endl;
// std::cout << "globalExoticNpv;";
// while (xtmp <= 5.0 + QL_EPSILON) {
// std::cout << underlyingNpv(expiry, xtmp) << ";";
// xtmp += 0.1;
// }
// std::cout << std::endl;
// end debug output
// play safe, we restrict the maximum maturity so to easily fit
// in the date class restriction
Real maxMaturity =
swaptionVolatility->dayCounter().yearFraction(
expiry, Date::maxDate() - 365);
boost::shared_ptr<MatchHelper> matchHelper_;
matchHelper_ = boost::shared_ptr<MatchHelper>(new MatchHelper(
underlyingType(), npv, delta, gamma, onefactormodel_,
standardSwapBase, expiry, maxMaturity, h));
// Optimize
Array initial = initialGuess(expiry);
QL_REQUIRE(initial.size() == 3,
"initial guess must have size 3 (but is "
<< initial.size() << ")");
EndCriteria ec(1000, 200, 1E-8, 1E-8, 1E-8); // make these
// criteria and the
// optimizer itself
// parameters of
// the method ?
Constraint constraint = NoConstraint();
Problem p(*matchHelper_, constraint, initial);
LevenbergMarquardt lm;
EndCriteria::Type ret = lm.minimize(p, ec);
QL_REQUIRE(ret != EndCriteria::None &&
ret != EndCriteria::Unknown &&
ret != EndCriteria::MaxIterations,
"optimizer returns error (" << ret << ")");
Array solution = p.currentValue();
Real maturity = fabs(solution[1]);
Size years = (Size)std::floor(maturity);
maturity -= (Real)years;
maturity *= 12.0;
Size months = (Size)std::floor(maturity + 0.5);
if (years == 0 && months == 0)
months = 1; // ensure a maturity of at least one months
// maturity -= (Real)months; maturity *= 365.25;
// Size days = (Size)std::floor(maturity);
Period matPeriod =
years * Years + months * Months; //+days*Days;
// we have to floor the strike of the calibration instrument,
// see warning in the header
solution[2] = std::max(solution[2], 0.00001); // floor at 0.1bp
// also the calibrated nominal may be zero, so we floor it, too
solution[0] =
std::max(solution[0], 0.000001); // float at 0.01bp
helper = boost::shared_ptr<SwaptionHelper>(new SwaptionHelper(
expiry, matPeriod,
Handle<Quote>(boost::make_shared<SimpleQuote>(
swaptionVolatility->volatility(
expiry, matPeriod, solution[2], true))),
standardSwapBase->iborIndex(),
standardSwapBase->fixedLegTenor(),
standardSwapBase->dayCounter(),
standardSwapBase->iborIndex()->dayCounter(),
standardSwapBase->exogenousDiscount()
? standardSwapBase->discountingTermStructure()
: standardSwapBase->forwardingTermStructure(),
CalibrationHelper::RelativePriceError, solution[2],
fabs(solution[0])));
break;
}
default:
QL_FAIL("Calibration basket type not known (" << basketType
<< ")");
}
result.push_back(helper);
}
return result;
}
}
|