| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2008 Roland Lichters |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | #include <ql/experimental/credit/nthtodefault.hpp> |
| 21 | #include <ql/experimental/credit/lossdistribution.hpp> |
| 22 | #include <ql/instruments/claim.hpp> |
| 23 | #include <ql/cashflows/fixedratecoupon.hpp> |
| 24 | #include <ql/termstructures/yieldtermstructure.hpp> |
| 25 | #include <ql/event.hpp> |
| 26 | #include <ql/experimental/credit/basket.hpp> |
| 27 | |
| 28 | namespace QuantLib { |
| 29 | |
| 30 | |
| 31 | NthToDefault::NthToDefault( |
| 32 | const ext::shared_ptr<Basket>& basket, |
| 33 | Size n, |
| 34 | Protection::Side side, |
| 35 | const Schedule& premiumSchedule, |
| 36 | Rate upfrontRate, |
| 37 | Rate premiumRate, |
| 38 | const DayCounter& dayCounter, |
| 39 | Real nominal, |
| 40 | bool settlePremiumAccrual |
| 41 | ) |
| 42 | : basket_(basket), n_(n), |
| 43 | side_(side), nominal_(nominal), |
| 44 | premiumSchedule_(premiumSchedule), premiumRate_(premiumRate), |
| 45 | upfrontRate_(upfrontRate), |
| 46 | dayCounter_(dayCounter), settlePremiumAccrual_(settlePremiumAccrual) |
| 47 | { |
| 48 | QL_REQUIRE(n_ <= basket_->size(), |
| 49 | "NTD order provided is larger than the basket size." ); |
| 50 | |
| 51 | // Basket inception must lie before contract protection start. |
| 52 | QL_REQUIRE(basket->refDate() <= premiumSchedule.startDate(), |
| 53 | //using the start date of the schedule might be wrong, think of the CDS rule |
| 54 | "Basket did not exist before contract start." ); |
| 55 | |
| 56 | premiumLeg_ = FixedRateLeg(premiumSchedule) |
| 57 | .withNotionals(nominal) |
| 58 | .withCouponRates(premiumRate, paymentDayCounter: dayCounter) |
| 59 | .withPaymentAdjustment(Unadjusted); |
| 60 | |
| 61 | registerWith(h: basket_); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | // SOME OF THESE ARE INLINES--------------------------------- |
| 66 | Size NthToDefault::basketSize() const { return basket_->size(); } |
| 67 | |
| 68 | bool NthToDefault::isExpired() const { |
| 69 | return detail::simple_event(premiumLeg_.back()->date()).hasOccurred(); |
| 70 | } |
| 71 | |
| 72 | Rate NthToDefault::fairPremium() const { |
| 73 | calculate(); |
| 74 | QL_REQUIRE(fairPremium_ != Null<Rate>(), |
| 75 | "fair premium not available" ); |
| 76 | return fairPremium_; |
| 77 | } |
| 78 | |
| 79 | Real NthToDefault::premiumLegNPV() const { |
| 80 | calculate(); |
| 81 | QL_REQUIRE(premiumValue_ != Null<Rate>(), |
| 82 | "premium leg not available" ); |
| 83 | QL_REQUIRE(upfrontPremiumValue_ != Null<Rate>(), |
| 84 | "upfront value not available" ); |
| 85 | return premiumValue_ + upfrontPremiumValue_; |
| 86 | } |
| 87 | |
| 88 | Real NthToDefault::protectionLegNPV() const { |
| 89 | calculate(); |
| 90 | QL_REQUIRE(protectionValue_ != Null<Rate>(), |
| 91 | "protection leg not available" ); |
| 92 | return protectionValue_; |
| 93 | } |
| 94 | |
| 95 | Real NthToDefault::errorEstimate() const { |
| 96 | calculate(); |
| 97 | QL_REQUIRE(errorEstimate_ != Null<Rate>(), |
| 98 | "error estimate not available" ); |
| 99 | return errorEstimate_; |
| 100 | |
| 101 | } |
| 102 | |
| 103 | void NthToDefault::setupExpired() const { |
| 104 | Instrument::setupExpired(); |
| 105 | |
| 106 | premiumValue_ = 0.0; |
| 107 | protectionValue_ = 0.0; |
| 108 | upfrontPremiumValue_ = 0.0; |
| 109 | fairPremium_ = 0.0; |
| 110 | errorEstimate_ = 0.0; |
| 111 | } |
| 112 | |
| 113 | void NthToDefault::setupArguments(PricingEngine::arguments* args) const { |
| 114 | auto* arguments = dynamic_cast<NthToDefault::arguments*>(args); |
| 115 | QL_REQUIRE(arguments != nullptr, "wrong argument type" ); |
| 116 | arguments->basket = basket_; |
| 117 | arguments->side = side_; |
| 118 | arguments->premiumLeg = premiumLeg_; |
| 119 | arguments->ntdOrder = n_; |
| 120 | arguments->settlePremiumAccrual = settlePremiumAccrual_; |
| 121 | arguments->notional = nominal_; |
| 122 | arguments->premiumRate = premiumRate_; |
| 123 | arguments->upfrontRate = upfrontRate_; |
| 124 | } |
| 125 | |
| 126 | void NthToDefault::fetchResults(const PricingEngine::results* r) const { |
| 127 | Instrument::fetchResults(r); |
| 128 | |
| 129 | const auto* results = dynamic_cast<const NthToDefault::results*>(r); |
| 130 | QL_REQUIRE(results != nullptr, "wrong result type" ); |
| 131 | |
| 132 | premiumValue_ = results->premiumValue; |
| 133 | protectionValue_ = results->protectionValue; |
| 134 | upfrontPremiumValue_ = results->upfrontPremiumValue; |
| 135 | fairPremium_ = results->fairPremium; |
| 136 | errorEstimate_ = results->errorEstimate; |
| 137 | } |
| 138 | |
| 139 | void NthToDefault::results::reset() { |
| 140 | Instrument::results::reset(); |
| 141 | premiumValue = Null<Real>(); |
| 142 | protectionValue = Null<Real>(); |
| 143 | upfrontPremiumValue = Null<Real>(); |
| 144 | fairPremium = Null<Real>(); |
| 145 | errorEstimate = Null<Real>(); |
| 146 | additionalResults.clear(); |
| 147 | } |
| 148 | |
| 149 | void NthToDefault::arguments::validate() const { |
| 150 | QL_REQUIRE(basket && !basket->names().empty(), "no basket given" ); |
| 151 | QL_REQUIRE(side != Protection::Side(-1), "side not set" ); |
| 152 | QL_REQUIRE(premiumRate != Null<Real>(), "no premium rate given" ); |
| 153 | QL_REQUIRE(upfrontRate != Null<Real>(), "no upfront rate given" ); |
| 154 | QL_REQUIRE(notional != Null<Real>(), "no notional given" ); |
| 155 | QL_REQUIRE(ntdOrder != Null<Size>(), "no NTD order given" ); |
| 156 | } |
| 157 | |
| 158 | } |
| 159 | |
| 160 | |