Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | Private Types | List of all members
DefaultLatentModel< copulaPolicy > Class Template Reference

Default event Latent Model. More...

#include <qle/models/defaultprobabilitylatentmodel.hpp>

+ Inheritance diagram for DefaultLatentModel< copulaPolicy >:
+ Collaboration diagram for DefaultLatentModel< copulaPolicy >:

Public Member Functions

 DefaultLatentModel (const std::vector< std::vector< Real > > &factorWeights, LatentModelIntegrationType::LatentModelIntegrationType integralType, const initTraits &ini=initTraits())
 
 DefaultLatentModel (const Handle< Quote > &mktCorrel, Size nVariables, LatentModelIntegrationType::LatentModelIntegrationType integralType, const initTraits &ini=initTraits())
 
void resetBasket (const QuantLib::ext::shared_ptr< Basket > basket) const
 
Probability conditionalDefaultProbability (Probability prob, Size iName, const std::vector< Real > &mktFactors) const
 
Probability conditionalDefaultProbabilityInvP (Real invCumYProb, Size iName, const std::vector< Real > &m) const
 
Probability probOfDefault (Size iName, const Date &d) const
 
Real defaultCorrelation (const Date &d, Size iNamei, Size iNamej) const
 
Probability probAtLeastNEvents (Size n, const Date &date) const
 

Protected Member Functions

void update () override
 
Probability conditionalDefaultProbability (const Date &date, Size iName, const std::vector< Real > &mktFactors) const
 
Probability condProbProduct (Real invCumYProb1, Real invCumYProb2, Size iName1, Size iName2, const std::vector< Real > &mktFactors) const
 
Real conditionalProbAtLeastNEvents (Size n, const Date &date, const std::vector< Real > &mktFactors) const
 Conditional probability of n default events or more. More...
 
const QuantLib::ext::shared_ptr< LMIntegration > & integration () const override
 access to integration: More...
 

Protected Attributes

QuantLib::ext::shared_ptr< Basketbasket_
 
QuantLib::ext::shared_ptr< LMIntegration > integration_
 

Private Types

typedef copulaPolicy::initTraits initTraits
 

Detailed Description

template<class copulaPolicy>
class QuantExt::DefaultLatentModel< copulaPolicy >

Default event Latent Model.

This is a model for joint default events based on a generic Latent Model. It models solely the default events in a portfolio, not making any reference to severities, exposures, etc... An implicit correspondence is stablished between the variables modelled and the names in the basket given by the basket and model variable access indices. The class is parametric on the Latent Model copula.

Definition at line 44 of file defaultprobabilitylatentmodel.hpp.

Member Typedef Documentation

◆ initTraits

typedef copulaPolicy::initTraits initTraits
private

Definition at line 62 of file defaultprobabilitylatentmodel.hpp.

Constructor & Destructor Documentation

◆ DefaultLatentModel() [1/2]

DefaultLatentModel ( const std::vector< std::vector< Real > > &  factorWeights,
LatentModelIntegrationType::LatentModelIntegrationType  integralType,
const initTraits ini = initTraits() 
)
Parameters
factorWeightsLatent model independent factors weights for each variable.
integralTypeIntegration type.
iniCopula initialization if any.
Warning:
Baskets with realized defaults not tested/WIP.

Definition at line 73 of file defaultprobabilitylatentmodel.hpp.

76 : LatentModel<copulaPolicy>(factorWeights, ini),
78 integralType)) {}
QuantLib::ext::shared_ptr< LMIntegration > integration_

◆ DefaultLatentModel() [2/2]

DefaultLatentModel ( const Handle< Quote > &  mktCorrel,
Size  nVariables,
LatentModelIntegrationType::LatentModelIntegrationType  integralType,
const initTraits ini = initTraits() 
)

Definition at line 79 of file defaultprobabilitylatentmodel.hpp.

Member Function Documentation

◆ resetBasket()

void resetBasket ( const QuantLib::ext::shared_ptr< Basket basket) const

Definition at line 92 of file defaultprobabilitylatentmodel.hpp.

92 {
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 }
QuantLib::ext::shared_ptr< Basket > basket_
+ Here is the caller graph for this function:

◆ conditionalDefaultProbability() [1/2]

Probability conditionalDefaultProbability ( Probability  prob,
Size  iName,
const std::vector< Real > &  mktFactors 
) const

Returns the probability of default of a given name conditional on the realization of a given set of values of the model independent factors. The date at which the probability is given is implicit in the probability since theres not other time dependence in this model.

Parameters
probUnconditional probability of default.
iNamedesired name.
mktFactorsValue of LM independent factors.
Warning:
Most often it is preferred to use the method below avoiding the cumulative inversion.

Definition at line 109 of file defaultprobabilitylatentmodel.hpp.

