/usr/include/ql/experimental/credit/spotlosslatentmodel.hpp is in libquantlib0-dev 1.7.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2014 Jose Aparicio
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef quantlib_spotlosslatentmodel_hpp
#define quantlib_spotlosslatentmodel_hpp
#include <ql/experimental/credit/defaultprobabilitylatentmodel.hpp>
namespace QuantLib {
/*! \brief Random spot recovery rate latent variable portfolio model.\par
See: \par
<b>A Spot Stochastic Recovery Extension of the Gaussian Copula</b> N.Bennani
and J.Maetz, MPRA July 2009 \par
<b>Extension of Spot Recovery model for Gaussian Copula</b> H.Li, October
2009, MPRA \par
The model is adpated here for a multifactor set up and a generic copula so
it can be used for pricing in single factor mode or for risk metrics in its
multifactor version.\par
\todo Rewrite this model: the distribution of the spot recovery given
default could be given as a functional of rr_i with the market factors and
the rest of methods depend on this. That would offer a family of models.
\todo Implement eq. 45 to have the EL(t) and be able to integrate the model
*/
template <class copulaPolicy>
class SpotRecoveryLatentModel : public LatentModel<copulaPolicy> {
public:
// resolve LM interface:
using LatentModel<copulaPolicy>::factorWeights;
using LatentModel<copulaPolicy>::inverseCumulativeY;
using LatentModel<copulaPolicy>::cumulativeY;
using LatentModel<copulaPolicy>::latentVarValue;
using LatentModel<copulaPolicy>::integratedExpectedValue;
private:
const std::vector<Real> recoveries_;
const Real modelA_;
// products of default and recoveries factors, see refs ('covariances')
std::vector<Real> crossIdiosyncFctrs_;
mutable Size numNames_;
mutable boost::shared_ptr<Basket> basket_;
boost::shared_ptr<LMIntegration> integration_;
protected:
//! access to integration:
const boost::shared_ptr<LMIntegration>&
integration() const { return integration_; }
private:
typedef typename copulaPolicy::initTraits initTraits;
public:
SpotRecoveryLatentModel(
const std::vector<std::vector<Real> >& factorWeights,
const std::vector<Real>& recoveries,
Real modelA,
LatentModelIntegrationType::LatentModelIntegrationType integralType,
const initTraits& ini = initTraits()
);
void resetBasket(const boost::shared_ptr<Basket> basket) const;
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;
Probability conditionalDefaultProbabilityInvP(Real invCumYProb,
Size iName,
const std::vector<Real>& m) const;
/*! Expected conditional spot recovery rate. Conditional on a set of
systemic factors and default returns the integrated attainable recovery
values. \par
Corresponds to a multifactor generalization of the model in eq. 44
on p.15 of <b>Extension of Spot Recovery Model for Gaussian Copula</b>
Hui Li. 2009 Only remember that \f$\rho_l Z \f$ there is here
(multiple betas):
\f$ \sum_k \beta_{ik}^l Z_k \f$ and that \f$ \rho_d \rho_l \f$ there is
here:
\f$ \sum_k \beta_{ik}^d \beta_{ik}^l \f$ \par
(d,l corresponds to first and last set of betas)
*/
Real expCondRecovery/*conditionalRecovery*/(const Date& d, Size iName,
const std::vector<Real>& mktFactors) const;
Real expCondRecoveryP(Real uncondDefP, Size iName,
const std::vector<Real>& mktFactors) const;
Real expCondRecoveryInvPinvRR(Real invUncondDefP, Real invUncondRR,
Size iName, const std::vector<Real>& mktFactors) const;
/*! Implements equation 42 on p.14 (second).
Remember that for this call to make sense the sample used must be
one leading to a default. Theres no check on this. This member
typically to be used within a simulation.
*/
Real conditionalRecovery(Real latentVarSample, Size iName,
const Date& d) const;
/*! Due to the way the latent model is splitted in two parts, we call
the base class for the default sample and the LM owned here for the RR
model sample. This sample only makes sense if it led to a default.
@param allFactors All sampled factors, default and RR valiables.
@param iName The index of the name for which we want the RR sample
\todo Write vector version for all names' RRs
*/
Real latentRRVarValue(const std::vector<Real>& allFactors,
Size iName) const;
Real conditionalExpLossRR(const Date& d, Size iName,
const std::vector<Real>& mktFactors) const;
Real conditionalExpLossRRInv(Real invP, Real invRR, Size iName,
const std::vector<Real>& mktFactors) const;
/*! Single name expected loss.\par
The main reason of this method is for the testing of this model. The
model is coherent in that it preserves the single name expected loss
and thus is coherent with the single name CDS market when used in the
pricing context. i.e. it should match: \f$pdef_i(d) \times RR_i \f$
*/
Real expectedLoss(const Date& d, Size iName) const;
};
typedef SpotRecoveryLatentModel<GaussianCopulaPolicy> GaussianSpotLossLM;
typedef SpotRecoveryLatentModel<TCopulaPolicy> TSpotLossLM;
// ------------------------------------------------------------------------
template<class CP>
inline void SpotRecoveryLatentModel<CP>::resetBasket(
const boost::shared_ptr<Basket> basket) const
{
basket_ = basket;
// in the future change 'size' to 'liveSize'
QL_REQUIRE(basket_->size() == numNames_,
"Incompatible new basket and model sizes.");
}
template<class CP>
inline Probability
SpotRecoveryLatentModel<CP>::conditionalDefaultProbability(
const Date& date,
Size iName, const std::vector<Real>& mktFactors) const
{
const boost::shared_ptr<Pool>& pool = basket_->pool();
Probability pDefUncond =
pool->get(pool->names()[iName]).
defaultProbability(basket_->defaultKeys()[iName])
->defaultProbability(date);
return conditionalDefaultProbability(pDefUncond, iName, mktFactors);
}
template<class CP>
inline Probability
SpotRecoveryLatentModel<CP>::conditionalDefaultProbability(
Probability prob,
Size iName, const std::vector<Real>& mktFactors) const
{
// we can be called from the outside (from an integrable loss model)
// but we are called often at integration points. This or
// consider a list of friends.
#if defined(QL_EXTRA_SAFETY_CHECKS)
QL_REQUIRE(basket_, "No portfolio basket set.");
#endif
/*Avoid redundant call to minimum value inversion (might be \infty),
and this independently of the copula function.
*/
if (prob < 1.e-10) return 0.;// use library macro...
return conditionalDefaultProbabilityInvP(
inverseCumulativeY(prob, iName), iName, mktFactors);
}
template<class CP>
inline Probability
SpotRecoveryLatentModel<CP>::conditionalDefaultProbabilityInvP(
Real invCumYProb,
Size iName,
const std::vector<Real>& m) const
{
Real sumMs =
std::inner_product(this->factorWeights_[iName].begin(),
this->factorWeights_[iName].end(), m.begin(), 0.);
Real res = this->cumulativeZ((invCumYProb - sumMs) /
this->idiosyncFctrs_[iName] );
#if defined(QL_EXTRA_SAFETY_CHECKS)
QL_REQUIRE (res >= 0. && res <= 1.,
"conditional probability " << res << "out of range");
#endif
return res;
}
template<class CP>
inline Real
SpotRecoveryLatentModel<CP>::expCondRecovery(const Date& d,
Size iName,
const std::vector<Real>& mktFactors) const
{
#if defined(QL_EXTRA_SAFETY_CHECKS)
QL_REQUIRE(mktFactors.size() == this->numFactors(),
"Realization of market factors and latent model size do not match");
#endif
const boost::shared_ptr<Pool>& pool = basket_->pool();
Probability pDefUncond =
pool->get(pool->names()[iName]).
defaultProbability(basket_->defaultKeys()[iName])
->defaultProbability(d);
return expCondRecoveryP(pDefUncond, iName, mktFactors);
}
template<class CP>
inline Real SpotRecoveryLatentModel<CP>::expCondRecoveryP(
Real uncondDefP, Size iName, const std::vector<Real>& mktFactors) const
{
return expCondRecoveryInvPinvRR(
inverseCumulativeY(uncondDefP, iName),
inverseCumulativeY(recoveries_[iName], iName + numNames_),
iName, mktFactors);
}
template<class CP>
Real SpotRecoveryLatentModel<CP>::expCondRecoveryInvPinvRR(
Real invUncondDefP,
Real invUncondRR,
Size iName,
const std::vector<Real>& mktFactors) const
{
const std::vector<std::vector<Real> >& fctrs_ = factorWeights();
//Size iRR = iName + basket_->size();// should be live pool
const Real sumMs =
std::inner_product(fctrs_[iName].begin(), fctrs_[iName].end(),
mktFactors.begin(), 0.);
const Real sumBetaLoss =
std::inner_product(fctrs_[iName + numNames_].begin(),
fctrs_[iName + numNames_].end(),
fctrs_[iName + numNames_].begin(),
0.);
return this->cumulativeZ((sumMs + std::sqrt(1.-crossIdiosyncFctrs_[iName])
* std::sqrt(1.+modelA_*modelA_) *
invUncondRR
- std::sqrt(crossIdiosyncFctrs_[iName]) *
invUncondDefP
)
/ std::sqrt(1.- sumBetaLoss + modelA_*modelA_ *
(1.-crossIdiosyncFctrs_[iName])) );
}
template<class CP>
Real SpotRecoveryLatentModel<CP>::conditionalRecovery(Real latentVarSample,
Size iName, const Date& d) const
{
const boost::shared_ptr<Pool>& pool = basket_->pool();
// retrieve the default probability for this name
const Handle<DefaultProbabilityTermStructure>& dfts =
pool->get(basket_->names()[iName]).defaultProbability(
basket_->defaultKeys()[iName]);
const Probability pdef = dfts->defaultProbability(d, true);
// before asking for -\infty
if (pdef < 1.e-10) return 0.;
Size iRecovery = iName + numNames_;// should be live pool
return cumulativeY(
(latentVarSample - std::sqrt(crossIdiosyncFctrs_[iName])
* inverseCumulativeY(pdef, iName)) /
(modelA_ * std::sqrt(1.-crossIdiosyncFctrs_[iName]))
// cache the sqrts
// cache this factor.
+std::sqrt(1.+ 1./(modelA_*modelA_)) *
inverseCumulativeY(recoveries_[iName], iRecovery)
, iRecovery);
}
template<class CP>
inline Real SpotRecoveryLatentModel<CP>::latentRRVarValue(
const std::vector<Real>& allFactors,
Size iName) const
{
Size iRecovery = iName + numNames_;// should be live pool
return latentVarValue(allFactors, iRecovery);
}
template<class CP>
inline Real SpotRecoveryLatentModel<CP>::conditionalExpLossRR(const Date& d,
Size iName,
const std::vector<Real>& mktFactors) const
{
const boost::shared_ptr<Pool>& pool = basket_->pool();
Probability pDefUncond =
pool->get(pool->names()[iName]).
defaultProbability(basket_->defaultKeys()[iName])
->defaultProbability(d);
Real invP = inverseCumulativeY(pDefUncond, iName);
Real invRR = inverseCumulativeY(recoveries_[iName], iName + numNames_);
return conditionalExpLossRRInv(invP, invRR, iName, mktFactors);
}
template<class CP>
inline Real SpotRecoveryLatentModel<CP>::conditionalExpLossRRInv(
Real invP,
Real invRR,
Size iName,
const std::vector<Real>& mktFactors) const
{
return conditionalDefaultProbabilityInvP(invP, iName, mktFactors)
* (1.-this->conditionalRecoveryInvPinvRR(invP, invRR, iName, mktFactors));
}
template<class CP>
inline Real SpotRecoveryLatentModel<CP>::expectedLoss(const Date& d,
Size iName) const
{
const boost::shared_ptr<Pool>& pool = basket_->pool();
Probability pDefUncond =
pool->get(pool->names()[iName]).
defaultProbability(basket_->defaultKeys()[iName])
->defaultProbability(d);
Real invP = inverseCumulativeY(pDefUncond, iName);
Real invRR = inverseCumulativeY(recoveries_[iName], iName + numNames_);
return integratedExpectedValue(
boost::function<Real (const std::vector<Real>& v1)>(
boost::bind(
&SpotRecoveryLatentModel<CP>::conditionalExpLossRRInv,
this,
invP,
invRR,
iName,
_1)
));
}
template<class CP>
SpotRecoveryLatentModel<CP>::SpotRecoveryLatentModel(
const std::vector<std::vector<Real> >& factorWeights,
const std::vector<Real>& recoveries,
Real modelA,
LatentModelIntegrationType::LatentModelIntegrationType integralType,
const typename CP::initTraits& ini
)
: LatentModel<CP>(factorWeights, ini),
recoveries_(recoveries),
modelA_(modelA),
numNames_(factorWeights.size()/2),
integration_(LatentModel<CP>::IntegrationFactory::
createLMIntegration(factorWeights[0].size(), integralType))
{
QL_REQUIRE(factorWeights.size() % 2 == 0,
"Number of RR variables must be equal to number of default variables");
QL_REQUIRE(recoveries.size() == numNames_ ,
"Number of recoveries does not match number of defaultable entities.");
// reminder: first betas are default, last ones are recovery
for(Size iName=0; iName<numNames_; iName++) /// USE STL
/* Corresponds to: (k denotes factor, i denotes modelled
variable -default and recoveries))
\sum_k a^2_{i,k} a^2_{N+i,k}
*/
{
Real cumul = 0.;
for(Size iB=0; iB<factorWeights[iName].size(); iB++)
// actually this size is unique
cumul += factorWeights[iName][iB] *
factorWeights[iName][iB] *
factorWeights[iName + numNames_][iB] *
factorWeights[iName + numNames_][iB];
crossIdiosyncFctrs_.push_back(cumul);
}
}
}
#endif
|