Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
cdsoption.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
19/*
20 Copyright (C) 2008 Roland Stamm
21 Copyright (C) 2009 Jose Aparicio
22
23 This file is part of QuantLib, a free-software/open-source library
24 for financial quantitative analysts and developers - http://quantlib.org/
25
26 QuantLib is free software: you can redistribute it and/or modify it
27 under the terms of the QuantLib license. You should have received a
28 copy of the license along with this program; if not, please email
29 <quantlib-dev@lists.sf.net>. The license is also available online at
30 <http://quantlib.org/license.shtml>.
31
32 This program is distributed in the hope that it will be useful, but WITHOUT
33 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
34 FOR A PARTICULAR PURPOSE. See the license for more details.
35*/
36
39
40#include <ql/exercise.hpp>
41#include <ql/instruments/payoffs.hpp>
42#include <ql/math/distributions/normaldistribution.hpp>
43#include <ql/math/solvers1d/brent.hpp>
44#include <ql/quotes/simplequote.hpp>
45#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
46#include <ql/termstructures/yieldtermstructure.hpp>
47
48#include <boost/make_shared.hpp>
49
50namespace QuantExt {
51using namespace QuantLib;
52
53namespace {
54
55class ImpliedVolHelper {
56public:
57 ImpliedVolHelper(const CdsOption& cdsoption, const Handle<DefaultProbabilityTermStructure>& probability,
58 Real recoveryRate, const Handle<YieldTermStructure>& termStructure, Real targetValue)
59 : targetValue_(targetValue) {
60
61 vol_ = QuantLib::ext::shared_ptr<SimpleQuote>(new SimpleQuote(0.0));
62 Handle<BlackVolTermStructure> h(
63 QuantLib::ext::make_shared<BlackConstantVol>(0, NullCalendar(), Handle<Quote>(vol_), Actual365Fixed()));
64 engine_ = QuantLib::ext::shared_ptr<PricingEngine>(
65 new QuantExt::BlackCdsOptionEngine(probability, recoveryRate, termStructure,
66 Handle<CreditVolCurve>(QuantLib::ext::make_shared<CreditVolCurveWrapper>(h))));
67 cdsoption.setupArguments(engine_->getArguments());
68
69 results_ = dynamic_cast<const Instrument::results*>(engine_->getResults());
70 }
71 Real operator()(Volatility x) const {
72 vol_->setValue(x);
73 engine_->calculate();
74 return results_->value - targetValue_;
75 }
76
77private:
78 QuantLib::ext::shared_ptr<PricingEngine> engine_;
80 QuantLib::ext::shared_ptr<SimpleQuote> vol_;
82};
83} // namespace
84
85CdsOption::CdsOption(const QuantLib::ext::shared_ptr<CreditDefaultSwap>& swap, const QuantLib::ext::shared_ptr<Exercise>& exercise,
86 bool knocksOut, const Real strike, const StrikeType strikeType)
87 : Option(QuantLib::ext::shared_ptr<Payoff>(new NullPayoff), exercise), swap_(swap), knocksOut_(knocksOut),
88 strike_(strike == Null<Real>() ? swap_->runningSpread() : strike), strikeType_(strikeType) {
89 registerWith(swap_);
90}
91
92bool CdsOption::isExpired() const { return detail::simple_event(exercise_->dates().back()).hasOccurred(); }
93
95 Instrument::setupExpired();
96 riskyAnnuity_ = 0.0;
97}
98
100 swap_->setupArguments(args);
101 Option::setupArguments(args);
102
104
105 QL_REQUIRE(arguments != 0, "wrong argument type");
106
111}
112
113void CdsOption::fetchResults(const PricingEngine::results* r) const {
114 Option::fetchResults(r);
115 const CdsOption::results* results = dynamic_cast<const CdsOption::results*>(r);
116 QL_ENSURE(results != 0, "wrong results type");
118}
119
120Rate CdsOption::atmRate() const { return swap_->fairSpreadClean(); }
121
123 calculate();
124 QL_REQUIRE(riskyAnnuity_ != Null<Real>(), "risky annuity not provided");
125 return riskyAnnuity_;
126}
127
128Volatility CdsOption::impliedVolatility(Real targetValue, const Handle<YieldTermStructure>& termStructure,
129 const Handle<DefaultProbabilityTermStructure>& probability, Real recoveryRate,
130 Real accuracy, Size maxEvaluations, Volatility minVol,
131 Volatility maxVol) const {
132 calculate();
133 QL_REQUIRE(!isExpired(), "instrument expired");
134
135 Volatility guess = 0.10;
136
137 ImpliedVolHelper f(*this, probability, recoveryRate, termStructure, targetValue);
138 Brent solver;
139 solver.setMaxEvaluations(maxEvaluations);
140 return solver.solve(f, accuracy, guess, minVol, maxVol);
141}
142
144 CreditDefaultSwap::arguments::validate();
145 Option::arguments::validate();
146 QL_REQUIRE(swap, "CDS not set");
147 QL_REQUIRE(exercise, "exercise not set");
148}
149
151 Option::results::reset();
152 riskyAnnuity = Null<Real>();
153}
154} // namespace QuantExt
Black 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
CDS option, removed requirements (rec must knock out, no upfront amount), that should be taken care o...
Arguments for CDS-option calculation
Definition: cdsoption.hpp:110
QuantLib::ext::shared_ptr< CreditDefaultSwap > swap
Definition: cdsoption.hpp:114
void validate() const override
Definition: cdsoption.cpp:143
CdsOption(const QuantLib::ext::shared_ptr< CreditDefaultSwap > &swap, const QuantLib::ext::shared_ptr< Exercise > &exercise, bool knocksOut=true, const Real strike=Null< Real >(), const StrikeType strikeType=StrikeType::Spread)
Definition: cdsoption.cpp:85
Real riskyAnnuity() const
Definition: cdsoption.cpp:122
void setupArguments(PricingEngine::arguments *) const override
Definition: cdsoption.cpp:99
Volatility impliedVolatility(Real price, const Handle< QuantLib::YieldTermStructure > &termStructure, const Handle< DefaultProbabilityTermStructure > &, Real recoveryRate, Real accuracy=1.e-4, Size maxEvaluations=100, Volatility minVol=1.0e-7, Volatility maxVol=4.0) const
Definition: cdsoption.cpp:128
bool isExpired() const override
Definition: cdsoption.cpp:92
QuantLib::ext::shared_ptr< CreditDefaultSwap > swap_
Definition: cdsoption.hpp:99
StrikeType strikeType_
Definition: cdsoption.hpp:102
Rate atmRate() const
Definition: cdsoption.cpp:120
void setupExpired() const override
Definition: cdsoption.cpp:94
void fetchResults(const PricingEngine::results *) const override
Definition: cdsoption.cpp:113