Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
indexcdsoptionbaseengine.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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
20
21#include <ql/exercise.hpp>
22#include <ql/pricingengines/blackformula.hpp>
23#include <ql/pricingengines/credit/isdacdsengine.hpp>
24#include <ql/pricingengines/credit/midpointcdsengine.hpp>
25#include <ql/termstructures/credit/flathazardrate.hpp>
26#include <ql/termstructures/yield/flatforward.hpp>
27#include <ql/time/daycounters/actual360.hpp>
29
30#include <numeric>
31
32using namespace QuantLib;
33using std::string;
34using std::vector;
35
36namespace QuantExt {
37
38IndexCdsOptionBaseEngine::IndexCdsOptionBaseEngine(const Handle<DefaultProbabilityTermStructure>& probability,
39 Real recovery,
40 const Handle<YieldTermStructure>& discountSwapCurrency,
41 const Handle<YieldTermStructure>& discountTradeCollateral,
42 const Handle<QuantExt::CreditVolCurve>& volatility)
43 : probabilities_({probability}), recoveries_({recovery}), discountSwapCurrency_(discountSwapCurrency),
44 discountTradeCollateral_(discountTradeCollateral), volatility_(volatility), indexRecovery_(recovery) {
45 registerWithMarket();
46}
47
48IndexCdsOptionBaseEngine::IndexCdsOptionBaseEngine(const vector<Handle<DefaultProbabilityTermStructure>>& probabilities,
49 const vector<Real>& recoveries,
50 const Handle<YieldTermStructure>& discountSwapCurrency,
51 const Handle<YieldTermStructure>& discountTradeCollateral,
52 const Handle<QuantExt::CreditVolCurve>& volatility,
53 Real indexRecovery)
54 : probabilities_(probabilities), recoveries_(recoveries), discountSwapCurrency_(discountSwapCurrency),
55 discountTradeCollateral_(discountTradeCollateral), volatility_(volatility), indexRecovery_(indexRecovery) {
56
57 QL_REQUIRE(!probabilities_.empty(), "IndexCdsOptionBaseEngine: need at least one probability curve.");
58 QL_REQUIRE(probabilities_.size() == recoveries_.size(), "IndexCdsOptionBaseEngine: mismatch between size"
59 << " of probabilities (" << probabilities_.size()
60 << ") and recoveries (" << recoveries_.size() << ").");
61
62 registerWithMarket();
63
64 // If the index recovery is not populated, use the average recovery.
65 if (indexRecovery_ == Null<Real>())
66 indexRecovery_ = accumulate(recoveries_.begin(), recoveries_.end(), 0.0) / recoveries_.size();
67}
68
69const vector<Handle<DefaultProbabilityTermStructure>>& IndexCdsOptionBaseEngine::probabilities() const {
70 return probabilities_;
71}
72
73const vector<Real>& IndexCdsOptionBaseEngine::recoveries() const { return recoveries_; }
74
75const Handle<YieldTermStructure> IndexCdsOptionBaseEngine::discountSwapCurrency() const {
76 return discountSwapCurrency_;
77}
78
79const Handle<YieldTermStructure> IndexCdsOptionBaseEngine::discountTradeCollateral() const {
80 return discountTradeCollateral_;
81}
82
83const Handle<QuantExt::CreditVolCurve> IndexCdsOptionBaseEngine::volatility() const { return volatility_; }
84
85void IndexCdsOptionBaseEngine::calculate() const {
86
87 // Underlying index CDS
88 const auto& cds = *arguments_.swap;
89
90 // If given constituent curves, store constituent notionals. Otherwise, store top level notional.
91 if (probabilities_.size() > 1) {
92 notionals_ = cds.underlyingNotionals();
93 QL_REQUIRE(probabilities_.size() == notionals_.size(), "IndexCdsOptionBaseEngine: mismatch between size"
94 << " of probabilities (" << probabilities_.size()
95 << ") and notionals (" << notionals_.size() << ").");
96 } else {
97 notionals_ = {cds.notional()};
98 }
99
100 // Get additional results of underlying index CDS.
101 cds.NPV();
102 results_.additionalResults = cds.additionalResults();
103
104 // call engine-specific calculation
105 doCalc();
106}
107
108Real IndexCdsOptionBaseEngine::fep() const {
109
110 // Exercise date
111 const Date& exerciseDate = arguments_.exercise->dates().front();
112
113 // Realised FEP
114 results_.additionalResults["realisedFEP"] = arguments_.realisedFep;
115
116 // Unrealised FEP
117 Real fep = 0.0;
118 for (Size i = 0; i < probabilities_.size(); ++i) {
119 fep += (1 - recoveries_[i]) * probabilities_[i]->defaultProbability(exerciseDate) * notionals_[i];
120 }
121 results_.additionalResults["UnrealisedFEP"] = fep;
122
123 // Total FEP
124 fep += arguments_.realisedFep;
125 results_.additionalResults["FEP"] = fep;
126
127 // Discounted FEP
128 fep *= discountTradeCollateral_->discount(exerciseDate);
129 results_.additionalResults["discountedFEP"] = fep;
130
131 return fep;
132}
133
134void IndexCdsOptionBaseEngine::registerWithMarket() {
135 for (const auto& p : probabilities_)
136 registerWith(p);
137 registerWith(discountTradeCollateral_);
138 registerWith(discountSwapCurrency_);
139 registerWith(volatility_);
140}
141
142} // namespace QuantExt
const Instrument::results * results_
Definition: cdsoption.cpp:81
IndexCdsOptionBaseEngine(const QuantLib::Handle< QuantLib::DefaultProbabilityTermStructure > &probability, QuantLib::Real recovery, const Handle< YieldTermStructure > &discountSwapCurrency, const Handle< YieldTermStructure > &discountTradeCollateral, const QuantLib::Handle< QuantExt::CreditVolCurve > &volatility)
Constructor taking a default probability term structure bootstrapped from the index spreads.
Base class for index cds option pricing engines.
Swap::arguments * arguments_
time related utilities.