Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
midpointindexcdsengine.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/instruments/claim.hpp>
22
23namespace QuantExt {
24
25MidPointIndexCdsEngine::MidPointIndexCdsEngine(const Handle<DefaultProbabilityTermStructure>& probability,
26 Real recoveryRate, const Handle<YieldTermStructure>& discountCurve,
27 boost::optional<bool> includeSettlementDateFlows)
28 : MidPointCdsEngineBase(discountCurve, includeSettlementDateFlows), probability_(probability),
29 recoveryRate_(recoveryRate), useUnderlyingCurves_(false) {
30 registerWith(discountCurve_);
31 registerWith(probability_);
32}
33
35 const std::vector<Handle<DefaultProbabilityTermStructure>>& underlyingProbability,
36 const std::vector<Real>& underlyingRecoveryRate, const Handle<YieldTermStructure>& discountCurve,
37 boost::optional<bool> includeSettlementDateFlows)
38 : MidPointCdsEngineBase(discountCurve, includeSettlementDateFlows), underlyingProbability_(underlyingProbability),
39 underlyingRecoveryRate_(underlyingRecoveryRate), useUnderlyingCurves_(true) {
40 registerWith(discountCurve_);
41 for (Size i = 0; i < underlyingProbability_.size(); ++i)
42 registerWith(underlyingProbability_[i]);
43}
44
47 return probability_->survivalProbability(d);
48 Real sum = 0.0, sumNotional = 0.0;
49 for (Size i = 0; i < underlyingProbability_.size(); ++i) {
50 sum += underlyingProbability_[i]->survivalProbability(d) * arguments_.underlyingNotionals[i];
51 sumNotional += arguments_.underlyingNotionals[i];
52 }
53 return sum / sumNotional;
54}
55
56Real MidPointIndexCdsEngine::defaultProbability(const Date& d1, const Date& d2) const {
58 return probability_->defaultProbability(d1, d2);
59 Real sum = 0.0, sumNotional = 0.0;
60 for (Size i = 0; i < underlyingProbability_.size(); ++i) {
61 sum += underlyingProbability_[i]->defaultProbability(d1, d2) * arguments_.underlyingNotionals[i];
62 sumNotional += arguments_.underlyingNotionals[i];
63 }
64 return sum / sumNotional;
65}
66
67Real MidPointIndexCdsEngine::expectedLoss(const Date& defaultDate, const Date& d1, const Date& d2,
68 const Real notional) const {
70 return arguments_.claim->amount(defaultDate, notional, recoveryRate_) *
71 probability_->defaultProbability(d1, d2);
72 Real sum = 0.0, sumNotional = 0.0;
73 for (Size i = 0; i < underlyingProbability_.size(); ++i) {
74 sum += arguments_.claim->amount(defaultDate, arguments_.underlyingNotionals[i], underlyingRecoveryRate_[i]) *
75 underlyingProbability_[i]->defaultProbability(d1, d2);
76 sumNotional += arguments_.underlyingNotionals[i];
77 }
78 return sum / sumNotional * notional;
79}
80
82 QL_REQUIRE(!discountCurve_.empty(), "no discount term structure set");
83 Date refDate;
85 QL_REQUIRE(underlyingProbability_.size() == arguments_.underlyingNotionals.size(),
86 "number of underlyings (" << arguments_.underlyingNotionals.size()
87 << ") does not match number of curves (" << underlyingProbability_.size()
88 << ")");
89 for (Size i = 0; i < underlyingProbability_.size(); ++i)
90 QL_REQUIRE(!underlyingProbability_.empty(), "no probability term structure set for underlying " << i);
91 refDate = underlyingProbability_.front()->referenceDate();
92 } else {
93 QL_REQUIRE(!probability_.empty(), "no probability term structure set");
94 refDate = probability_->referenceDate();
95 }
96
97 MidPointCdsEngineBase::calculate(refDate, arguments_, results_);
98}
99} // namespace QuantExt
const Instrument::results * results_
Definition: cdsoption.cpp:81
Real survivalProbability(const Date &d) const override
Handle< DefaultProbabilityTermStructure > probability_
MidPointIndexCdsEngine(const Handle< DefaultProbabilityTermStructure > &, Real recoveryRate, const Handle< YieldTermStructure > &discountCurve, boost::optional< bool > includeSettlementDateFlows=boost::none)
const std::vector< Handle< DefaultProbabilityTermStructure > > underlyingProbability_
Real expectedLoss(const Date &defaultDate, const Date &d1, const Date &d2, const Real notional) const override
const std::vector< Real > underlyingRecoveryRate_
Real defaultProbability(const Date &d1, const Date &d2) const override
Mid-point engine for credit default swaps.
Real sum(const Cash &c, const Cash &d)
Definition: bondbasket.cpp:107
Swap::arguments * arguments_