109 {
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 }
Probability conditionalDefaultProbabilityInvP(Real invCumYProb, Size iName, const std::vector< Real > &m) const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ update()

void update ( )
overrideprotected

Definition at line 125 of file defaultprobabilitylatentmodel.hpp.

125 {
126 if (basket_)
127 basket_->notifyObservers();
129 }

◆ conditionalDefaultProbabilityInvP()

Probability conditionalDefaultProbabilityInvP ( Real  invCumYProb,
Size  iName,
const std::vector< Real > &  m 
) const

Returns the probability of default of a given name conditional on the realization of a given set of values of the model independent factors. The date at which the probability is given is implicit in the probability since theres not other time dependent in this model. Same intention as above but provides a performance opportunity, if the integration is along the market factors (as usually is) avoids computing the inverse of the probability on each call.

Parameters
invCumYProbInverse cumul of the unconditional probability of default, has to follow the same copula law for results to be coherent
iNamedesired name.
mValue of LM independent factors.

Definition at line 144 of file defaultprobabilitylatentmodel.hpp.

144 {
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 }
+ Here is the caller graph for this function:

◆ conditionalDefaultProbability() [2/2]

Probability conditionalDefaultProbability ( const Date &  date,
Size  iName,
const std::vector< Real > &  mktFactors 
) const
protected

Returns the probability of default of a given name conditional on the realization of a given set of values of the model independent factors.

Parameters
dateThe date for the probability of default.
iNamedesired name.
mktFactorsValue of LM independent factors.

Same intention as the above methods. Usage of this one is typically more expensive because most often the date we call this method with repeats itself and with this one the probability can not be cached outside the call.

Definition at line 167 of file defaultprobabilitylatentmodel.hpp.

167 {
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 }
Probability conditionalDefaultProbability(Probability prob, Size iName, const std::vector< Real > &mktFactors) const
+ Here is the call graph for this function:

◆ condProbProduct()

Probability condProbProduct ( Real  invCumYProb1,
Real  invCumYProb2,
Size  iName1,
Size  iName2,
const std::vector< Real > &  mktFactors 
) const
protected

Conditional default probability product, intermediate step in the correlation calculation.

Definition at line 175 of file defaultprobabilitylatentmodel.hpp.

176 {
177 return conditionalDefaultProbabilityInvP(invCumYProb1, iName1, mktFactors) *
178 conditionalDefaultProbabilityInvP(invCumYProb2, iName2, mktFactors);
179 }
+ Here is the call graph for this function:

◆ conditionalProbAtLeastNEvents()

Real conditionalProbAtLeastNEvents ( Size  n,
const Date &  date,
const std::vector< Real > &  mktFactors 
) const
protected

Conditional probability of n default events or more.

Definition at line 246 of file defaultprobabilitylatentmodel.hpp.

247 {
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}

◆ integration()

const QuantLib::ext::shared_ptr< LMIntegration > & integration ( ) const
overrideprotected

access to integration:

Definition at line 184 of file defaultprobabilitylatentmodel.hpp.

184{ return integration_; }

◆ probOfDefault()

Probability probOfDefault ( Size  iName,
const Date &  d 
) const

Computes the unconditional probability of default of a given name. Trivial method for testing

Definition at line 190 of file defaultprobabilitylatentmodel.hpp.

190 {
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 }

◆ defaultCorrelation()

Real defaultCorrelation ( const Date &  d,
Size  iNamei,
Size  iNamej 
) const

Pearsons' default probability correlation. Users should consider specialization on the copula type for specific distributions since that might simplify the integrations, most importantly if this is to be used in calibration of observations for factor coefficients as it is expensive to integrate directly.

Definition at line 222 of file defaultprobabilitylatentmodel.hpp.

222 {
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}
Probability condProbProduct(Real invCumYProb1, Real invCumYProb2, Size iName1, Size iName2, const std::vector< Real > &mktFactors) const
+ Here is the caller graph for this function:

◆ probAtLeastNEvents()

Probability probAtLeastNEvents ( Size  n,
const Date &  date 
) const

Returns the probaility of having a given or larger number of defaults in the basket portfolio at a given time.

Definition at line 214 of file defaultprobabilitylatentmodel.hpp.

214 {
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 }
Real conditionalProbAtLeastNEvents(Size n, const Date &date, const std::vector< Real > &mktFactors) const
Conditional probability of n default events or more.
+ Here is the caller graph for this function:

Member Data Documentation

◆ basket_

QuantLib::ext::shared_ptr<Basket> basket_
mutableprotected

Definition at line 58 of file defaultprobabilitylatentmodel.hpp.

◆ integration_

QuantLib::ext::shared_ptr<LMIntegration> integration_
protected

Definition at line 59 of file defaultprobabilitylatentmodel.hpp.