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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
Copyright (C) 2006 Cristina Duminuco
Copyright (C) 2006 Marco Bianchetti
Copyright (C) 2007 StatPro Italia srl
Copyright (C) 2014 Ferdinando Ametrano
Copyright (C) 2016, 2018 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/instruments/swaption.hpp>
#include <ql/pricingengines/swaption/blackswaptionengine.hpp>
#include <ql/math/solvers1d/newtonsafe.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/exercise.hpp>
#include <ql/shared_ptr.hpp>
namespace QuantLib {
namespace {
class ImpliedSwaptionVolHelper {
public:
ImpliedSwaptionVolHelper(const Swaption&,
const Handle<YieldTermStructure>& discountCurve,
Real targetValue,
Real displacement,
VolatilityType type);
Real operator()(Volatility x) const;
Real derivative(Volatility x) const;
private:
ext::shared_ptr<PricingEngine> engine_;
Handle<YieldTermStructure> discountCurve_;
Real targetValue_;
ext::shared_ptr<SimpleQuote> vol_;
const Instrument::results* results_;
};
ImpliedSwaptionVolHelper::ImpliedSwaptionVolHelper(
const Swaption& swaption,
const Handle<YieldTermStructure>& discountCurve,
Real targetValue,
Real displacement,
VolatilityType type)
: discountCurve_(discountCurve), targetValue_(targetValue),
vol_(ext::make_shared<SimpleQuote>(-1.0)) {
// vol_ is set an implausible value, so that calculation is forced
// at first ImpliedSwaptionVolHelper::operator()(Volatility x) call
Handle<Quote> h(vol_);
switch (type) {
case ShiftedLognormal:
engine_ = ext::make_shared<BlackSwaptionEngine>(
discountCurve_, h, Actual365Fixed(), displacement);
break;
case Normal:
engine_ = ext::make_shared<BachelierSwaptionEngine>(
discountCurve_, h, Actual365Fixed());
break;
default:
QL_FAIL("unknown VolatilityType (" << type << ")");
break;
}
swaption.setupArguments(engine_->getArguments());
results_ = dynamic_cast<const Instrument::results *>(
engine_->getResults());
}
Real ImpliedSwaptionVolHelper::operator()(Volatility x) const {
if (x!=vol_->value()) {
vol_->setValue(x);
engine_->calculate();
}
return results_->value-targetValue_;
}
Real ImpliedSwaptionVolHelper::derivative(Volatility x) const {
if (x!=vol_->value()) {
vol_->setValue(x);
engine_->calculate();
}
std::map<std::string,boost::any>::const_iterator vega_ =
results_->additionalResults.find("vega");
QL_REQUIRE(vega_ != results_->additionalResults.end(),
"vega not provided");
return boost::any_cast<Real>(vega_->second);
}
}
std::ostream& operator<<(std::ostream& out,
Settlement::Type t) {
switch (t) {
case Settlement::Physical:
return out << "Delivery";
case Settlement::Cash:
return out << "Cash";
default:
QL_FAIL("unknown Settlement::Type(" << Integer(t) << ")");
}
}
std::ostream& operator<<(std::ostream& out, Settlement::Method m) {
switch (m) {
case Settlement::PhysicalOTC:
return out << "PhysicalOTC";
case Settlement::PhysicalCleared:
return out << "PhysicalCleared";
case Settlement::CollateralizedCashPrice:
return out << "CollateralizedCashPrice";
case Settlement::ParYieldCurve:
return out << "ParYieldCurve";
default:
QL_FAIL("unknown Settlement::Method(" << Integer(m) << ")");
}
}
Swaption::Swaption(const ext::shared_ptr<VanillaSwap>& swap,
const ext::shared_ptr<Exercise>& exercise,
Settlement::Type delivery,
Settlement::Method settlementMethod)
: Option(ext::shared_ptr<Payoff>(), exercise), swap_(swap),
settlementType_(delivery), settlementMethod_(settlementMethod) {
registerWith(swap_);
registerWithObservables(swap_);
}
bool Swaption::isExpired() const {
return detail::simple_event(exercise_->dates().back()).hasOccurred();
}
void Swaption::setupArguments(PricingEngine::arguments* args) const {
swap_->setupArguments(args);
Swaption::arguments* arguments =
dynamic_cast<Swaption::arguments*>(args);
QL_REQUIRE(arguments != 0, "wrong argument type");
arguments->swap = swap_;
arguments->settlementType = settlementType_;
arguments->settlementMethod = settlementMethod_;
arguments->exercise = exercise_;
}
void Swaption::arguments::validate() const {
VanillaSwap::arguments::validate();
QL_REQUIRE(swap, "vanilla swap not set");
QL_REQUIRE(exercise, "exercise not set");
Settlement::checkTypeAndMethodConsistency(settlementType,
settlementMethod);
}
Volatility Swaption::impliedVolatility(Real targetValue,
const Handle<YieldTermStructure>& d,
Volatility guess,
Real accuracy,
Natural maxEvaluations,
Volatility minVol,
Volatility maxVol,
VolatilityType type,
Real displacement) const {
//calculate();
QL_REQUIRE(!isExpired(), "instrument expired");
ImpliedSwaptionVolHelper f(*this, d, targetValue, displacement, type);
//Brent solver;
NewtonSafe solver;
solver.setMaxEvaluations(maxEvaluations);
return solver.solve(f, accuracy, guess, minVol, maxVol);
}
void Settlement::checkTypeAndMethodConsistency(
Settlement::Type settlementType,
Settlement::Method settlementMethod) {
if (settlementType == Physical) {
QL_REQUIRE(settlementMethod == PhysicalOTC ||
settlementMethod == PhysicalCleared,
"invalid settlement method for physical settlement");
}
if (settlementType == Cash) {
QL_REQUIRE(settlementMethod == CollateralizedCashPrice ||
settlementMethod == ParYieldCurve,
"invalid settlement method for cash settlement");
}
}
}
|