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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2008 Roland Lichters
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/syntheticcdoengines.hpp>
#include <ql/experimental/credit/loss.hpp>
#include <ql/cashflows/fixedratecoupon.hpp>
#include <ql/time/daycounters/actualactual.hpp>
using namespace std;
namespace QuantLib {
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void IntegralCDOEngine::calculate() const {
Date today = Settings::instance().evaluationDate();
const vector<Date>& dates = arguments_.schedule.dates();
results_.protectionValue = 0.0;
results_.premiumValue = 0.0;
results_.upfrontPremiumValue = 0.0;
results_.error = 0;
results_.expectedTrancheLoss.clear();
results_.expectedTrancheLoss.resize(dates.size(), 0.0);
// set remainingBasket_, results_.remainingNotional,
// vector results_.expectedTrancheLoss for all schedule dates
initialize();
Real e1 = 0;
if (arguments_.schedule.dates().front() > today)
e1 = expectedTrancheLoss (arguments_.schedule.dates()[0]);
for (Size i = 1; i < arguments_.schedule.size(); i++) {
Date d2 = arguments_.schedule.dates()[i];
if (d2 < today)
continue;
Date d1 = arguments_.schedule.dates()[i-1];
Date d, d0 = d1;
do {
d = NullCalendar().advance (d0 > today ? d0 : today,
stepSize_);
if (d > d2) d = d2;
Real e2 = expectedTrancheLoss (d);
results_.premiumValue
+= (results_.remainingNotional - e2)
* arguments_.runningRate
* arguments_.dayCounter.yearFraction (d0, d)
* arguments_.yieldTS->discount (d);
if (e2 < e1) results_.error ++;
results_.protectionValue
+= (e2 - e1) * arguments_.yieldTS->discount (d);
d0 = d;
e1 = e2;
}
while (d < d2);
}
if (arguments_.schedule.dates().front() >= today)
results_.upfrontPremiumValue
= results_.remainingNotional * arguments_.upfrontRate
* arguments_.yieldTS->discount(arguments_.schedule.dates()[0]);
if (arguments_.side == Protection::Buyer) {
results_.protectionValue *= -1;
results_.premiumValue *= -1;
results_.upfrontPremiumValue *= -1;
}
results_.value = results_.premiumValue - results_.protectionValue
+ results_.upfrontPremiumValue;
results_.errorEstimate = Null<Real>();
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void MidPointCDOEngine::calculate() const {
Date today = Settings::instance().evaluationDate();
results_.premiumValue = 0.0;
results_.upfrontPremiumValue = 0.0;
results_.protectionValue = 0.0;
results_.expectedTrancheLoss.clear();
// set remainingBasket_, results_.remainingNotional,
// vector results_.expectedTrancheLoss for all schedule dates
initialize();
const vector<Date>& dates = arguments_.schedule.dates();
if (dates.front() > today)
results_.upfrontPremiumValue =
arguments_.upfrontRate * results_.remainingNotional;
vector<boost::shared_ptr<CashFlow> > premiumLeg =
FixedRateLeg(arguments_.schedule)
.withCouponRates(arguments_.runningRate, arguments_.dayCounter)
.withPaymentAdjustment(arguments_.paymentConvention)
.withNotionals(1.0);
Real e1 = 0;
if (dates[0] > today)
e1 = expectedTrancheLoss (dates[0]);
for (Size i = 0; i < premiumLeg.size(); i++) {
boost::shared_ptr<Coupon> coupon =
boost::dynamic_pointer_cast<Coupon>(premiumLeg[i]);
Date paymentDate = coupon->date();
Date startDate = std::max(coupon->accrualStartDate(),
arguments_.yieldTS->referenceDate());
Date endDate = coupon->accrualEndDate();
Date defaultDate = startDate + (endDate-startDate)/2;
if (paymentDate <= today)
continue;
Real e2 = expectedTrancheLoss(paymentDate);
results_.premiumValue += (results_.remainingNotional - e2)
* coupon->amount()
* arguments_.yieldTS->discount(paymentDate);
Real discount = arguments_.yieldTS->discount(defaultDate);
results_.premiumValue += coupon->accruedAmount(defaultDate)
* discount * (e2 - e1);
results_.protectionValue += discount * (e2 - e1);
e1 = e2;
}
if (arguments_.side == Protection::Buyer) {
results_.protectionValue *= -1;
results_.premiumValue *= -1;
results_.upfrontPremiumValue *= -1;
}
results_.value = results_.premiumValue - results_.protectionValue
+ results_.upfrontPremiumValue;
results_.errorEstimate = Null<Real>();
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void MonteCarloCDOEngine1::defaultScenarios() const {
results_.expectedTrancheLoss.clear();
const vector<Date>& dates = arguments_.schedule.dates();
Date today = Settings::instance().evaluationDate();
Real tmax = ActualActual().yearFraction(today, dates.back());
QL_REQUIRE(tmax >= 0, "tmax < 0");
/*
1) Generate a vector of random default times in the single-factor
Gaussian Copula framework
2) Work out cumulative portfolio and tranche loss for each scenario
3) Map cumulative tranche losses to schedule dates
4) Average over many scenarios
*/
const boost::shared_ptr<Pool> pool = remainingBasket_->pool();
vector<vector<Real> > cumulativeTrancheLoss(samples_, vector<Real>());
results_.expectedTrancheLoss.resize(dates.size(), 0.0);
for (Size i = 0; i < samples_; i++) {
rdm_->nextSequence(tmax);
cumulativeTrancheLoss[i].resize(dates.size(), 0.0);
remainingBasket_->updateScenarioLoss();
for (Size k = 0; k < dates.size(); k++) {
cumulativeTrancheLoss[i][k]
= remainingBasket_->scenarioTrancheLoss(dates[k]);
// aggregate
results_.expectedTrancheLoss[k] += cumulativeTrancheLoss[i][k];
}
}
// normalize
for (Size i = 0; i < dates.size(); i++)
results_.expectedTrancheLoss[i] /= samples_;
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void MonteCarloCDOEngine2::calculate() const {
Date today = Settings::instance().evaluationDate();
results_.protectionValue = 0.0;
results_.premiumValue = 0.0;
results_.expectedTrancheLoss.clear();
// set remainingBasket_, results_.remainingNotional,
initialize();
const vector<Date>& dates = arguments_.schedule.dates();
if (dates.front() > today)
results_.upfrontPremiumValue =
arguments_.upfrontRate * results_.remainingNotional;
Real tmax = ActualActual().yearFraction(today, dates.back());
//Real tmin = ActualActual().yearFraction(today, dates.front());
QL_REQUIRE(tmax >= 0, "tmax < 0");
vector<boost::shared_ptr<CashFlow> > premiumLeg =
FixedRateLeg(arguments_.schedule)
.withNotionals(1.0)
.withCouponRates(arguments_.runningRate, arguments_.dayCounter)
.withPaymentAdjustment(arguments_.paymentConvention);
boost::shared_ptr<Pool> pool = remainingBasket_->pool();
vector<Real> premiumValue(samples_, 0.0);
vector<Real> protectionValue(samples_, 0.0);
vector<Real> value(samples_, 0.0);
vector<Real> fairPremium(samples_, 0.0);
vector<vector<Real> > cumulativeTrancheLoss(samples_, vector<Real>());
for (Size i = 0; i < samples_; i++) { //================================
/******************************************************************
* (1) Compute default times
******************************************************************/
rdm_->nextSequence(tmax);
/******************************************************************
* (2) Cumulative tranche loss to schedule dates
******************************************************************/
cumulativeTrancheLoss[i].resize(dates.size(), 0.0);
remainingBasket_->updateScenarioLoss();
for (Size k = 0; k < dates.size(); k++)
cumulativeTrancheLoss[i][k]
= remainingBasket_->scenarioTrancheLoss(dates[k]);
/*****************************************************************
* (3) Contribution of this scenario to the protection leg
* - Loop through all incremental tranche loss events between
* start and end date
* - Pay and discount these increments as they occur
*****************************************************************/
vector<Loss> increments = remainingBasket_->scenarioIncrementalTrancheLosses(dates.front(), dates.back());
for (Size k = 0; k < increments.size(); k++)
protectionValue[i] += increments[k].amount
* arguments_.yieldTS->discount(increments[k].time);
/*****************************************************************
* (4) Contribution of this scenario to the premium leg
* - Loop through all coupon periods
* - Pay coupon at period end on effective notional
* - Effective notional:
* - Start with remaining notional minus cumulative loss
* on the tranche until period start =: N
* - Reduce N for each loss in the period by subtracting the
* the incremental tranche loss weighted with the time
* to period end
*****************************************************************/
for (Size j = 0; j < premiumLeg.size(); j++) {
boost::shared_ptr<Coupon> coupon =
boost::dynamic_pointer_cast<Coupon>(premiumLeg[j]);
Date startDate = std::max(coupon->accrualStartDate(),
arguments_.yieldTS->referenceDate());
Date endDate = coupon->accrualEndDate();
Date paymentDate = coupon->date();
if (paymentDate <= today)
continue;
Real t1 = ActualActual().yearFraction(today, startDate);
Real t2 = ActualActual().yearFraction(today, endDate);
Real PL = cumulativeTrancheLoss[i][j];
Real N = results_.remainingNotional - PL;
for (Size k = 0; k < increments.size(); k++) {
Real t = increments[k].time;
if (t <= t1) continue;
if (t >= t2) break;
N -= (t2-t) / (t2-t1) * increments[k].amount;
}
Real discount = arguments_.yieldTS->discount(paymentDate);
premiumValue[i] += N * coupon->amount() * discount;
}
/*****************
* Aggregate
*****************/
results_.premiumValue += premiumValue[i];
results_.protectionValue += protectionValue[i];
value[i] = premiumValue[i] - protectionValue[i]
+ results_.upfrontPremiumValue;
for (Size k = 0; k < dates.size(); k++)
results_.expectedTrancheLoss[k] += cumulativeTrancheLoss[i][k];
/*
cout.setf (ios::fixed, ios::floatfield);
cout << setprecision(0);
for (Size k = 0; k < dates.size(); k++)
cout << setw(3) << cumulativeTrancheLoss[i][k] << " ";
cout << endl;
cout << setprecision(2);
for (Size k = 0; k < pool->size(); k++) {
const string name = pool->names()[k];
Real t = pool->getTime(name);
if (t < 6)
cout << setw(10) << name << " " << setw(5) << t << endl;
}
*/
} // end of loop over samples ==========================================
/*****************************************
* Expected values, normalize, switch sign
*****************************************/
results_.premiumValue /= samples_;
results_.protectionValue /= samples_;
for (Size k = 0; k < dates.size(); k++)
results_.expectedTrancheLoss[k] /= samples_;
if (arguments_.side == Protection::Buyer) {
results_.protectionValue *= -1;
results_.premiumValue *= -1;
results_.upfrontPremiumValue *= -1;
}
results_.value = results_.premiumValue - results_.protectionValue
+ results_.upfrontPremiumValue;
/*************************************************
* Error estimates - NPV
*************************************************/
Real avg = 0.0;
Real var = 0.0;
for (Size i = 0; i < samples_; i++) {
var += value[i] * value[i];
avg += value[i];
}
avg /= samples_;
var /= samples_;
results_.errorEstimate = sqrt(var - avg * avg);
/*****************************************************
* Error estimates - fair premium
* http://math.nyu.edu/~atm262/files/spring06/ircm/cdo
*****************************************************/
/*
Real x = 0.0, xx = 0.0, y = 0.0, yy = 0.0, xy = 0.0;
for (Size i = 0; i < samples_; i++) {
Real dx = protectionValue[i] - results_.upfrontPremiumValue;
Real dy = premiumValue[i];
x += dx;
xx += dx * dx;
y += dy;
yy += dy * dy;
xy += dx * dy;
}
x /= samples_;
y /= samples_;
xx /= samples_;
yy /= samples_;
xy /= samples_;
Real v = x*x/(y*y) * (xx/(x*x) + yy/(y*y) - 2.0 * xy/(x*y));
Real stdFairPremium = sqrt(v) * arguments_.runningRate;
*/
}
}
|