| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|---|---|
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2008 Roland Lichters |
| 5 | Copyright (C) 2009, 2014 Jose Aparicio |
| 6 | |
| 7 | This file is part of QuantLib, a free-software/open-source library |
| 8 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 9 | |
| 10 | QuantLib is free software: you can redistribute it and/or modify it |
| 11 | under the terms of the QuantLib license. You should have received a |
| 12 | copy of the license along with this program; if not, please email |
| 13 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 14 | <http://quantlib.org/license.shtml>. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 19 | */ |
| 20 | |
| 21 | #include <ql/experimental/credit/integralcdoengine.hpp> |
| 22 | |
| 23 | #ifndef QL_PATCH_SOLARIS |
| 24 | |
| 25 | #include <ql/cashflows/fixedratecoupon.hpp> |
| 26 | #include <ql/termstructures/yieldtermstructure.hpp> |
| 27 | |
| 28 | namespace QuantLib { |
| 29 | |
| 30 | void IntegralCDOEngine::calculate() const { |
| 31 | Date today = Settings::instance().evaluationDate(); |
| 32 | |
| 33 | results_.protectionValue = 0.0; |
| 34 | results_.premiumValue = 0.0; |
| 35 | results_.upfrontPremiumValue = 0.0; |
| 36 | results_.error = 0; |
| 37 | results_.expectedTrancheLoss.clear(); |
| 38 | // todo Should be remaining when considering realized loses |
| 39 | results_.xMin = arguments_.basket->attachmentAmount(); |
| 40 | results_.xMax = arguments_.basket->detachmentAmount(); |
| 41 | results_.remainingNotional = results_.xMax - results_.xMin; |
| 42 | const Real inceptionTrancheNotional = |
| 43 | arguments_.basket->trancheNotional(); |
| 44 | |
| 45 | // compute expected loss at the beginning of first relevant period |
| 46 | Real e1 = 0; |
| 47 | // todo add includeSettlement date flows variable to engine. |
| 48 | if (!arguments_.normalizedLeg[0]->hasOccurred(refDate: today)) |
| 49 | // cast to fixed rate coupon? |
| 50 | e1 = arguments_.basket->expectedTrancheLoss( |
| 51 | d: ext::dynamic_pointer_cast<Coupon>( |
| 52 | r: arguments_.normalizedLeg[0])->accrualStartDate()); |
| 53 | results_.expectedTrancheLoss.push_back(x: e1);// zero or realized losses? |
| 54 | |
| 55 | for (auto& i : arguments_.normalizedLeg) { |
| 56 | if (i->hasOccurred(refDate: today)) { |
| 57 | // add includeSettlement date flows variable to engine. |
| 58 | results_.expectedTrancheLoss.push_back(x: 0.); |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | const ext::shared_ptr<Coupon> coupon = ext::dynamic_pointer_cast<Coupon>(r: i); |
| 63 | |
| 64 | Date d1 = coupon->accrualStartDate(); |
| 65 | Date d2 = coupon->date(); |
| 66 | |
| 67 | Date d, d0 = d1; |
| 68 | Real e2; |
| 69 | do { |
| 70 | d = NullCalendar().advance(date: d0 > today ? d0 : today, |
| 71 | period: stepSize_); |
| 72 | if (d > d2) d = d2; |
| 73 | |
| 74 | e2 = arguments_.basket->expectedTrancheLoss(d); |
| 75 | |
| 76 | results_.premiumValue |
| 77 | // ..check for e2 including past/realized losses |
| 78 | += (inceptionTrancheNotional - e2) |
| 79 | * arguments_.runningRate |
| 80 | * arguments_.dayCounter.yearFraction(d1: d0, d2: d) |
| 81 | * discountCurve_->discount(d); |
| 82 | |
| 83 | // TO DO: Addd default coupon accrual value here----- |
| 84 | |
| 85 | if (e2 < e1) results_.error ++; |
| 86 | |
| 87 | results_.protectionValue |
| 88 | += (e2 - e1) * discountCurve_->discount(d); |
| 89 | |
| 90 | d0 = d; |
| 91 | e1 = e2; |
| 92 | } |
| 93 | while (d < d2); |
| 94 | results_.expectedTrancheLoss.push_back(x: e2); |
| 95 | } |
| 96 | |
| 97 | // add includeSettlement date flows variable to engine. |
| 98 | if (!arguments_.normalizedLeg[0]->hasOccurred(refDate: today)) |
| 99 | results_.upfrontPremiumValue |
| 100 | = inceptionTrancheNotional * arguments_.upfrontRate |
| 101 | * discountCurve_->discount( |
| 102 | d: ext::dynamic_pointer_cast<Coupon>( |
| 103 | r: arguments_.normalizedLeg[0])->accrualStartDate()); |
| 104 | |
| 105 | if (arguments_.side == Protection::Buyer) { |
| 106 | results_.protectionValue *= -1; |
| 107 | results_.premiumValue *= -1; |
| 108 | results_.upfrontPremiumValue *= -1; |
| 109 | } |
| 110 | |
| 111 | results_.value = results_.premiumValue - results_.protectionValue |
| 112 | + results_.upfrontPremiumValue; |
| 113 | results_.errorEstimate = Null<Real>(); |
| 114 | // Fair spread GIVEN the upfront |
| 115 | Real fairSpread = 0.; |
| 116 | if (results_.premiumValue != 0.0) { |
| 117 | fairSpread = |
| 118 | -(results_.protectionValue + results_.upfrontPremiumValue) |
| 119 | *arguments_.runningRate/results_.premiumValue; |
| 120 | } |
| 121 | |
| 122 | results_.additionalResults["fairPremium"] = fairSpread; |
| 123 | results_.additionalResults["premiumLegNPV"] = |
| 124 | Real(results_.premiumValue + results_.upfrontPremiumValue); |
| 125 | results_.additionalResults["protectionLegNPV"] = |
| 126 | results_.protectionValue; |
| 127 | } |
| 128 | |
| 129 | } |
| 130 | |
| 131 | #endif |
| 132 |
