/usr/include/ql/experimental/credit/recursivecdoengine.hpp is in libquantlib0-dev 1.1-2build1.
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 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2009 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 recursive_cdo_engine_hpp
#define recursive_cdo_engine_hpp
#include <ql/math/integrals/gaussianquadratures.hpp>
#include <ql/math/distributions/normaldistribution.hpp>
#include <ql/math/matrixutilities/factorreduction.hpp>
#include <ql/experimental/credit/syntheticcdoengines.hpp>
#include <ql/experimental/credit/onefactorgaussiancopula.hpp>
#include <ql/experimental/credit/onefactorstudentcopula.hpp>
#include <boost/bind.hpp>
#include <map>
#include <algorithm>
namespace QuantLib {
/*! Recursive STCDO pricing for a heterogeneous pool of names. The pool
names are heterogeneous in their default probabilities, notionals
and recovery rates. Correlations are pairwise. The recursive pricing
algorithm used here is described in Andersen, Sidenius and Basu;
"All your hedges in one basket", Risk, November 2003, pages 67-72
Notice that using copulas other than Gaussian it is only an
approximation (see remark on p.68).
*/
template <class CDOEngine, class copulaT>
class RecursiveCdoEngine : public CDOEngine {
public:
// Base constructors call default Handle constructor, the copula is to
// be relinked by template partial specializations on the copula type
//! Single correlation construction
RecursiveCdoEngine(const Handle<Quote>& correl,
Size nbuckets = 1,
Size quadOrder = 20)
: correlQuote_(correl), copula_(), nBuckets_(nbuckets),
integral_(quadOrder), wk_()
{
this->registerWith(correl);
}
//! Correlation name to name single factor construction
RecursiveCdoEngine(const Handle<Quote>& correl,
const Matrix& correlMtrx,
Size nbuckets = 1,
Size quadOrder = 20)
: correlQuote_(correl), copula_(), nBuckets_(nbuckets),
integral_(quadOrder), wk_(),
oneFactorCorrels_(factorReduction(correlMtrx))
{
// at least
QL_REQUIRE(!oneFactorCorrels_.empty(),
"Invalid correlation parameter matrix.");
}
protected:
void initialize() const;
private:
//! Weights the conditional portfolio loss by the mkt factor
// distribtion
Real integratorLoss(const Date& date, Real mktFactor) const {
return expectedConditionalLoss(date, mktFactor) *
copula_->density(mktFactor);
}
//! Portfolio loss conditional to the market factor value
Real expectedConditionalLoss(const Date& date,
Real mktFactor) const;
public:
void update();
/* Expected tranche Loss calculation.
This is computed from the first equation on page 70 (not numbered)
Notice that while we want to compute:
\f[
EL(t) = \sum_{l_k}l_k P(l;t) =
\sum_{l_k}l_k \int P(l_k;t|\omega) d\omega q(\omega)
\f]
One can invert the sumation and the integral order to:
\f[
EL(t) = \int\,q(\omega)\,d\omega\,\sum_{l_k}\,l_k\,P(l_k;t|\omega) =
\int\,q(\omega)\,d\omega\,EL(t|\omega)
\f]
and this is the way it is integrated here. The recursion formula makes
it easier this way.
*/
Real expectedTrancheLoss(const Date& date) const {
return
integral_(boost::bind(
&RecursiveCdoEngine<CDOEngine, copulaT>::integratorLoss,
this,
date,
_1)
);
}
protected:
const Handle<Quote> correlQuote_;
mutable RelinkableHandle<copulaT> copula_;
private:
// loss model descriptor members
Size nBuckets_;
const GaussHermiteIntegration integral_;
mutable std::vector<Real> wk_;
mutable Real loss_unit_;
//! name to name factor loadings (betas). In the single factor copula:
// correl = beta * beta
// When constructing through a single correlation number the factor is
// taken to be the positive swuare root of this number in the copula.
mutable std::vector<Real> oneFactorCorrels_;
};
template <class CDOEngine, class copulaT>
void RecursiveCdoEngine<CDOEngine, copulaT>::update() {
oneFactorCorrels_.clear();
CDOEngine::update();
}
template <class CDOEngine, class copulaT>
void RecursiveCdoEngine<CDOEngine, copulaT>::initialize() const {
wk_.clear();
Date today = Settings::instance().evaluationDate();
Date start = this->arguments_.schedule.startDate();
boost::shared_ptr<Basket>& basket = this->arguments_.basket;
/*
Remove defaulted names and adjust the subordination.
*/
std::vector<std::string> names =
basket->remainingNames(start, today);
std::vector<Real> notionals
= basket->remainingNotionals(start, today);
Real a = basket->remainingAttachmentRatio(start, today);
Real d = basket->remainingDetachmentRatio(start, today);
const boost::shared_ptr<Pool> pool = basket->pool();
this->remainingBasket_ =
boost::shared_ptr<Basket>(new Basket(names, notionals, pool,
basket->remainingDefaultKeys(start, today),
basket->remainingRecModels(start, today),
a, d));
this->results_.xMin = this->remainingBasket_->attachmentAmount();
this->results_.xMax = this->remainingBasket_->detachmentAmount();
this->results_.remainingNotional =
this->results_.xMax - this->results_.xMin;
//----------------------------------------------------------------
if(oneFactorCorrels_.empty())
oneFactorCorrels_ = std::vector<Real>(names.size(),
correlQuote_->value());
// check size of factors:
QL_REQUIRE(oneFactorCorrels_.size() == names.size(),
"Size of matrix must match number of names in the basket.");
//
std::vector<Real> lgdsTmp, lgds = this->remainingBasket_->LGDs();
lgdsTmp = lgds;
std::remove(lgds.begin(), lgds.end(), 0.);
loss_unit_ = *(std::min_element(lgds.begin(), lgds.end()))
/ nBuckets_;
for(Size i = 0; i<names.size(); i++)
wk_.push_back(std::floor(lgdsTmp[i]/loss_unit_ + .5));
// Could not check parameters at construction time because we
// had no arguments yet, do it now:
if(oneFactorCorrels_.size() == 1)
oneFactorCorrels_ =
std::vector<Real>(pool->size(), oneFactorCorrels_[0]);
else
QL_REQUIRE(oneFactorCorrels_.size() == this->remainingBasket_->size(),
"Incompatible correl matrix, pool size.");
//----------------------------------------------------------------
const std::vector<Date>& dates = this->arguments_.schedule.dates();
for (Size i = 0; i < dates.size(); i++) {
if (dates[i] <= today)
this->results_.expectedTrancheLoss.push_back(0.0);
else {
Real L = expectedTrancheLoss(dates[i]);
this->results_.expectedTrancheLoss.push_back(L);
}
}
}
//! Portfolio loss conditional to the market factor value
template <class CDOEngine, class copulaT>
Real RecursiveCdoEngine<CDOEngine, copulaT>::expectedConditionalLoss(
const Date& date,
Real mktFactor) const {
const std::vector<std::string>& names = this->remainingBasket_->names();
// eq. 10 p.68
// attainable losses distribution, recursive algorithm
std::vector<Probability> uncDefProb =
this->remainingBasket_->probabilities(date);;
std::map<Real, Probability> pIndepDistrib;
// K=0
pIndepDistrib.insert(std::make_pair(0., 1.));
for(Size iName=0; iName<names.size(); iName++) {
Real nameLoss = wk_[iName];
// to do: allow for matrix constructor and uncoment this
// correlQuote_->setValue(oneFactorCorrels_[iName]);
Probability pDef =
copula_->conditionalProbability(uncDefProb[iName],
mktFactor);
// iterate on all possible losses in the distribution:
std::map<Real, Probability> pDistTemp;
std::map<Real, Probability>::iterator distIt =
pIndepDistrib.begin();
while(distIt != pIndepDistrib.end()) {
// update prob if this name does not default
std::map<Real, Probability>::iterator matchIt
= pDistTemp.find(distIt->first);
if(matchIt != pDistTemp.end()) {
matchIt->second += distIt->second * (1.-pDef);
}else{
pDistTemp.insert(std::make_pair(distIt->first,
distIt->second * (1.-pDef)));
}
// and if it does
matchIt = pDistTemp.find(distIt->first + wk_[iName]);
if(matchIt != pDistTemp.end()) {
matchIt->second += distIt->second * pDef;
}else{
pDistTemp.insert(std::make_pair(
distIt->first+wk_[iName], distIt->second * pDef));
}
distIt++;
}
// copy back
pIndepDistrib = pDistTemp;
}
// get the expected value subject to the value of the market
// factor.
Real expLoss = 0.;
//---------------------------------------------------------------
/* This is the original (easy to read) loop which I have partially
unroll below to take profit of the fact that once we go over
the tranche top the loss amount is fixed:
std::map<Real, Probability>::iterator distIt =
pIndepDistrib.begin();
while(distIt != pIndepDistrib.end()) {
Real loss = distIt->first * loss_unit_
;
loss = std::max(std::min(loss,
results_.xMax)-results_.xMin, 0.);
expLoss += loss * distIt->second;
distIt++;
}
return expLoss ;
*/
//---------------------------------------------------------------
Real relativeMax = this->results_.xMax / loss_unit_;
Real relativeMin = this->results_.xMin / loss_unit_;
Size relativeMaxIdx = std::ceil(relativeMax);
Size relativeMinIdx = std::floor(relativeMin);
std::map<Real, Probability>::iterator
distIt = pIndepDistrib.lower_bound(relativeMinIdx),
itTop = pIndepDistrib.lower_bound(relativeMax);
for(; distIt != itTop; distIt++)
expLoss += std::max(std::min(distIt->first, relativeMax)
-relativeMin, 0.) * distIt->second;
Real sumProbs = 0.;
for(;distIt != pIndepDistrib.end(); distIt++)
sumProbs += distIt->second;
return expLoss * loss_unit_ + this->results_.remainingNotional * sumProbs;
}
// Partial specializations on the copula type. Kind
// of template virtual constructions. Allows to own the correlation
// quote and the copula for each specific copula type. It only
// needs to know its particular copula constructor.
// These and the base correlation pricers use a unifactorial copula
// which needs to be modified for different parameters of the
// correlation per name to name if theres such an structure or
// because the correlation parameter has got a time or/and loss
// level surface.
//
// TO do: Correlation matrix constructors.
//! Specialization for Gaussian copula, the integration still remains
// to be defined by the user out of the available ones in
// syntheticcdoengines.
template <class CDOEngine>
class GaussianRecursiveCdoEngine : public
RecursiveCdoEngine<CDOEngine, OneFactorGaussianCopula> {
public:
//! quote constructor.
GaussianRecursiveCdoEngine(
const Handle<Quote>& correlQuote,
Size nbuckets = 1,
Size quadOrder = 12,
Real maxval = 5.,
Size steps = 50)
:
RecursiveCdoEngine<CDOEngine, OneFactorGaussianCopula>(correlQuote,
nbuckets, quadOrder) {
this->copula_.linkTo(boost::shared_ptr<OneFactorGaussianCopula>(
new OneFactorGaussianCopula(correlQuote, maxval, steps)), true);
}
};
template <class CDOEngine>
class StudentRecursiveCdoEngine : public
RecursiveCdoEngine<CDOEngine, OneFactorStudentCopula> {
public:
//! quote constructor.
StudentRecursiveCdoEngine(
const Handle<Quote>& correlQuote,
Size nz,
Size nm,
Size nbuckets = 1,
Size quadOrder = 12,
Real maxval = 5.,
Size steps = 50)
:
RecursiveCdoEngine<CDOEngine, OneFactorStudentCopula>(correlQuote,
nbuckets, quadOrder) {
this->copula_.linkTo(boost::shared_ptr<OneFactorStudentCopula>(
new OneFactorStudentCopula(correlQuote, nz, nm, maxval, steps)), true);
}
};
typedef GaussianRecursiveCdoEngine<MidPointCDOEngine> GaussRecCDOEngine;
typedef StudentRecursiveCdoEngine<MidPointCDOEngine> StudentRecCDOEngine;
}
#endif
|