20#ifndef quantlib_spotlosslatentmodel_hpp
21#define quantlib_spotlosslatentmodel_hpp
41 template <
class copulaPolicy>
67 const std::vector<Real>& recoveries,
73 void resetBasket(
const ext::shared_ptr<Basket>& basket)
const;
75 const std::vector<Real>& mktFactors)
const;
77 const std::vector<Real>& mktFactors)
const;
80 const std::vector<Real>& m)
const;
94 const std::vector<Real>& mktFactors)
const;
96 const std::vector<Real>& mktFactors)
const;
98 Size iName,
const std::vector<Real>& mktFactors)
const;
105 const Date&
d)
const;
117 const std::vector<Real>& mktFactors)
const;
119 const std::vector<Real>& mktFactors)
const;
142 "Incompatible new basket and model sizes.");
149 Size iName,
const std::vector<Real>& mktFactors)
const
151 const ext::shared_ptr<Pool>& pool = basket_->pool();
153 pool->get(pool->names()[iName]).
154 defaultProbability(basket_->defaultKeys()[iName])
155 ->defaultProbability(date);
156 return conditionalDefaultProbability(pDefUncond, iName, mktFactors);
163 Size iName,
const std::vector<Real>& mktFactors)
const
168 #if defined(QL_EXTRA_SAFETY_CHECKS)
169 QL_REQUIRE(basket_,
"No portfolio basket set.");
174 if (prob < 1.e-10)
return 0.;
175 return conditionalDefaultProbabilityInvP(
176 inverseCumulativeY(prob, iName), iName, mktFactors);
184 const std::vector<Real>& m)
const
187 std::inner_product(this->factorWeights_[iName].begin(),
188 this->factorWeights_[iName].end(), m.begin(), 0.);
189 Real res = this->cumulativeZ((invCumYProb - sumMs) /
190 this->idiosyncFctrs_[iName] );
191 #if defined(QL_EXTRA_SAFETY_CHECKS)
193 "conditional probability " << res <<
"out of range");
203 const std::vector<Real>& mktFactors)
const
205 #if defined(QL_EXTRA_SAFETY_CHECKS)
206 QL_REQUIRE(mktFactors.size() == this->numFactors(),
207 "Realization of market factors and latent model size do not match");
209 const ext::shared_ptr<Pool>& pool = basket_->pool();
211 pool->get(pool->names()[iName]).
212 defaultProbability(basket_->defaultKeys()[iName])
213 ->defaultProbability(
d);
215 return expCondRecoveryP(pDefUncond, iName, mktFactors);
220 Real uncondDefP,
Size iName,
const std::vector<Real>& mktFactors)
const
222 return expCondRecoveryInvPinvRR(
223 inverseCumulativeY(uncondDefP, iName),
224 inverseCumulativeY(recoveries_[iName], iName + numNames_),
233 const std::vector<Real>& mktFactors)
const
235 const std::vector<std::vector<Real> >& fctrs_ = factorWeights();
238 std::inner_product(fctrs_[iName].begin(), fctrs_[iName].end(),
239 mktFactors.begin(), 0.);
240 const Real sumBetaLoss =
241 std::inner_product(fctrs_[iName + numNames_].begin(),
242 fctrs_[iName + numNames_].end(),
243 fctrs_[iName + numNames_].begin(),
245 return this->cumulativeZ((sumMs + std::sqrt(1.-crossIdiosyncFctrs_[iName])
246 * std::sqrt(1.+modelA_*modelA_) *
248 - std::sqrt(crossIdiosyncFctrs_[iName]) *
251 / std::sqrt(1.- sumBetaLoss + modelA_*modelA_ *
252 (1.-crossIdiosyncFctrs_[iName])) );
259 const ext::shared_ptr<Pool>& pool = basket_->pool();
263 pool->get(basket_->names()[iName]).defaultProbability(
264 basket_->defaultKeys()[iName]);
265 const Probability pdef = dfts->defaultProbability(
d,
true);
267 if (pdef < 1.e-10)
return 0.;
269 Size iRecovery = iName + numNames_;
271 (latentVarSample - std::sqrt(crossIdiosyncFctrs_[iName])
272 * inverseCumulativeY(pdef, iName)) /
273 (modelA_ * std::sqrt(1.-crossIdiosyncFctrs_[iName]))
276 +std::sqrt(1.+ 1./(modelA_*modelA_)) *
277 inverseCumulativeY(recoveries_[iName], iRecovery)
283 const std::vector<Real>& allFactors,
286 Size iRecovery = iName + numNames_;
287 return latentVarValue(allFactors, iRecovery);
293 const std::vector<Real>& mktFactors)
const
295 const ext::shared_ptr<Pool>& pool = basket_->pool();
297 pool->get(pool->names()[iName]).
298 defaultProbability(basket_->defaultKeys()[iName])
299 ->defaultProbability(
d);
301 Real invP = inverseCumulativeY(pDefUncond, iName);
302 Real invRR = inverseCumulativeY(recoveries_[iName], iName + numNames_);
304 return conditionalExpLossRRInv(invP, invRR, iName, mktFactors);
312 const std::vector<Real>& mktFactors)
const
314 return conditionalDefaultProbabilityInvP(invP, iName, mktFactors)
315 * (1.-this->conditionalRecoveryInvPinvRR(invP, invRR, iName, mktFactors));
322 const ext::shared_ptr<Pool>& pool = basket_->pool();
324 pool->get(pool->names()[iName]).
325 defaultProbability(basket_->defaultKeys()[iName])
326 ->defaultProbability(
d);
328 Real invP = inverseCumulativeY(pDefUncond, iName);
329 Real invRR = inverseCumulativeY(recoveries_[iName], iName + numNames_);
331 return integratedExpectedValue(
332 [&](
const std::vector<Real>&
v){
333 return conditionalExpLossRRInv(invP, invRR, iName,
v);
339 const std::vector<std::vector<Real> >& factorWeights,
340 const std::vector<Real>& recoveries,
343 const typename CP::initTraits& ini
346 recoveries_(recoveries),
348 numNames_(factorWeights.size()/2),
350 createLMIntegration(factorWeights[0].size(), integralType))
353 "Number of RR variables must be equal to number of default variables");
355 "Number of recoveries does not match number of defaultable entities.");
Shared handle to an observable.
Generic multifactor latent variable model.
Real latentVarValue(const std::vector< Real > &allFactors, Size iVar) const
Real inverseCumulativeY(Probability p, Size iVariable) const
Probability cumulativeY(Real val, Size iVariable) const
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
Random spot recovery rate latent variable portfolio model.
Real conditionalExpLossRR(const Date &d, Size iName, const std::vector< Real > &mktFactors) const
ext::shared_ptr< Basket > basket_
SpotRecoveryLatentModel(const std::vector< std::vector< Real > > &factorWeights, const std::vector< Real > &recoveries, Real modelA, LatentModelIntegrationType::LatentModelIntegrationType integralType, const initTraits &ini=initTraits())
Real expectedLoss(const Date &d, Size iName) const
Probability conditionalDefaultProbability(const Date &date, Size iName, const std::vector< Real > &mktFactors) const
Real expCondRecoveryP(Real uncondDefP, Size iName, const std::vector< Real > &mktFactors) const
void resetBasket(const ext::shared_ptr< Basket > &basket) const
Real expCondRecovery(const Date &d, Size iName, const std::vector< Real > &mktFactors) const
const std::vector< Real > recoveries_
Real conditionalRecovery(Real latentVarSample, Size iName, const Date &d) const
Real latentRRVarValue(const std::vector< Real > &allFactors, Size iName) const
const ext::shared_ptr< LMIntegration > & integration() const override
access to integration:
ext::shared_ptr< LMIntegration > integration_
Real expCondRecoveryInvPinvRR(Real invUncondDefP, Real invUncondRR, Size iName, const std::vector< Real > &mktFactors) const
Real conditionalExpLossRRInv(Real invP, Real invRR, Size iName, const std::vector< Real > &mktFactors) const
copulaPolicy::initTraits initTraits
std::vector< Real > crossIdiosyncFctrs_
Probability conditionalDefaultProbabilityInvP(Real invCumYProb, Size iName, const std::vector< Real > &m) 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
LatentModelIntegrationType
SpotRecoveryLatentModel< TCopulaPolicy > TSpotLossLM
SpotRecoveryLatentModel< GaussianCopulaPolicy > GaussianSpotLossLM
ext::shared_ptr< BlackVolTermStructure > v