Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
indexcdsoption.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
21#include <ql/exercise.hpp>
22#include <ql/instruments/payoffs.hpp>
23#include <ql/math/distributions/normaldistribution.hpp>
24#include <ql/math/solvers1d/brent.hpp>
25#include <ql/quotes/simplequote.hpp>
26#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
27#include <ql/termstructures/yieldtermstructure.hpp>
28#include <boost/make_shared.hpp>
29
30using namespace QuantLib;
31
32namespace QuantExt {
33
34namespace {
35
36class ImpliedVolHelper {
37public:
38 ImpliedVolHelper(const IndexCdsOption& cdsoption, const Handle<DefaultProbabilityTermStructure>& probability,
39 Real recoveryRate, const Handle<YieldTermStructure>& termStructureSwapCurrency,
40 const Handle<YieldTermStructure>& termStructureTradeCollateral, Real targetValue)
41 : targetValue_(targetValue) {
42
43 vol_ = QuantLib::ext::shared_ptr<SimpleQuote>(new SimpleQuote(0.0));
44 Handle<BlackVolTermStructure> h(
45 QuantLib::ext::make_shared<BlackConstantVol>(0, NullCalendar(), Handle<Quote>(vol_), Actual365Fixed()));
46 engine_ = QuantLib::ext::make_shared<QuantExt::BlackIndexCdsOptionEngine>(
47 probability, recoveryRate, termStructureSwapCurrency, termStructureTradeCollateral,
48 Handle<CreditVolCurve>(QuantLib::ext::make_shared<CreditVolCurveWrapper>(h)));
49 cdsoption.setupArguments(engine_->getArguments());
50
51 results_ = dynamic_cast<const Instrument::results*>(engine_->getResults());
52 }
53 Real operator()(Volatility x) const {
54 vol_->setValue(x);
55 engine_->calculate();
56 return results_->value - targetValue_;
57 }
58
59private:
60 QuantLib::ext::shared_ptr<PricingEngine> engine_;
62 QuantLib::ext::shared_ptr<SimpleQuote> vol_;
64};
65
66}
67
68IndexCdsOption::IndexCdsOption(const QuantLib::ext::shared_ptr<IndexCreditDefaultSwap>& swap,
69 const QuantLib::ext::shared_ptr<Exercise>& exercise, Real strike,
70 CdsOption::StrikeType strikeType, const Settlement::Type settlementType,
71 Real tradeDateNtl, Real realisedFep, const QuantLib::Period& indexTerm)
72 : Option(QuantLib::ext::make_shared<NullPayoff>(), exercise), swap_(swap), strike_(strike), strikeType_(strikeType),
73 settlementType_(settlementType), tradeDateNtl_(tradeDateNtl), realisedFep_(realisedFep),
74 indexTerm_(indexTerm), riskyAnnuity_(0.0) {
75 registerWith(swap_);
76}
77
78bool IndexCdsOption::isExpired() const { return detail::simple_event(exercise_->dates().back()).hasOccurred(); }
79
81 Instrument::setupExpired();
82 riskyAnnuity_ = 0.0;
83}
84
86 swap_->setupArguments(args);
87 Option::setupArguments(args);
88
90
91 QL_REQUIRE(arguments != 0, "wrong argument type");
92
100}
101
102void IndexCdsOption::fetchResults(const PricingEngine::results* r) const {
103 Option::fetchResults(r);
104 const IndexCdsOption::results* results = dynamic_cast<const IndexCdsOption::results*>(r);
105 QL_ENSURE(results != 0, "wrong results type");
107}
108
109Rate IndexCdsOption::atmRate() const { return swap_->fairSpreadClean(); }
110
112 calculate();
113 QL_REQUIRE(riskyAnnuity_ != Null<Real>(), "risky annuity not provided");
114 return riskyAnnuity_;
115}
116
117Volatility IndexCdsOption::impliedVolatility(Real targetValue,
118 const Handle<YieldTermStructure>& termStructureSwapCurrency,
119 const Handle<YieldTermStructure>& termStructureTradeCollateral,
120 const Handle<DefaultProbabilityTermStructure>& probability,
121 Real recoveryRate, Real accuracy, Size maxEvaluations, Volatility minVol,
122 Volatility maxVol) const {
123 calculate();
124 QL_REQUIRE(!isExpired(), "instrument expired");
125
126 Volatility guess = 0.10;
127
128 ImpliedVolHelper f(*this, probability, recoveryRate, termStructureSwapCurrency, termStructureTradeCollateral,
129 targetValue);
130 Brent solver;
131 solver.setMaxEvaluations(maxEvaluations);
132 return solver.solve(f, accuracy, guess, minVol, maxVol);
133}
134
136 IndexCreditDefaultSwap::arguments::validate();
137 Option::arguments::validate();
138 QL_REQUIRE(swap, "CDS not set");
139 QL_REQUIRE(exercise, "exercise not set");
140}
141
143 Option::results::reset();
144 riskyAnnuity = Null<Real>();
145}
146} // namespace QuantExt
Black index credit default swap option engine.
QuantLib::ext::shared_ptr< PricingEngine > engine_
Definition: cdsoption.cpp:78
Real targetValue_
Definition: cdsoption.cpp:79
QuantLib::ext::shared_ptr< SimpleQuote > vol_
Definition: cdsoption.cpp:80
const Instrument::results * results_
Definition: cdsoption.cpp:81
Arguments for index CDS option calculation
QuantLib::ext::shared_ptr< IndexCreditDefaultSwap > swap
Results from index CDS option calculation
void setupArguments(QuantLib::PricingEngine::arguments *args) const override
QuantLib::Real riskyAnnuity() const
bool isExpired() const override
QuantLib::Volatility impliedVolatility(QuantLib::Real price, const QuantLib::Handle< QuantLib::YieldTermStructure > &termStructureSwapCurrency, const QuantLib::Handle< QuantLib::YieldTermStructure > &termStructureTradeCollateral, const QuantLib::Handle< QuantLib::DefaultProbabilityTermStructure > &, QuantLib::Real recoveryRate, QuantLib::Real accuracy=1.e-4, QuantLib::Size maxEvaluations=100, QuantLib::Volatility minVol=1.0e-7, QuantLib::Volatility maxVol=4.0) const
void fetchResults(const QuantLib::PricingEngine::results *results) const override
CdsOption::StrikeType strikeType_
QuantLib::Rate atmRate() const
QuantLib::ext::shared_ptr< IndexCreditDefaultSwap > swap_
void setupExpired() const override
IndexCdsOption(const QuantLib::ext::shared_ptr< IndexCreditDefaultSwap > &swap, const QuantLib::ext::shared_ptr< QuantLib::Exercise > &exercise, QuantLib::Real strike, CdsOption::StrikeType strikeType_=CdsOption::Spread, Settlement::Type settlementType=Settlement::Cash, QuantLib::Real tradeDateNtl=QuantLib::Null< QuantLib::Real >(), QuantLib::Real realisedFep=QuantLib::Null< QuantLib::Real >(), const QuantLib::Period &indexTerm=5 *Years)
QuantLib::Real riskyAnnuity_
Settlement::Type settlementType_
QuantLib::Real tradeDateNtl_
QuantLib::Period indexTerm_
Index CDS option instrument.