/src/quantlib/ql/experimental/credit/integralcdoengine.cpp
Line | Count | Source |
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 | | <https://www.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 | 0 | void IntegralCDOEngine::calculate() const { |
31 | 0 | Date today = Settings::instance().evaluationDate(); |
32 | |
|
33 | 0 | results_.protectionValue = 0.0; |
34 | 0 | results_.premiumValue = 0.0; |
35 | 0 | results_.upfrontPremiumValue = 0.0; |
36 | 0 | results_.error = 0; |
37 | 0 | results_.expectedTrancheLoss.clear(); |
38 | | // todo Should be remaining when considering realized loses |
39 | 0 | results_.xMin = arguments_.basket->attachmentAmount(); |
40 | 0 | results_.xMax = arguments_.basket->detachmentAmount(); |
41 | 0 | results_.remainingNotional = results_.xMax - results_.xMin; |
42 | 0 | const Real inceptionTrancheNotional = |
43 | 0 | arguments_.basket->trancheNotional(); |
44 | | |
45 | | // compute expected loss at the beginning of first relevant period |
46 | 0 | Real e1 = 0; |
47 | | // todo add includeSettlement date flows variable to engine. |
48 | 0 | if (!arguments_.normalizedLeg[0]->hasOccurred(today)) |
49 | | // cast to fixed rate coupon? |
50 | 0 | e1 = arguments_.basket->expectedTrancheLoss( |
51 | 0 | ext::dynamic_pointer_cast<Coupon>( |
52 | 0 | arguments_.normalizedLeg[0])->accrualStartDate()); |
53 | 0 | results_.expectedTrancheLoss.push_back(e1);// zero or realized losses? |
54 | |
|
55 | 0 | for (auto& i : arguments_.normalizedLeg) { |
56 | 0 | if (i->hasOccurred(today)) { |
57 | | // add includeSettlement date flows variable to engine. |
58 | 0 | results_.expectedTrancheLoss.push_back(0.); |
59 | 0 | continue; |
60 | 0 | } |
61 | | |
62 | 0 | const ext::shared_ptr<Coupon> coupon = ext::dynamic_pointer_cast<Coupon>(i); |
63 | |
|
64 | 0 | Date d1 = coupon->accrualStartDate(); |
65 | 0 | Date d2 = coupon->date(); |
66 | |
|
67 | 0 | Date d, d0 = d1; |
68 | 0 | Real e2; |
69 | 0 | do { |
70 | 0 | d = NullCalendar().advance(d0 > today ? d0 : today, |
71 | 0 | stepSize_); |
72 | 0 | if (d > d2) d = d2; |
73 | |
|
74 | 0 | e2 = arguments_.basket->expectedTrancheLoss(d); |
75 | |
|
76 | 0 | results_.premiumValue |
77 | | // ..check for e2 including past/realized losses |
78 | 0 | += (inceptionTrancheNotional - e2) |
79 | 0 | * arguments_.runningRate |
80 | 0 | * arguments_.dayCounter.yearFraction(d0, d) |
81 | 0 | * discountCurve_->discount(d); |
82 | | |
83 | | // TO DO: Addd default coupon accrual value here----- |
84 | |
|
85 | 0 | if (e2 < e1) results_.error ++; |
86 | |
|
87 | 0 | results_.protectionValue |
88 | 0 | += (e2 - e1) * discountCurve_->discount(d); |
89 | |
|
90 | 0 | d0 = d; |
91 | 0 | e1 = e2; |
92 | 0 | } |
93 | 0 | while (d < d2); |
94 | 0 | results_.expectedTrancheLoss.push_back(e2); |
95 | 0 | } |
96 | | |
97 | | // add includeSettlement date flows variable to engine. |
98 | 0 | if (!arguments_.normalizedLeg[0]->hasOccurred(today)) |
99 | 0 | results_.upfrontPremiumValue |
100 | 0 | = inceptionTrancheNotional * arguments_.upfrontRate |
101 | 0 | * discountCurve_->discount( |
102 | 0 | ext::dynamic_pointer_cast<Coupon>( |
103 | 0 | arguments_.normalizedLeg[0])->accrualStartDate()); |
104 | |
|
105 | 0 | if (arguments_.side == Protection::Buyer) { |
106 | 0 | results_.protectionValue *= -1; |
107 | 0 | results_.premiumValue *= -1; |
108 | 0 | results_.upfrontPremiumValue *= -1; |
109 | 0 | } |
110 | |
|
111 | 0 | results_.value = results_.premiumValue - results_.protectionValue |
112 | 0 | + results_.upfrontPremiumValue; |
113 | 0 | results_.errorEstimate = Null<Real>(); |
114 | | // Fair spread GIVEN the upfront |
115 | 0 | Real fairSpread = 0.; |
116 | 0 | if (results_.premiumValue != 0.0) { |
117 | 0 | fairSpread = |
118 | 0 | -(results_.protectionValue + results_.upfrontPremiumValue) |
119 | 0 | *arguments_.runningRate/results_.premiumValue; |
120 | 0 | } |
121 | |
|
122 | 0 | results_.additionalResults["fairPremium"] = fairSpread; |
123 | 0 | results_.additionalResults["premiumLegNPV"] = |
124 | 0 | Real(results_.premiumValue + results_.upfrontPremiumValue); |
125 | 0 | results_.additionalResults["protectionLegNPV"] = |
126 | 0 | results_.protectionValue; |
127 | 0 | } |
128 | | |
129 | | } |
130 | | |
131 | | #endif |