Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
defaultprobabilitylatentmodel.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_default_latent_model_hpp
20#define quantext_default_latent_model_hpp
21
22//#include <ql/experimental/credit/basket.hpp>
23#include <boost/dynamic_bitset.hpp>
24#include <ql/experimental/math/gaussiancopulapolicy.hpp>
25#include <ql/experimental/math/latentmodel.hpp>
26#include <qle/models/basket.hpp>
27
28namespace QuantExt {
29using namespace QuantLib;
30
31/*! \brief Default event Latent Model.
32
33 This is a model for joint default events based on a generic Latent
34 Model. It models solely the default events in a portfolio, not making any
35 reference to severities, exposures, etc...
36 An implicit correspondence is stablished between the variables modelled and
37 the names in the basket given by the basket and model variable access
38 indices.
39 The class is parametric on the Latent Model copula.
40
41 \todo Consider QL_REQUIRE(basket_, "No portfolio basket set.") test in
42 debug model only for performance reasons.
43*/
44template <class copulaPolicy> class DefaultLatentModel : public LatentModel<copulaPolicy> {
45 // import template members
46protected:
47 using LatentModel<copulaPolicy>::factorWeights_;
48 using LatentModel<copulaPolicy>::idiosyncFctrs_;
49 using LatentModel<copulaPolicy>::copula_;
50
51public:
52 using LatentModel<copulaPolicy>::inverseCumulativeY;
53 using LatentModel<copulaPolicy>::cumulativeZ;
54 using LatentModel<copulaPolicy>::integratedExpectedValue; // which one?
55protected:
56 // not a handle, the model doesnt keep any cached magnitudes, no need
57 // for notifications, still...
58 mutable QuantLib::ext::shared_ptr<Basket> basket_;
59 QuantLib::ext::shared_ptr<LMIntegration> integration_;
60
61private:
62 typedef typename copulaPolicy::initTraits initTraits;
63
64public:
65 /*!
66 @param factorWeights Latent model independent factors weights for each
67 variable.
68 @param integralType Integration type.
69 @param ini Copula initialization if any.
70
71 \warning Baskets with realized defaults not tested/WIP.
72 */
73 DefaultLatentModel(const std::vector<std::vector<Real>>& factorWeights,
74 LatentModelIntegrationType::LatentModelIntegrationType integralType,
75 const initTraits& ini = initTraits())
76 : LatentModel<copulaPolicy>(factorWeights, ini),
77 integration_(LatentModel<copulaPolicy>::IntegrationFactory::createLMIntegration(factorWeights[0].size(),
78 integralType)) {}
79 DefaultLatentModel(const Handle<Quote>& mktCorrel, Size nVariables,
80 LatentModelIntegrationType::LatentModelIntegrationType integralType,
81 const initTraits& ini = initTraits())
82 : LatentModel<copulaPolicy>(mktCorrel, nVariables, ini),
83 integration_(LatentModel<copulaPolicy>::IntegrationFactory::createLMIntegration(1, integralType)) {}
84 /* \todo
85 Add other constructors as in LatentModel for ease of use. (less
86 dimensions, factors, etcc...)
87 */
88
89 /* To interface with loss models. It is possible to change the basket
90 since there are no cached magnitudes.
91 */
92 void resetBasket(const QuantLib::ext::shared_ptr<Basket> basket) const {
93 basket_ = basket;
94 // in the future change 'size' to 'liveSize'
95 QL_REQUIRE(basket_->size() == factorWeights_.size(), "Incompatible new basket and model sizes.");
96 }
97
98public:
99 /*! Returns the probability of default of a given name conditional on
100 the realization of a given set of values of the model independent
101 factors. The date at which the probability is given is implicit in the
102 probability since theres not other time dependence in this model.
103 @param prob Unconditional probability of default.
104 @param iName desired name.
105 @param mktFactors Value of LM independent factors.
106 \warning Most often it is preferred to use the method below avoiding the
107 cumulative inversion.
108 */
109 Probability conditionalDefaultProbability(Probability prob, Size iName, const std::vector<Real>& mktFactors) const {
110// we can be called from the outside (from an integrable loss model)
111// but we are called often at integration points. This or
112// consider a list of friends.
113#if defined(QL_EXTRA_SAFETY_CHECKS)
114 QL_REQUIRE(basket_, "No portfolio basket set.");
115#endif
116 /*Avoid redundant call to minimum value inversion (might be \infty),
117 and this independently of the copula function.
118 */
119 if (prob < 1.e-10)
120 return 0.; // use library macro...
121 return conditionalDefaultProbabilityInvP(inverseCumulativeY(prob, iName), iName, mktFactors);
122 }
123
124protected:
125 void update() override {
126 if (basket_)
127 basket_->notifyObservers();
129 }
130
131public: // open since users access it for performance on joint integrations.
132 /*! Returns the probability of default of a given name conditional on
133 the realization of a given set of values of the model independent
134 factors. The date at which the probability is given is implicit in the
135 probability since theres not other time dependent in this model.
136 Same intention as above but provides a performance opportunity, if the
137 integration is along the market factors (as usually is) avoids computing
138 the inverse of the probability on each call.
139 @param invCumYProb Inverse cumul of the unconditional probability of
140 default, has to follow the same copula law for results to be coherent
141 @param iName desired name.
142 @param m Value of LM independent factors.
143 */
144 Probability conditionalDefaultProbabilityInvP(Real invCumYProb, Size iName, const std::vector<Real>& m) const {
145 Real sumMs = std::inner_product(factorWeights_[iName].begin(), factorWeights_[iName].end(), m.begin(), 0.);
146 Real res = cumulativeZ((invCumYProb - sumMs) / idiosyncFctrs_[iName]);
147#if defined(QL_EXTRA_SAFETY_CHECKS)
148 QL_REQUIRE(res >= 0. && res <= 1., "conditional probability " << res << "out of range");
149#endif
150
151 return res;
152 }
153
154protected:
155 /*! Returns the probability of default of a given name conditional on
156 the realization of a given set of values of the model independent
157 factors.
158 @param date The date for the probability of default.
159 @param iName desired name.
160 @param mktFactors Value of LM independent factors.
161
162 Same intention as the above methods. Usage of this one is typically more
163 expensive because most often the date we call this method with
164 repeats itself and with this one the probability can not be cached
165 outside the call.
166 */
167 Probability conditionalDefaultProbability(const Date& date, Size iName, const std::vector<Real>& mktFactors) const {
168 const QuantLib::ext::shared_ptr<Pool>& pool = basket_->pool();
169 Probability pDefUncond =
170 pool->get(pool->names()[iName]).defaultProbability(basket_->defaultKeys()[iName])->defaultProbability(date);
171 return conditionalDefaultProbability(pDefUncond, iName, mktFactors);
172 }
173 /*! Conditional default probability product, intermediate step in the
174 correlation calculation.*/
175 Probability condProbProduct(Real invCumYProb1, Real invCumYProb2, Size iName1, Size iName2,
176 const std::vector<Real>& mktFactors) const {
177 return conditionalDefaultProbabilityInvP(invCumYProb1, iName1, mktFactors) *
178 conditionalDefaultProbabilityInvP(invCumYProb2, iName2, mktFactors);
179 }
180 //! Conditional probability of n default events or more.
181 // \todo: check the issuer has not defaulted.
182 Real conditionalProbAtLeastNEvents(Size n, const Date& date, const std::vector<Real>& mktFactors) const;
183 //! access to integration:
184 const QuantLib::ext::shared_ptr<LMIntegration>& integration() const override { return integration_; }
185
186public:
187 /*! Computes the unconditional probability of default of a given name.
188 Trivial method for testing
189 */
190 Probability probOfDefault(Size iName, const Date& d) const {
191 QL_REQUIRE(basket_, "No portfolio basket set.");
192 const QuantLib::ext::shared_ptr<Pool>& pool = basket_->pool();
193 // avoid repeating this in the integration:
194 Probability pUncond =
195 pool->get(pool->names()[iName]).defaultProbability(basket_->defaultKeys()[iName])->defaultProbability(d);
196 if (pUncond < 1.e-10)
197 return 0.;
198
199 return integratedExpectedValue(QuantLib::ext::function<Real(const std::vector<Real>& v1)>(
201 inverseCumulativeY(pUncond, iName), iName, QuantLib::ext::placeholders::_1)));
202 }
203 /*! Pearsons' default probability correlation.
204 Users should consider specialization on the copula type for specific
205 distributions since that might simplify the integrations, most
206 importantly if this is to be used in calibration of observations for
207 factor coefficients as it is expensive to integrate directly.
208 */
209 Real defaultCorrelation(const Date& d, Size iNamei, Size iNamej) const;
210
211 /*! Returns the probaility of having a given or larger number of
212 defaults in the basket portfolio at a given time.
213 */
214 Probability probAtLeastNEvents(Size n, const Date& date) const {
215 return integratedExpectedValue(QuantLib::ext::function<Real(const std::vector<Real>& v1)>(QuantLib::ext::bind(
216 &DefaultLatentModel<copulaPolicy>::conditionalProbAtLeastNEvents, this, n, QuantLib::ext::cref(date), QuantLib::ext::placeholders::_1)));
217 }
218};
219
220//---- Defines -----------------------------------------------------------
221
222template <class CP> Real DefaultLatentModel<CP>::defaultCorrelation(const Date& d, Size iNamei, Size iNamej) const {
223 QL_REQUIRE(basket_, "No portfolio basket set.");
224
225 const QuantLib::ext::shared_ptr<Pool>& pool = basket_->pool();
226 // unconditionals:
227 Probability pi =
228 pool->get(pool->names()[iNamei]).defaultProbability(basket_->defaultKeys()[iNamei])->defaultProbability(d);
229 Probability pj =
230 pool->get(pool->names()[iNamej]).defaultProbability(basket_->defaultKeys()[iNamej])->defaultProbability(d);
231 Real pipj = pi * pj;
232 Real invPi = inverseCumulativeY(pi, iNamei);
233 Real invPj = inverseCumulativeY(pj, iNamej);
234 // avoid repetitive calls when i=j?
235 Real E1i1j; // joint default covariance term
236 if (iNamei != iNamej) {
237 E1i1j = integratedExpectedValue(QuantLib::ext::function<Real(const std::vector<Real>& v1)>(
238 QuantLib::ext::bind(&DefaultLatentModel<CP>::condProbProduct, this, invPi, invPj, iNamei, iNamej, QuantLib::ext::placeholders::_1)));
239 } else {
240 E1i1j = pi;
241 }
242 return (E1i1j - pipj) / std::sqrt(pipj * (1. - pi) * (1. - pj));
243}
244
245template <class CP>
247 const std::vector<Real>& mktFactors) const {
248 QL_REQUIRE(basket_, "No portfolio basket set.");
249
250 /* \todo
251 This algorithm traverses all permutations starting form the
252 lowest one. This is inneficient, there shouldnt be any need to
253 go through the invalid ones. Use combinations of n elements.
254
255 See integration in O'Kane for homogeneous ntds.
256 */
257 // first position with as many defaults as desired:
258 Size poolSize = basket_->size(); // move to 'livesize'
259 const QuantLib::ext::shared_ptr<Pool>& pool = basket_->pool();
260
261 BigNatural limit = static_cast<BigNatural>(std::pow(2., (int)(poolSize)));
262
263 // Precalc conditional probabilities
264 std::vector<Probability> pDefCond;
265 for (Size i = 0; i < poolSize; i++)
266 pDefCond.push_back(conditionalDefaultProbability(
267 pool->get(pool->names()[i]).defaultProbability(basket_->defaultKeys()[i])->defaultProbability(date), i,
268 mktFactors));
269
270 Probability probNEventsOrMore = 0.;
271 for (BigNatural mask = static_cast<BigNatural>(std::pow(2., (int)(n)) - 1); mask < limit; mask++) {
272 // cheap permutations
273 boost::dynamic_bitset<> bsetMask(poolSize, mask);
274 if (bsetMask.count() >= n) {
275 Probability pConfig = 1;
276 for (Size i = 0; i < bsetMask.size(); i++)
277 pConfig *= (bsetMask[i] ? pDefCond[i] : (1. - pDefCond[i]));
278 probNEventsOrMore += pConfig;
279 }
280 }
281 return probNEventsOrMore;
282}
283
284// often used:
287} // namespace QuantExt
288
289#endif
basket of issuers and related notionals
DefaultLatentModel(const std::vector< std::vector< Real > > &factorWeights, LatentModelIntegrationType::LatentModelIntegrationType integralType, const initTraits &ini=initTraits())
Probability condProbProduct(Real invCumYProb1, Real invCumYProb2, Size iName1, Size iName2, const std::vector< Real > &mktFactors) const
QuantLib::ext::shared_ptr< LMIntegration > integration_
QuantLib::ext::shared_ptr< Basket > basket_
Probability conditionalDefaultProbability(const Date &date, Size iName, const std::vector< Real > &mktFactors) const
Probability conditionalDefaultProbability(Probability prob, Size iName, const std::vector< Real > &mktFactors) const
DefaultLatentModel(const Handle< Quote > &mktCorrel, Size nVariables, LatentModelIntegrationType::LatentModelIntegrationType integralType, const initTraits &ini=initTraits())
Real defaultCorrelation(const Date &d, Size iNamei, Size iNamej) const
Real conditionalProbAtLeastNEvents(Size n, const Date &date, const std::vector< Real > &mktFactors) const
Conditional probability of n default events or more.
const QuantLib::ext::shared_ptr< LMIntegration > & integration() const override
access to integration:
Probability probOfDefault(Size iName, const Date &d) const
void resetBasket(const QuantLib::ext::shared_ptr< Basket > basket) const
Probability probAtLeastNEvents(Size n, const Date &date) const
Probability conditionalDefaultProbabilityInvP(Real invCumYProb, Size iName, const std::vector< Real > &m) const
DefaultLatentModel< GaussianCopulaPolicy > GaussianDefProbLM
DefaultLatentModel< TCopulaPolicy > TDefProbLM