19#ifndef quantext_default_latent_model_hpp
20#define quantext_default_latent_model_hpp
23#include <boost/dynamic_bitset.hpp>
24#include <ql/experimental/math/gaussiancopulapolicy.hpp>
25#include <ql/experimental/math/latentmodel.hpp>
52 using LatentModel<copulaPolicy>::inverseCumulativeY;
54 using LatentModel<copulaPolicy>::integratedExpectedValue;
58 mutable QuantLib::ext::shared_ptr<Basket>
basket_;
74 LatentModelIntegrationType::LatentModelIntegrationType integralType,
80 LatentModelIntegrationType::LatentModelIntegrationType integralType,
82 :
LatentModel<copulaPolicy>(mktCorrel, nVariables, ini),
92 void resetBasket(
const QuantLib::ext::shared_ptr<Basket> basket)
const {
95 QL_REQUIRE(
basket_->size() == factorWeights_.size(),
"Incompatible new basket and model sizes.");
113#if defined(QL_EXTRA_SAFETY_CHECKS)
114 QL_REQUIRE(
basket_,
"No portfolio basket set.");
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");
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);
175 Probability
condProbProduct(Real invCumYProb1, Real invCumYProb2, Size iName1, Size iName2,
176 const std::vector<Real>& mktFactors)
const {
191 QL_REQUIRE(
basket_,
"No portfolio basket set.");
192 const QuantLib::ext::shared_ptr<Pool>& pool =
basket_->pool();
194 Probability pUncond =
195 pool->get(pool->names()[iName]).defaultProbability(
basket_->defaultKeys()[iName])->defaultProbability(d);
196 if (pUncond < 1.e-10)
199 return integratedExpectedValue(QuantLib::ext::function<Real(
const std::vector<Real>& v1)>(
201 inverseCumulativeY(pUncond, iName), iName, QuantLib::ext::placeholders::_1)));
215 return integratedExpectedValue(QuantLib::ext::function<Real(
const std::vector<Real>& v1)>(QuantLib::ext::bind(
223 QL_REQUIRE(basket_,
"No portfolio basket set.");
225 const QuantLib::ext::shared_ptr<Pool>& pool = basket_->pool();
228 pool->get(pool->names()[iNamei]).defaultProbability(basket_->defaultKeys()[iNamei])->defaultProbability(d);
230 pool->get(pool->names()[iNamej]).defaultProbability(basket_->defaultKeys()[iNamej])->defaultProbability(d);
232 Real invPi = inverseCumulativeY(pi, iNamei);
233 Real invPj = inverseCumulativeY(pj, iNamej);
236 if (iNamei != iNamej) {
237 E1i1j = integratedExpectedValue(QuantLib::ext::function<Real(
const std::vector<Real>& v1)>(
242 return (E1i1j - pipj) / std::sqrt(pipj * (1. - pi) * (1. - pj));
247 const std::vector<Real>& mktFactors)
const {
248 QL_REQUIRE(basket_,
"No portfolio basket set.");
258 Size poolSize = basket_->size();
259 const QuantLib::ext::shared_ptr<Pool>& pool = basket_->pool();
261 BigNatural limit =
static_cast<BigNatural
>(std::pow(2., (
int)(poolSize)));
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,
270 Probability probNEventsOrMore = 0.;
271 for (BigNatural mask =
static_cast<BigNatural
>(std::pow(2., (
int)(n)) - 1); mask < limit; mask++) {
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;
281 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
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
copulaPolicy::initTraits initTraits
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