Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
midpointcdsenginemultistate.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
20
21#include <ql/cashflows/fixedratecoupon.hpp>
22#include <ql/instruments/claim.hpp>
23#include <ql/termstructures/yieldtermstructure.hpp>
24
25using namespace QuantLib;
26
27namespace QuantExt {
28
30 const std::vector<Handle<DefaultProbabilityTermStructure>>& defaultCurves,
31 const std::vector<Handle<Quote>> recoveryRates, const Handle<YieldTermStructure>& discountCurve,
32 const Size mainResultState, const boost::optional<bool> includeSettlementDateFlows)
33 : MidPointCdsEngine(Handle<DefaultProbabilityTermStructure>(), 0.0, discountCurve, includeSettlementDateFlows),
34 defaultCurves_(defaultCurves), recoveryRates_(recoveryRates), mainResultState_(mainResultState) {
35 QL_REQUIRE(defaultCurves.size() == recoveryRates.size(), "MidPointCdsEngineMultiState: number of default curves ("
36 << defaultCurves_.size()
37 << ") must match number of recovery rates ("
38 << recoveryRates_.size() << ")");
39 QL_REQUIRE(!defaultCurves.empty(), "MidPointCdsEngineMultiState: no default curves / recovery rates given");
40 for (auto const& h : defaultCurves_) {
41 registerWith(h);
42 }
43 for (auto const& r : recoveryRates_) {
44 registerWith(r);
45 }
46 QL_REQUIRE(mainResultState < defaultCurves.size(), "MidPointCdsEngineMultiState: mainResultState ("
47 << mainResultState << ") out of range 0..."
48 << defaultCurves.size() - 1);
49}
50
52 probability_ = defaultCurves_[i];
53 recoveryRate_ = recoveryRates_[i]->value();
54}
55
57
58 // calculate all states except the main states
59
60 std::vector<Real> values(defaultCurves_.size() + 1);
61 for (Size i = 0; i < defaultCurves_.size(); ++i) {
62 if (i == mainResultState_)
63 continue;
64 linkCurves(i);
65 MidPointCdsEngine::calculate();
66 values[i] = results_.value;
67 }
68
69 // calculate the main state last to keep the results from this calculation
70
72 MidPointCdsEngine::calculate();
73 values[mainResultState_] = results_.value;
74
75 // calculate the default state
76
77 values.back() = calculateDefaultValue();
78
79 // set additional result
80
81 results_.additionalResults["stateNpv"] = values;
82}
83
85 Date defaultDate = discountCurve_->referenceDate();
86 Real phi = arguments_.side == Protection::Seller ? -1.0 : 1.0;
87 return phi * arguments_.claim->amount(defaultDate, arguments_.notional, recoveryRates_.back()->value());
88}
89
90} // namespace QuantExt
const Instrument::results * results_
Definition: cdsoption.cpp:81
const std::vector< Handle< Quote > > & recoveryRates() const
MidPointCdsEngineMultiState(const std::vector< Handle< DefaultProbabilityTermStructure > > &defaultCurves, const std::vector< Handle< Quote > > recoveryRates, const Handle< YieldTermStructure > &discountCurve, const Size mainResultState, const boost::optional< bool > includeSettlementDateFlows=boost::none)
std::vector< Handle< DefaultProbabilityTermStructure > > defaultCurves_
const std::vector< Handle< DefaultProbabilityTermStructure > > & defaultCurves() const
Swap::arguments * arguments_