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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2008 Roland Lichters
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.
*/
#include <ql/experimental/credit/basket.hpp>
#include <ql/experimental/credit/loss.hpp>
#include <ql/time/daycounters/actualactual.hpp>
using namespace std;
namespace QuantLib {
Basket::Basket(const vector<string>& names,
const vector<Real>& notionals,
const boost::shared_ptr<Pool> pool,
const vector<DefaultProbKey>& defaultKeys,
const vector<boost::shared_ptr<RecoveryRateModel> >&
rrModels,
Real attachment,
Real detachment)
: names_(names),
notionals_(notionals),
pool_(pool),
defaultKeys_(defaultKeys),
rrModels_(rrModels),
attachmentRatio_(attachment),
detachmentRatio_(detachment),
basketNotional_(0.0),
basketLGD_(0.0),
trancheNotional_(0.0),
attachmentAmount_(0.0),
detachmentAmount_(0.0),
LGDs_(notionals.size(), 0.0),
scenarioLoss_(names.size(), Loss(0.0, 0.0)) {
QL_REQUIRE(!names_.empty(), "no names given");
QL_REQUIRE(!notionals_.empty(), "notionals empty");
QL_REQUIRE (attachmentRatio_ >= 0 &&
attachmentRatio_ <= detachmentRatio_ &&
detachmentRatio_ <= 1,
"invalid attachment/detachment ratio");
QL_REQUIRE(names_.size() == notionals_.size() &&
notionals_.size() == defaultKeys_.size() &&
defaultKeys_.size() == rrModels_.size(),
"unmatched data entry sizes in basket");
for(Size i=0; i<notionals_.size(); i++)
registerWith(rrModels_[i]);
registerWith(Settings::instance().evaluationDate());
// At this point Issuers in the pool might or might not have
// probability term structures for the defultKeys(eventType+
// currency+seniority) entering in this basket. This is not
// neccessarily a problem.
for (Size i = 0; i < notionals_.size(); i++) {
basketNotional_ += notionals_[i];
attachmentAmount_ += notionals_[i] * attachmentRatio_;
detachmentAmount_ += notionals_[i] * detachmentRatio_;
}
trancheNotional_ = detachmentAmount_ - attachmentAmount_;
}
Size Basket::size() const {
return names_.size();
}
const vector<string>& Basket::names() const {
return names_;
}
const vector<Real>& Basket::notionals() const {
return notionals_;
}
const std::vector<DefaultProbKey>&
Basket::defaultKeys() const {
return defaultKeys_;
}
const std::vector<boost::shared_ptr<RecoveryRateModel> >&
Basket::recoveryModels() const {
return rrModels_;
}
boost::shared_ptr<Pool> Basket::pool() const {
return pool_;
}
const vector<Real>& Basket::LGDs() const {
calculate();
return LGDs_;
}
Real Basket::attachmentRatio() const {
return attachmentRatio_;
}
Real Basket::detachmentRatio() const {
return detachmentRatio_;
}
Real Basket::basketNotional() const {
return basketNotional_;
}
Real Basket::basketLGD() const {
calculate();
return basketLGD_;
}
Real Basket::trancheNotional() const {
return trancheNotional_;
}
Real Basket::attachmentAmount() const {
return attachmentAmount_;
}
Real Basket::detachmentAmount() const {
return detachmentAmount_;
}
vector<Real> Basket::probabilities(const Date& d) const {
vector<Real> prob (names_.size());
for (Size j = 0; j < names_.size(); j++)
prob[j] = pool_->get(names_[j]).defaultProbability(
defaultKeys_[j])->defaultProbability(d);
return prob;
}
Real Basket::cumulatedLoss(const Date& startDate,
const Date& endDate) const {
Real loss = 0.0;
for (Size i = 0; i < names_.size(); i++) {
boost::shared_ptr<DefaultEvent> credEvent =
pool_->get(names_[i]).defaultedBetween(startDate,
endDate,
defaultKeys_[i]);
// to do: adjust for settlement notional accrued convexity, see doc
if (credEvent) {
if(credEvent->hasSettled()) {
loss += notionals_[i] * (1. -
credEvent->settlement().recoveryRate(
defaultKeys_[i].seniority()));
}else{// name defaulted but did not settled/confirm
loss += notionals_[i] * (1. -
rrModels_[i]->recoveryValue(credEvent->date(),
defaultKeys_[i]
));
}
}
}
return loss;
}
Real Basket::remainingNotional(const Date& startDate,
const Date& endDate) const {
Real notional = 0;
for (Size i = 0; i < names_.size(); i++) {
if (!pool_->get(names_[i]).defaultedBetween(startDate,
endDate,
defaultKeys_[i]))
notional += notionals_[i];
}
return notional;
}
vector<Real> Basket::remainingNotionals(const Date& startDate,
const Date& endDate) const {
vector<Real> notionals;
for (Size i = 0; i < names_.size(); i++) {
if (!pool_->get(names_[i]).defaultedBetween(startDate,
endDate,
defaultKeys_[i]))
notionals.push_back(notionals_[i]);
}
return notionals;
}
vector<string> Basket::remainingNames(const Date& startDate,
const Date& endDate) const {
vector<string> names;
for (Size i = 0; i < names_.size(); i++) {
if (!pool_->get(names_[i]).defaultedBetween(startDate,
endDate,
defaultKeys_[i]))
names.push_back(names_[i]);
}
return names;
}
vector<boost::shared_ptr<RecoveryRateModel> >
Basket::remainingRecModels(const Date& startDate,
const Date& endDate) const {
vector<boost::shared_ptr<RecoveryRateModel> > models;
for (Size i = 0; i < names_.size(); i++) {
if (!pool_->get(names_[i]).defaultedBetween(startDate,
endDate,
defaultKeys_[i]))
models.push_back(rrModels_[i]);
}
return models;
}
vector<DefaultProbKey>
Basket::remainingDefaultKeys(const Date& startDate,
const Date& endDate) const {
vector<DefaultProbKey> keys;
for (Size i = 0; i < names_.size(); i++) {
if (!pool_->get(names_[i]).defaultedBetween(startDate,
endDate,
defaultKeys_[i]))
keys.push_back(defaultKeys_[i]);
}
return keys;
}
Real Basket::remainingAttachmentAmount(const Date& startDate,
const Date& endDate) const {
Real loss = cumulatedLoss(startDate, endDate);
return std::max(0.0, attachmentAmount_ - loss);
}
Real Basket::remainingAttachmentRatio(const Date& startDate,
const Date& endDate) const {
return remainingAttachmentAmount(startDate, endDate)
/ remainingNotional(startDate, endDate);
}
Real Basket::remainingDetachmentAmount(const Date& startDate,
const Date& endDate) const {
Real loss = cumulatedLoss(startDate, endDate);
return std::max(0.0, detachmentAmount_ - loss);
}
Real Basket::remainingDetachmentRatio(const Date& startDate,
const Date& endDate) const {
return remainingDetachmentAmount(startDate, endDate)
/ remainingNotional(startDate, endDate);
}
void Basket::updateScenarioLoss(bool zeroRecovery) {
calculate();
for (Size i = 0; i < names_.size(); i++) {
if (zeroRecovery)
scenarioLoss_[i].amount = notionals_[i];
else
scenarioLoss_[i].amount = LGDs_[i];
scenarioLoss_[i].time = pool_->getTime(names_[i]);
}
std::sort(scenarioLoss_.begin(), scenarioLoss_.end());
}
vector<Loss> Basket::scenarioIncrementalBasketLosses() const {
return scenarioLoss_;
}
Real Basket::scenarioTrancheLoss(Date endDate) const {
Real A = attachmentAmount_;
Real D = detachmentAmount_;
Date today = Settings::instance().evaluationDate();
Real t2 = ActualActual().yearFraction(today, endDate);
Real L = 0.0;
for (Size i = 0; i < scenarioLoss_.size(); i++) {
if (scenarioLoss_[i].time <= t2)
L += scenarioLoss_[i].amount;
else break;
}
return std::min(L, D) - std::min(L, A);
}
vector<Loss> Basket::scenarioIncrementalTrancheLosses(Date startDate,
Date endDate) const {
vector<Loss> losses;
Real A = attachmentAmount_;
Real D = detachmentAmount_;
Date today = Settings::instance().evaluationDate();
Real tmin = ActualActual().yearFraction(today, startDate);
Real tmax = ActualActual().yearFraction(today, endDate);
Real TL1 = 0.0;
Real L = 0.0;
for (Size i = 0; i < scenarioLoss_.size(); i++) {
Real t = scenarioLoss_[i].time;
if (t > tmax && endDate != Date::maxDate()) break;
if (t < tmin && startDate != Date::minDate()) continue;
L += scenarioLoss_[i].amount;
Real TL2 = std::min(L, D) - std::min(L, A);
Real increment = TL2 - TL1;
TL1 = TL2;
losses.push_back(Loss(t, increment));
}
return losses;
}
void Basket::performCalculations() const {
Date today = Settings::instance().evaluationDate();
for (Size i = 0; i < notionals_.size(); i++) {
//we are registered, the quote might have changed.
QL_REQUIRE(
rrModels_[i]->appliesToSeniority(defaultKeys_[i].seniority()),
"Recovery model does not match basket member seniority.");
LGDs_[i] = notionals_[i]
* (1.0 - rrModels_[i]->recoveryValue(today,
defaultKeys_[i]
));
basketLGD_ += LGDs_[i];
}
}
}
|