| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2005, 2006 Theo Boafo |
| 5 | Copyright (C) 2006, 2007 StatPro Italia srl |
| 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/pricingengines/bond/discretizedconvertible.hpp> |
| 22 | #include <ql/math/comparison.hpp> |
| 23 | #include <ql/processes/blackscholesprocess.hpp> |
| 24 | #include <utility> |
| 25 | |
| 26 | namespace QuantLib { |
| 27 | |
| 28 | DiscretizedConvertible::DiscretizedConvertible( |
| 29 | ConvertibleBond::arguments args, |
| 30 | ext::shared_ptr<GeneralizedBlackScholesProcess> process, |
| 31 | DividendSchedule dividends, |
| 32 | Handle<Quote> creditSpread, |
| 33 | const TimeGrid& grid) |
| 34 | : arguments_(std::move(args)), process_(std::move(process)), |
| 35 | creditSpread_(std::move(creditSpread)) { |
| 36 | |
| 37 | for (const auto& dividend : dividends) { |
| 38 | if (!dividend->hasOccurred(refDate: arguments_.settlementDate, includeRefDate: false)) { |
| 39 | dividends_.push_back(x: dividend); |
| 40 | dividendDates_.push_back(x: dividend->date()); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | dividendValues_ = Array(dividends_.size(), 0.0); |
| 45 | |
| 46 | Date settlementDate = process_->riskFreeRate()->referenceDate(); |
| 47 | for (Size i=0; i<dividends.size(); i++) { |
| 48 | if (dividends[i]->date() >= settlementDate) { |
| 49 | dividendValues_[i] = |
| 50 | dividends[i]->amount() * |
| 51 | process_->riskFreeRate()->discount( |
| 52 | d: dividends[i]->date()); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | DayCounter dayCounter = process_->riskFreeRate()->dayCounter(); |
| 57 | Date bondSettlement = arguments_.settlementDate; |
| 58 | |
| 59 | stoppingTimes_.resize(new_size: arguments_.exercise->dates().size()); |
| 60 | for (Size i=0; i<stoppingTimes_.size(); ++i) |
| 61 | stoppingTimes_[i] = |
| 62 | dayCounter.yearFraction(d1: bondSettlement, |
| 63 | d2: arguments_.exercise->date(index: i)); |
| 64 | |
| 65 | callabilityTimes_.resize(new_size: arguments_.callabilityDates.size()); |
| 66 | for (Size i=0; i<callabilityTimes_.size(); ++i) |
| 67 | callabilityTimes_[i] = |
| 68 | dayCounter.yearFraction(d1: bondSettlement, |
| 69 | d2: arguments_.callabilityDates[i]); |
| 70 | |
| 71 | couponTimes_.clear(); |
| 72 | couponAmounts_.clear(); |
| 73 | for (Size i = 0; i < arguments_.cashflows.size() - 1; ++i) { |
| 74 | if (!arguments_.cashflows[i]->hasOccurred(refDate: bondSettlement, includeRefDate: false)) { |
| 75 | couponTimes_.push_back(x: dayCounter.yearFraction(d1: bondSettlement, |
| 76 | d2: arguments_.cashflows[i]->date())); |
| 77 | couponAmounts_.push_back(x: arguments_.cashflows[i]->amount()); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | dividendTimes_.resize(new_size: dividendDates_.size()); |
| 82 | for (Size i=0; i<dividendTimes_.size(); ++i) |
| 83 | dividendTimes_[i] = |
| 84 | dayCounter.yearFraction(d1: bondSettlement, |
| 85 | d2: dividendDates_[i]); |
| 86 | |
| 87 | if (!grid.empty()) { |
| 88 | // adjust times to grid |
| 89 | for (Real& stoppingTime : stoppingTimes_) |
| 90 | stoppingTime = grid.closestTime(t: stoppingTime); |
| 91 | for (Real& couponTime : couponTimes_) |
| 92 | couponTime = grid.closestTime(t: couponTime); |
| 93 | for (Real& callabilityTime : callabilityTimes_) |
| 94 | callabilityTime = grid.closestTime(t: callabilityTime); |
| 95 | for (Real& dividendTime : dividendTimes_) |
| 96 | dividendTime = grid.closestTime(t: dividendTime); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void DiscretizedConvertible::reset(Size size) { |
| 101 | |
| 102 | // Set to bond redemption values |
| 103 | values_ = Array(size, arguments_.redemption); |
| 104 | |
| 105 | // coupon amounts should be added when adjusting |
| 106 | // values_ = Array(size, arguments_.cashFlows.back()->amount()); |
| 107 | |
| 108 | conversionProbability_ = Array(size, 0.0); |
| 109 | spreadAdjustedRate_ = Array(size, 0.0); |
| 110 | |
| 111 | DayCounter rfdc = process_->riskFreeRate()->dayCounter(); |
| 112 | |
| 113 | // this takes care of convertibility and conversion probabilities |
| 114 | adjustValues(); |
| 115 | |
| 116 | Real creditSpread = creditSpread_->value(); |
| 117 | |
| 118 | Date exercise = arguments_.exercise->lastDate(); |
| 119 | |
| 120 | Rate riskFreeRate = |
| 121 | process_->riskFreeRate()->zeroRate(d: exercise, resultDayCounter: rfdc, |
| 122 | comp: Continuous, freq: NoFrequency); |
| 123 | |
| 124 | // Calculate blended discount rate to be used on roll back. |
| 125 | for (Size j=0; j<values_.size(); j++) { |
| 126 | spreadAdjustedRate_[j] = |
| 127 | conversionProbability_[j] * riskFreeRate + |
| 128 | (1-conversionProbability_[j])*(riskFreeRate + creditSpread); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void DiscretizedConvertible::postAdjustValuesImpl() { |
| 133 | |
| 134 | bool convertible = false; |
| 135 | switch (arguments_.exercise->type()) { |
| 136 | case Exercise::American: |
| 137 | if (time() <= stoppingTimes_[1] && time() >= stoppingTimes_[0]) |
| 138 | convertible = true; |
| 139 | break; |
| 140 | case Exercise::European: |
| 141 | if (isOnTime(t: stoppingTimes_[0])) |
| 142 | convertible = true; |
| 143 | break; |
| 144 | case Exercise::Bermudan: |
| 145 | for (Real stoppingTime : stoppingTimes_) { |
| 146 | if (isOnTime(t: stoppingTime)) |
| 147 | convertible = true; |
| 148 | } |
| 149 | break; |
| 150 | default: |
| 151 | QL_FAIL("invalid option type" ); |
| 152 | } |
| 153 | |
| 154 | for (Size i=0; i<callabilityTimes_.size(); i++) { |
| 155 | if (isOnTime(t: callabilityTimes_[i])) |
| 156 | applyCallability(i,convertible); |
| 157 | } |
| 158 | |
| 159 | for (Size i=0; i<couponTimes_.size(); i++) { |
| 160 | if (isOnTime(t: couponTimes_[i])) |
| 161 | addCoupon(i); |
| 162 | } |
| 163 | |
| 164 | if (convertible) |
| 165 | applyConvertibility(); |
| 166 | } |
| 167 | |
| 168 | void DiscretizedConvertible::applyConvertibility() { |
| 169 | Array grid = adjustedGrid(); |
| 170 | for (Size j=0; j<values_.size(); j++) { |
| 171 | Real payoff = arguments_.conversionRatio*grid[j]; |
| 172 | if (values_[j] <= payoff) { |
| 173 | values_[j] = payoff; |
| 174 | conversionProbability_[j] = 1.0; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void DiscretizedConvertible::applyCallability(Size i, bool convertible) { |
| 180 | Size j; |
| 181 | Array grid = adjustedGrid(); |
| 182 | switch (arguments_.callabilityTypes[i]) { |
| 183 | case Callability::Call: |
| 184 | if (arguments_.callabilityTriggers[i] != Null<Real>()) { |
| 185 | Real conversionValue = |
| 186 | arguments_.redemption/arguments_.conversionRatio; |
| 187 | Real trigger = |
| 188 | conversionValue*arguments_.callabilityTriggers[i]; |
| 189 | for (j=0; j<values_.size(); j++) { |
| 190 | // the callability is conditioned by the trigger... |
| 191 | if (grid[j] >= trigger) { |
| 192 | // ...and might trigger conversion |
| 193 | values_[j] = |
| 194 | std::min(a: std::max( |
| 195 | a: arguments_.callabilityPrices[i], |
| 196 | b: arguments_.conversionRatio*grid[j]), |
| 197 | b: values_[j]); |
| 198 | } |
| 199 | } |
| 200 | } else if (convertible) { |
| 201 | for (j=0; j<values_.size(); j++) { |
| 202 | // exercising the callability might trigger conversion |
| 203 | values_[j] = |
| 204 | std::min(a: std::max(a: arguments_.callabilityPrices[i], |
| 205 | b: arguments_.conversionRatio*grid[j]), |
| 206 | b: values_[j]); |
| 207 | } |
| 208 | } else { |
| 209 | for (j=0; j<values_.size(); j++) { |
| 210 | values_[j] = std::min(a: arguments_.callabilityPrices[i], |
| 211 | b: values_[j]); |
| 212 | } |
| 213 | } |
| 214 | break; |
| 215 | case Callability::Put: |
| 216 | for (j=0; j<values_.size(); j++) { |
| 217 | values_[j] = std::max(a: values_[j], |
| 218 | b: arguments_.callabilityPrices[i]); |
| 219 | } |
| 220 | break; |
| 221 | default: |
| 222 | QL_FAIL("unknown callability type" ); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void DiscretizedConvertible::addCoupon(Size i) { |
| 227 | values_ += couponAmounts_[i]; |
| 228 | } |
| 229 | |
| 230 | Array DiscretizedConvertible::adjustedGrid() const { |
| 231 | Time t = time(); |
| 232 | Array grid = method()->grid(t); |
| 233 | // add back all dividend amounts in the future |
| 234 | for (Size i=0; i<dividends_.size(); i++) { |
| 235 | Time dividendTime = dividendTimes_[i]; |
| 236 | if (dividendTime >= t || close(x: dividendTime,y: t)) { |
| 237 | const ext::shared_ptr<Dividend>& d = dividends_[i]; |
| 238 | DiscountFactor dividendDiscount = |
| 239 | process_->riskFreeRate()->discount(t: dividendTime) / |
| 240 | process_->riskFreeRate()->discount(t); |
| 241 | for (Real& j : grid) |
| 242 | j += d->amount(underlying: j) * dividendDiscount; |
| 243 | } |
| 244 | } |
| 245 | return grid; |
| 246 | } |
| 247 | |
| 248 | } |
| 249 | |
| 250 | |