20#ifndef quantlib_default_latent_model_hpp
21#define quantlib_default_latent_model_hpp
26#include <boost/dynamic_bitset.hpp>
43 template<
class copulaPolicy>
87 createLMIntegration(1, integralType))
97 void resetBasket(
const ext::shared_ptr<Basket>& basket)
const {
101 "Incompatible new basket and model sizes.");
115 const std::vector<Real>& mktFactors)
const
120 #if defined(QL_EXTRA_SAFETY_CHECKS)
126 if (prob < 1.e-10)
return 0.;
153 const std::vector<Real>& m)
const {
159 #if defined(QL_EXTRA_SAFETY_CHECKS)
161 "conditional probability " << res <<
"out of range");
180 const std::vector<Real>& mktFactors)
const
182 const ext::shared_ptr<Pool>& pool =
basket_->pool();
184 pool->get(pool->names()[iName]).
185 defaultProbability(
basket_->defaultKeys()[iName])
186 ->defaultProbability(date);
193 const std::vector<Real>& mktFactors)
const {
203 const std::vector<Real>& mktFactors)
const;
213 const ext::shared_ptr<Pool>& pool =
basket_->pool();
215 Probability pUncond = pool->get(pool->names()[iName]).
216 defaultProbability(
basket_->defaultKeys()[iName])
217 ->defaultProbability(
d);
218 if (pUncond < 1.e-10)
return 0.;
221 [&](
const std::vector<Real>& v1) {
239 [&](
const std::vector<Real>& v1) {
252 QL_REQUIRE(basket_,
"No portfolio basket set.");
254 const ext::shared_ptr<Pool>& pool = basket_->pool();
257 defaultProbability(basket_->defaultKeys()[iNamei])
258 ->defaultProbability(
d);
260 defaultProbability(basket_->defaultKeys()[iNamej])
261 ->defaultProbability(
d);
263 Real invPi = inverseCumulativeY(pi, iNamei);
264 Real invPj = inverseCumulativeY(pj, iNamej);
267 if(iNamei !=iNamej) {
268 E1i1j = integratedExpectedValue(
269 [&](
const std::vector<Real>& v1) {
270 return condProbProduct(invPi, invPj, iNamei, iNamej, v1); });
274 return (E1i1j - pipj )/std::sqrt(pipj*(1.-pi)*(1.-pj));
281 const std::vector<Real>& mktFactors)
const {
282 QL_REQUIRE(basket_,
"No portfolio basket set.");
292 Size poolSize = basket_->size();
293 const ext::shared_ptr<Pool>& pool = basket_->pool();
295 auto limit =
static_cast<BigNatural>(std::pow(2., (
int)(poolSize)));
298 std::vector<Probability> pDefCond;
299 for(
Size i=0; i<poolSize; i++)
300 pDefCond.push_back(conditionalDefaultProbability(
301 pool->get(pool->names()[i]).
302 defaultProbability(basket_->defaultKeys()[i])->
303 defaultProbability(date), i, mktFactors));
306 for (
auto mask =
static_cast<BigNatural>(std::pow(2., (
int)(
n)) - 1); mask < limit;
309 boost::dynamic_bitset<> bsetMask(poolSize, mask);
310 if(bsetMask.count() >=
n) {
312 for(
Size i=0; i<bsetMask.size(); i++)
314 (bsetMask[i] ? pDefCond[i] : (1.- pDefCond[i]));
315 probNEventsOrMore += pConfig;
318 return probNEventsOrMore;
basket of issuers and related notionals
Default event Latent Model.
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
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())
void resetBasket(const ext::shared_ptr< Basket > &basket) const
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 ext::shared_ptr< LMIntegration > & integration() const override
access to integration:
ext::shared_ptr< LMIntegration > integration_
Probability probOfDefault(Size iName, const Date &d) const
copulaPolicy::initTraits initTraits
Probability probAtLeastNEvents(Size n, const Date &date) const
Probability conditionalDefaultProbabilityInvP(Real invCumYProb, Size iName, const std::vector< Real > &m) const
Shared handle to an observable.
Generic multifactor latent variable model.
Real inverseCumulativeY(Probability p, Size iVariable) const
std::vector< std::vector< Real > > factorWeights_
std::vector< Real > idiosyncFctrs_
Probability cumulativeZ(Real z) const
Cumulative distribution of Z, the idiosyncratic/error factors.
const std::vector< std::vector< Real > > & factorWeights() const
Provides values of the factors .
Real integratedExpectedValue(const ext::function< Real(const std::vector< Real > &v1)> &f) const
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Real Probability
probability
std::size_t Size
size of a container
Generic multifactor latent variable model.
LatentModelIntegrationType
DefaultLatentModel< GaussianCopulaPolicy > GaussianDefProbLM
DefaultLatentModel< TCopulaPolicy > TDefProbLM
unsigned QL_BIG_INTEGER BigNatural
large positive integer