| 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/midpointcdoengine.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 MidPointCDOEngine::calculate() const { |
| 31 | Date today = Settings::instance().evaluationDate(); |
| 32 | |
| 33 | results_.premiumValue = 0.0; |
| 34 | results_.protectionValue = 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 | // Notice that since there might be a gap between the end of |
| 50 | // acrrual and payment dates and today be in between |
| 51 | // the tranche loss on that date might not be contingent but |
| 52 | // realized: |
| 53 | e1 = arguments_.basket->expectedTrancheLoss( |
| 54 | d: ext::dynamic_pointer_cast<Coupon>( |
| 55 | r: arguments_.normalizedLeg[0])->accrualStartDate()); |
| 56 | results_.expectedTrancheLoss.push_back(x: e1); |
| 57 | //'e1' should contain the existing loses.....? use remaining amounts? |
| 58 | for (auto& i : arguments_.normalizedLeg) { |
| 59 | if (i->hasOccurred(refDate: today)) { |
| 60 | results_.expectedTrancheLoss.push_back(x: 0.); |
| 61 | continue; |
| 62 | } |
| 63 | ext::shared_ptr<Coupon> coupon = ext::dynamic_pointer_cast<Coupon>(r: i); |
| 64 | Date paymentDate = coupon->date(); |
| 65 | Date startDate = std::max(a: coupon->accrualStartDate(), |
| 66 | b: discountCurve_->referenceDate()); |
| 67 | Date endDate = coupon->accrualEndDate(); |
| 68 | // we assume the loss within the period took place on this date: |
| 69 | Date defaultDate = startDate + (endDate-startDate)/2; |
| 70 | |
| 71 | Real e2 = arguments_.basket->expectedTrancheLoss(d: endDate); |
| 72 | results_.expectedTrancheLoss.push_back(x: e2); |
| 73 | results_.premiumValue += |
| 74 | ((inceptionTrancheNotional - e2) / inceptionTrancheNotional) |
| 75 | * coupon->amount() |
| 76 | * discountCurve_->discount(d: paymentDate); |
| 77 | // default flows: |
| 78 | const Real discount = discountCurve_->discount(d: defaultDate); |
| 79 | |
| 80 | /* Accrual removed till the argument flag is implemented |
| 81 | // pays accrued on defaults' date |
| 82 | results_.premiumValue += coupon->accruedAmount(defaultDate) |
| 83 | * discount * (e2 - e1) / inceptionTrancheNotional; |
| 84 | */ |
| 85 | results_.protectionValue += discount * (e2 - e1); |
| 86 | /* use it in a future version for coherence with the integral engine |
| 87 | * arguments_.leverageFactor; |
| 88 | */ |
| 89 | e1 = e2; |
| 90 | } |
| 91 | |
| 92 | //\todo treat upfron tnow as in the new CDS (see March 2014) |
| 93 | // add includeSettlement date flows variable to engine ? |
| 94 | if (!arguments_.normalizedLeg[0]->hasOccurred(refDate: today)) |
| 95 | results_.upfrontPremiumValue |
| 96 | = inceptionTrancheNotional * arguments_.upfrontRate |
| 97 | * discountCurve_->discount( |
| 98 | d: ext::dynamic_pointer_cast<Coupon>( |
| 99 | r: arguments_.normalizedLeg[0])->accrualStartDate()); |
| 100 | /* use it in a future version for coherence with the integral engine |
| 101 | arguments_.leverageFactor * ; |
| 102 | */ |
| 103 | if (arguments_.side == Protection::Buyer) { |
| 104 | results_.protectionValue *= -1; |
| 105 | results_.premiumValue *= -1; |
| 106 | results_.upfrontPremiumValue *= -1; |
| 107 | } |
| 108 | results_.value = results_.premiumValue - results_.protectionValue |
| 109 | + results_.upfrontPremiumValue; |
| 110 | results_.errorEstimate = Null<Real>(); |
| 111 | // Fair spread GIVEN the upfront |
| 112 | Real fairSpread = 0.; |
| 113 | if (results_.premiumValue != 0.0) { |
| 114 | fairSpread = |
| 115 | -(results_.protectionValue + results_.upfrontPremiumValue) |
| 116 | *arguments_.runningRate/results_.premiumValue; |
| 117 | } |
| 118 | |
| 119 | results_.additionalResults["fairPremium"] = fairSpread; |
| 120 | results_.additionalResults["premiumLegNPV"] = |
| 121 | Real(results_.premiumValue + results_.upfrontPremiumValue); |
| 122 | results_.additionalResults["protectionLegNPV"] = |
| 123 | results_.protectionValue; |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | |
| 128 | #endif |
| 129 |
