QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
cdsoption.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Roland Stamm
5 Copyright (C) 2009 Jose Aparicio
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/experimental/credit/cdsoption.hpp>
22#include <ql/experimental/credit/blackcdsoptionengine.hpp>
23#include <ql/exercise.hpp>
24#include <ql/quotes/simplequote.hpp>
25#include <ql/instruments/payoffs.hpp>
26#include <ql/termstructures/yieldtermstructure.hpp>
27#include <ql/math/distributions/normaldistribution.hpp>
28#include <ql/math/solvers1d/brent.hpp>
29
30namespace QuantLib {
31
32 namespace {
33
34 class ImpliedVolHelper {
35 public:
36 ImpliedVolHelper(
37 const CdsOption& cdsoption,
38 const Handle<DefaultProbabilityTermStructure>& probability,
39 Real recoveryRate,
40 const Handle<YieldTermStructure>& termStructure,
41 Real targetValue)
42 : targetValue_(targetValue), vol_(ext::make_shared<SimpleQuote>(0.0)) {
43
44 Handle<Quote> h(vol_);
45 engine_ = ext::shared_ptr<PricingEngine>(
46 new BlackCdsOptionEngine(probability, recoveryRate,
47 termStructure, h));
48 cdsoption.setupArguments(engine_->getArguments());
49
50 results_ =
51 dynamic_cast<const Instrument::results*>(
52 engine_->getResults());
53 }
54 Real operator()(Volatility x) const {
55 vol_->setValue(x);
56 engine_->calculate();
57 return results_->value-targetValue_;
58 }
59 private:
60 ext::shared_ptr<PricingEngine> engine_;
61 Real targetValue_;
62 ext::shared_ptr<SimpleQuote> vol_;
63 const Instrument::results* results_;
64 };
65
66 }
67
68
69 CdsOption::CdsOption(const ext::shared_ptr<CreditDefaultSwap>& swap,
70 const ext::shared_ptr<Exercise>& exercise,
71 bool knocksOut)
72 : Option(ext::shared_ptr<Payoff>(new NullPayoff), exercise),
73 swap_(swap), knocksOut_(knocksOut) {
74 QL_REQUIRE(swap->side() == Protection::Buyer || knocksOut_,
75 "receiver CDS options must knock out");
76 QL_REQUIRE(!swap->upfront(), "underlying must be running-spread only");
78 }
79
80 bool CdsOption::isExpired () const {
81 return detail::simple_event(exercise_->dates().back()).hasOccurred();
82 }
83
86 riskyAnnuity_ = 0.0;
87 }
88
90 swap_->setupArguments(args);
92
93 auto* arguments = dynamic_cast<CdsOption::arguments*>(args);
94
95 QL_REQUIRE(arguments != nullptr, "wrong argument type");
96
99 }
100
103 const auto* results = dynamic_cast<const CdsOption::results*>(r);
104 QL_ENSURE(results != nullptr, "wrong results type");
106 }
107
108
109
111 return swap_->fairSpread();
112 }
113
115 calculate();
116 QL_REQUIRE(riskyAnnuity_ != Null<Real>(), "risky annuity not provided");
117 return riskyAnnuity_;
118 }
119
121 Real targetValue,
122 const Handle<YieldTermStructure>& termStructure,
124 Real recoveryRate,
125 Real accuracy,
126 Size maxEvaluations,
127 Volatility minVol,
128 Volatility maxVol) const {
129 calculate();
130 QL_REQUIRE(!isExpired(), "instrument expired");
131
132 Volatility guess = 0.10;
133
134 ImpliedVolHelper f(*this, probability, recoveryRate,
135 termStructure, targetValue);
136 Brent solver;
137 solver.setMaxEvaluations(maxEvaluations);
138 return solver.solve(f, accuracy, guess, minVol, maxVol);
139 }
140
141
142
146 QL_REQUIRE(swap, "CDS not set");
147 QL_REQUIRE(exercise, "exercise not set");
148 }
149
151 Option::results::reset();
153 }
154
155}
156
Brent 1-D solver
Definition: brent.hpp:37
Arguments for CDS-option calculation
Definition: cdsoption.hpp:90
ext::shared_ptr< CreditDefaultSwap > swap
Definition: cdsoption.hpp:94
void validate() const override
Definition: cdsoption.cpp:143
Results from CDS-option calculation
Definition: cdsoption.hpp:100
Real riskyAnnuity() const
Definition: cdsoption.cpp:114
void setupArguments(PricingEngine::arguments *) const override
Definition: cdsoption.cpp:89
bool isExpired() const override
returns whether the instrument might have value greater than zero.
Definition: cdsoption.cpp:80
CdsOption(const ext::shared_ptr< CreditDefaultSwap > &swap, const ext::shared_ptr< Exercise > &exercise, bool knocksOut=true)
Definition: cdsoption.cpp:69
Volatility impliedVolatility(Real price, const Handle< 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:120
Rate atmRate() const
Definition: cdsoption.cpp:110
void setupExpired() const override
Definition: cdsoption.cpp:84
void fetchResults(const PricingEngine::results *) const override
Definition: cdsoption.cpp:101
ext::shared_ptr< CreditDefaultSwap > swap_
Definition: cdsoption.hpp:79
virtual bool hasOccurred(const Date &refDate=Date(), ext::optional< bool > includeRefDate=ext::nullopt) const
returns true if an event has already occurred before a date
Definition: event.cpp:28
Shared handle to an observable.
Definition: handle.hpp:41
void calculate() const override
Definition: instrument.hpp:129
virtual void fetchResults(const PricingEngine::results *) const
Definition: instrument.hpp:155
virtual void setupExpired() const
Definition: instrument.hpp:140
template class providing a null value for a given type.
Definition: null.hpp:76
Dummy payoff class.
Definition: payoffs.hpp:36
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
ext::shared_ptr< Exercise > exercise
Definition: option.hpp:65
void validate() const override
Definition: option.hpp:60
base option class
Definition: option.hpp:36
void setupArguments(PricingEngine::arguments *) const override
Definition: option.hpp:92
ext::shared_ptr< Exercise > exercise_
Definition: option.hpp:50
Abstract base class for option payoffs.
Definition: payoff.hpp:36
void setMaxEvaluations(Size evaluations)
Definition: solver1d.hpp:238
Real solve(const F &f, Real accuracy, Real guess, Real step) const
Definition: solver1d.hpp:84
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
void swap(Array &v, Array &w) noexcept
Definition: array.hpp:903