| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2004, 2009 Ferdinando Ametrano |
| 5 | Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl |
| 6 | Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl |
| 7 | |
| 8 | This file is part of QuantLib, a free-software/open-source library |
| 9 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 10 | |
| 11 | QuantLib is free software: you can redistribute it and/or modify it |
| 12 | under the terms of the QuantLib license. You should have received a |
| 13 | copy of the license along with this program; if not, please email |
| 14 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 15 | <http://quantlib.org/license.shtml>. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 20 | */ |
| 21 | |
| 22 | #include <ql/termstructures/yieldtermstructure.hpp> |
| 23 | #include <ql/utilities/dataformatters.hpp> |
| 24 | #include <utility> |
| 25 | |
| 26 | namespace QuantLib { |
| 27 | |
| 28 | namespace { |
| 29 | // time interval used in finite differences |
| 30 | const Time dt = 0.0001; |
| 31 | } |
| 32 | |
| 33 | YieldTermStructure::YieldTermStructure(const DayCounter& dc) : TermStructure(dc) {} |
| 34 | |
| 35 | YieldTermStructure::YieldTermStructure(const Date& referenceDate, |
| 36 | const Calendar& cal, |
| 37 | const DayCounter& dc, |
| 38 | std::vector<Handle<Quote> > jumps, |
| 39 | const std::vector<Date>& jumpDates) |
| 40 | : TermStructure(referenceDate, cal, dc), jumps_(std::move(jumps)), jumpDates_(jumpDates), |
| 41 | jumpTimes_(jumpDates.size()), nJumps_(jumps_.size()) { |
| 42 | setJumps(YieldTermStructure::referenceDate()); |
| 43 | for (Size i=0; i<nJumps_; ++i) |
| 44 | registerWith(h: jumps_[i]); |
| 45 | } |
| 46 | |
| 47 | YieldTermStructure::YieldTermStructure(Natural settlementDays, |
| 48 | const Calendar& cal, |
| 49 | const DayCounter& dc, |
| 50 | std::vector<Handle<Quote> > jumps, |
| 51 | const std::vector<Date>& jumpDates) |
| 52 | : TermStructure(settlementDays, cal, dc), jumps_(std::move(jumps)), jumpDates_(jumpDates), |
| 53 | jumpTimes_(jumpDates.size()), nJumps_(jumps_.size()) { |
| 54 | setJumps(YieldTermStructure::referenceDate()); |
| 55 | for (Size i=0; i<nJumps_; ++i) |
| 56 | registerWith(h: jumps_[i]); |
| 57 | } |
| 58 | |
| 59 | void YieldTermStructure::setJumps(const Date& referenceDate) { |
| 60 | if (jumpDates_.empty() && !jumps_.empty()) { // turn of year dates |
| 61 | jumpDates_.resize(new_size: nJumps_); |
| 62 | jumpTimes_.resize(new_size: nJumps_); |
| 63 | Year y = referenceDate.year(); |
| 64 | for (Size i=0; i<nJumps_; ++i) |
| 65 | jumpDates_[i] = Date(31, December, y+i); |
| 66 | } else { // fixed dates |
| 67 | QL_REQUIRE(jumpDates_.size()==nJumps_, |
| 68 | "mismatch between number of jumps (" << nJumps_ << |
| 69 | ") and jump dates (" << jumpDates_.size() << ")" ); |
| 70 | } |
| 71 | for (Size i=0; i<nJumps_; ++i) |
| 72 | jumpTimes_[i] = timeFromReference(d: jumpDates_[i]); |
| 73 | latestReference_ = referenceDate; |
| 74 | } |
| 75 | |
| 76 | DiscountFactor YieldTermStructure::discount(Time t, |
| 77 | bool ) const { |
| 78 | checkRange(t, extrapolate); |
| 79 | |
| 80 | if (jumps_.empty()) |
| 81 | return discountImpl(t); |
| 82 | |
| 83 | DiscountFactor jumpEffect = 1.0; |
| 84 | for (Size i=0; i<nJumps_; ++i) { |
| 85 | if (jumpTimes_[i]>0 && jumpTimes_[i]<t) { |
| 86 | QL_REQUIRE(jumps_[i]->isValid(), |
| 87 | "invalid " << io::ordinal(i+1) << " jump quote" ); |
| 88 | DiscountFactor thisJump = jumps_[i]->value(); |
| 89 | QL_REQUIRE(thisJump > 0.0, |
| 90 | "invalid " << io::ordinal(i+1) << " jump value: " << |
| 91 | thisJump); |
| 92 | jumpEffect *= thisJump; |
| 93 | } |
| 94 | } |
| 95 | return jumpEffect * discountImpl(t); |
| 96 | } |
| 97 | |
| 98 | InterestRate YieldTermStructure::zeroRate(const Date& d, |
| 99 | const DayCounter& dayCounter, |
| 100 | Compounding comp, |
| 101 | Frequency freq, |
| 102 | bool ) const { |
| 103 | Time t = timeFromReference(d); |
| 104 | if (t == 0) { |
| 105 | Real compound = 1.0/discount(t: dt, extrapolate); |
| 106 | // t has been calculated with a possibly different daycounter |
| 107 | // but the difference should not matter for very small times |
| 108 | return InterestRate::impliedRate(compound, |
| 109 | resultDC: dayCounter, comp, freq, |
| 110 | t: dt); |
| 111 | } |
| 112 | Real compound = 1.0/discount(t, extrapolate); |
| 113 | return InterestRate::impliedRate(compound, |
| 114 | resultDC: dayCounter, comp, freq, |
| 115 | d1: referenceDate(), d2: d); |
| 116 | } |
| 117 | |
| 118 | InterestRate YieldTermStructure::zeroRate(Time t, |
| 119 | Compounding comp, |
| 120 | Frequency freq, |
| 121 | bool ) const { |
| 122 | if (t==0.0) t = dt; |
| 123 | Real compound = 1.0/discount(t, extrapolate); |
| 124 | return InterestRate::impliedRate(compound, |
| 125 | resultDC: dayCounter(), comp, freq, |
| 126 | t); |
| 127 | } |
| 128 | |
| 129 | InterestRate YieldTermStructure::forwardRate(const Date& d1, |
| 130 | const Date& d2, |
| 131 | const DayCounter& dayCounter, |
| 132 | Compounding comp, |
| 133 | Frequency freq, |
| 134 | bool ) const { |
| 135 | if (d1==d2) { |
| 136 | checkRange(d: d1, extrapolate); |
| 137 | Time t1 = std::max(a: timeFromReference(d: d1) - dt/2.0, b: 0.0); |
| 138 | Time t2 = t1 + dt; |
| 139 | Real compound = |
| 140 | discount(t: t1, extrapolate: true)/discount(t: t2, extrapolate: true); |
| 141 | // times have been calculated with a possibly different daycounter |
| 142 | // but the difference should not matter for very small times |
| 143 | return InterestRate::impliedRate(compound, |
| 144 | resultDC: dayCounter, comp, freq, |
| 145 | t: dt); |
| 146 | } |
| 147 | QL_REQUIRE(d1 < d2, d1 << " later than " << d2); |
| 148 | Real compound = discount(d: d1, extrapolate)/discount(d: d2, extrapolate); |
| 149 | return InterestRate::impliedRate(compound, |
| 150 | resultDC: dayCounter, comp, freq, |
| 151 | d1, d2); |
| 152 | } |
| 153 | |
| 154 | InterestRate YieldTermStructure::forwardRate(Time t1, |
| 155 | Time t2, |
| 156 | Compounding comp, |
| 157 | Frequency freq, |
| 158 | bool ) const { |
| 159 | Real compound; |
| 160 | if (t2==t1) { |
| 161 | checkRange(t: t1, extrapolate); |
| 162 | t1 = std::max(a: t1 - dt/2.0, b: 0.0); |
| 163 | t2 = t1 + dt; |
| 164 | compound = discount(t: t1, extrapolate: true)/discount(t: t2, extrapolate: true); |
| 165 | } else { |
| 166 | QL_REQUIRE(t2>t1, "t2 (" << t2 << ") < t1 (" << t2 << ")" ); |
| 167 | compound = discount(t: t1, extrapolate)/discount(t: t2, extrapolate); |
| 168 | } |
| 169 | return InterestRate::impliedRate(compound, |
| 170 | resultDC: dayCounter(), comp, freq, |
| 171 | t: t2-t1); |
| 172 | } |
| 173 | |
| 174 | void YieldTermStructure::update() { |
| 175 | TermStructure::update(); |
| 176 | Date newReference = Date(); |
| 177 | try { |
| 178 | newReference = referenceDate(); |
| 179 | if (newReference != latestReference_) |
| 180 | setJumps(newReference); |
| 181 | } catch (Error&) { |
| 182 | if (newReference == Date()) { |
| 183 | // the curve couldn't calculate the reference |
| 184 | // date. Most of the times, this is because some |
| 185 | // underlying handle wasn't set, so we can just absorb |
| 186 | // the exception and continue; the jumps will be set |
| 187 | // correctly when a valid underlying is set. |
| 188 | return; |
| 189 | } else { |
| 190 | // something else happened during the call to |
| 191 | // setJumps(), so we let the exception bubble up. |
| 192 | throw; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |