Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
extendedconstantlosslatentmodel.hpp
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#ifndef quantext_extended_constantloss_latentmodel_hpp
20#define quantext_extended_constantloss_latentmodel_hpp
21
22//#include <ql/experimental/credit/defaultprobabilitylatentmodel.hpp>
24// take the loss model to a different file and avoid this inclusion
25//#include <ql/experimental/credit/defaultlossmodel.hpp>
27
28namespace QuantExt {
29using namespace QuantLib;
30
31/*! Constant deterministic loss amount default latent model, extended to cover a discrete
32 distribution of recovery rates following Krekel (2008), https://ssrn.com/abstract=11340228.
33 For each obligor we pass a vector of J recovery probabilities p_1, ..., p_J and
34 recovery rates in decreasing order r_1 > r_2 > ... > r_J contional on default.
35 If this data is empty, the extended model will fall back on the ConstantLossLatentModel.
36*/
37template <class copulaPolicy> class ExtendedConstantLossLatentModel : public DefaultLatentModel<copulaPolicy> {
38private:
39 const std::vector<Real> recoveries_;
40 typedef typename copulaPolicy::initTraits initTraits;
41
42public:
43 ExtendedConstantLossLatentModel(const std::vector<std::vector<Real>>& factorWeights, const std::vector<Real>& recoveries,
44 const std::vector<std::vector<Real>>& recoveryProbabilities,
45 const std::vector<std::vector<Real>>& recoveryRates,
46 LatentModelIntegrationType::LatentModelIntegrationType integralType,
47 const initTraits& ini = initTraits())
48 : DefaultLatentModel<copulaPolicy>(factorWeights, integralType, ini), recoveries_(recoveries),
50 QL_REQUIRE(recoveries.size() == factorWeights.size(), "Incompatible factors and recovery sizes.");
52 }
53
54 ExtendedConstantLossLatentModel(const Handle<Quote>& mktCorrel, const std::vector<Real>& recoveries,
55 const std::vector<std::vector<Real>>& recoveryProbabilities,
56 const std::vector<std::vector<Real>>& recoveryRates,
57 LatentModelIntegrationType::LatentModelIntegrationType integralType, Size nVariables,
58 const initTraits& ini = initTraits())
59 : DefaultLatentModel<copulaPolicy>(mktCorrel, nVariables, integralType, ini), recoveries_(recoveries),
61 // actually one could define the other and get rid of the variable
62 // here and in other similar models
63 QL_REQUIRE(recoveries.size() == nVariables, "Incompatible model and recovery sizes.");
65 }
66
67 // Check vector sizes and that expected recovery matches market quoted recovery for each obligor
69 QL_REQUIRE(recoveryProbabilities_.size() == recoveryRates_.size(), "number of recovery probability vectors and market recovery rates differ");
70 if (recoveryProbabilities_.size() == 0)
71 return;
72 QL_REQUIRE(recoveryProbabilities_.size() == recoveries_.size(), "number of recovery rates and recovery probablity vectors differ");
73 for (Size i = 0; i < recoveries_.size(); ++i) {
74 QL_REQUIRE(recoveryProbabilities_[i].size() == recoveryRates_[i].size(), "recovery and probability vector size mismatch for obligor " << i);
75 Real expectedRecovery = 0.0;
76 for (Size j = 0; j < recoveryProbabilities_[i].size(); ++j)
78 QL_REQUIRE(QuantLib::close_enough(expectedRecovery, recoveries_[i]), "expected recovery does not match market recovery rate for obligor " << i);
79 }
80 }
81
82 Real conditionalRecovery(const Date& d, Size iName, const std::vector<Real>& mktFactors) const {
83 return recoveries_[iName];
84 }
85
86 Real conditionalRecovery(Probability uncondDefP, Size iName, const std::vector<Real>& mktFactors) const {
87 return recoveries_[iName];
88 }
89
90 Real conditionalRecoveryInvP(Real invUncondDefP, Size iName, const std::vector<Real>& mktFactors) const {
91 return recoveries_[iName];
92 }
93
94 Real conditionalRecovery(Real latentVarSample, Size iName, const Date& d) const { return recoveries_[iName]; }
95
96 const std::vector<Real>& recoveries() const { return recoveries_; }
97
98 // this is really an interface to rr models even if not imposed. Default
99 // loss models do have an interface for this one. Enforced only through
100 // duck typing.
101 Real expectedRecovery(const Date& d, Size iName, const DefaultProbKey& defKeys) const { return recoveries_[iName]; }
102
103 const std::vector<std::vector<Real>>& recoveryProbabilities() { return recoveryProbabilities_; }
104 const std::vector<std::vector<Real>>& recoveryRateGrids() { return recoveryRates_; }
105
106private:
107 std::vector<std::vector<Real>> recoveryProbabilities_;
108 std::vector<std::vector<Real>> recoveryRates_;
109};
110
112
113/*! ExtendedConstantLossLatentModel interface for loss models.
114 While it does not provide distribution type losses (e.g. expected tranche
115 losses) because it lacks an integration algorithm it serves to allow
116 pricing of digital type products like NTDs.
117
118 Alternatively fuse with the aboves class.
119*/
120template <class copulaPolicy>
121class ExtendedConstantLossModel : public virtual ExtendedConstantLossLatentModel<copulaPolicy>, public virtual DefaultLossModel {
122public:
123 ExtendedConstantLossModel(const std::vector<std::vector<Real>>& factorWeights, const std::vector<Real>& recoveries,
124 const std::vector<std::vector<Real>>& recoveryProbabilities,
125 const std::vector<std::vector<Real>>& recoveryRates,
126 LatentModelIntegrationType::LatentModelIntegrationType integralType,
127 const typename copulaPolicy::initTraits& ini = copulaPolicy::initTraits())
128 : ExtendedConstantLossLatentModel<copulaPolicy>(factorWeights, recoveries, recoveryProbabilities, recoveryRates, integralType, ini) {}
129
130 ExtendedConstantLossModel(const Handle<Quote>& mktCorrel, const std::vector<Real>& recoveries,
131 const std::vector<std::vector<Real>>& recoveryProbabilities,
132 const std::vector<std::vector<Real>>& recoveryRates,
133 LatentModelIntegrationType::LatentModelIntegrationType integralType, Size nVariables,
134 const typename copulaPolicy::initTraits& ini = copulaPolicy::initTraits())
135 : ExtendedConstantLossLatentModel<copulaPolicy>(mktCorrel, recoveries, recoveryProbabilities, recoveryRates, integralType, nVariables, ini) {}
136
137protected:
138 // Disposable<std::vector<Probability> > probsBeingNthEvent(
139 // Size n, const Date& d) const {
140 // return
141 // ConstantLossLatentmodel<copulaPolicy>::probsBeingNthEvent(n, d);
142 //}
143 Real defaultCorrelation(const Date& d, Size iName, Size jName) const {
145 }
146 Probability probAtLeastNEvents(Size n, const Date& d) const {
148 }
149 Real expectedRecovery(const Date& d, Size iName, const DefaultProbKey& k) const {
151 }
152
153private:
154 virtual void resetModel() {
155 // update the default latent model we derive from
157 }
158};
159
160} // namespace QuantExt
161
162#endif
Real defaultCorrelation(const Date &d, Size iNamei, Size iNamej) const
void resetBasket(const QuantLib::ext::shared_ptr< Basket > basket) const
Probability probAtLeastNEvents(Size n, const Date &date) const
RelinkableHandle< QuantExt::Basket > basket_
Real conditionalRecovery(const Date &d, Size iName, const std::vector< Real > &mktFactors) const
const std::vector< std::vector< Real > > & recoveryRateGrids()
Real expectedRecovery(const Date &d, Size iName, const DefaultProbKey &defKeys) const
const std::vector< std::vector< Real > > & recoveryProbabilities()
Real conditionalRecovery(Real latentVarSample, Size iName, const Date &d) const
Real conditionalRecoveryInvP(Real invUncondDefP, Size iName, const std::vector< Real > &mktFactors) const
ExtendedConstantLossLatentModel(const Handle< Quote > &mktCorrel, const std::vector< Real > &recoveries, const std::vector< std::vector< Real > > &recoveryProbabilities, const std::vector< std::vector< Real > > &recoveryRates, LatentModelIntegrationType::LatentModelIntegrationType integralType, Size nVariables, const initTraits &ini=initTraits())
Real conditionalRecovery(Probability uncondDefP, Size iName, const std::vector< Real > &mktFactors) const
ExtendedConstantLossLatentModel(const std::vector< std::vector< Real > > &factorWeights, const std::vector< Real > &recoveries, const std::vector< std::vector< Real > > &recoveryProbabilities, const std::vector< std::vector< Real > > &recoveryRates, LatentModelIntegrationType::LatentModelIntegrationType integralType, const initTraits &ini=initTraits())
Real defaultCorrelation(const Date &d, Size iName, Size jName) const
Pearsons' default probability correlation.
Real expectedRecovery(const Date &d, Size iName, const DefaultProbKey &k) const
virtual void resetModel()
Concrete models do now any updates/inits they need on basket reset.
ExtendedConstantLossModel(const std::vector< std::vector< Real > > &factorWeights, const std::vector< Real > &recoveries, const std::vector< std::vector< Real > > &recoveryProbabilities, const std::vector< std::vector< Real > > &recoveryRates, LatentModelIntegrationType::LatentModelIntegrationType integralType, const typename copulaPolicy::initTraits &ini=copulaPolicy::initTraits())
Probability probAtLeastNEvents(Size n, const Date &d) const
ExtendedConstantLossModel(const Handle< Quote > &mktCorrel, const std::vector< Real > &recoveries, const std::vector< std::vector< Real > > &recoveryProbabilities, const std::vector< std::vector< Real > > &recoveryRates, LatentModelIntegrationType::LatentModelIntegrationType integralType, Size nVariables, const typename copulaPolicy::initTraits &ini=copulaPolicy::initTraits())
ExtendedConstantLossLatentModel< GaussianCopulaPolicy > ExtendedGaussianConstantLossLM