| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2004 Ferdinando Ametrano |
| 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 | /*! \file interestrate.hpp |
| 21 | \brief Instrument rate class |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_interest_rate_hpp |
| 25 | #define quantlib_interest_rate_hpp |
| 26 | |
| 27 | #include <ql/compounding.hpp> |
| 28 | #include <ql/time/daycounters/actual365fixed.hpp> |
| 29 | |
| 30 | namespace QuantLib { |
| 31 | |
| 32 | //! Concrete interest rate class |
| 33 | /*! This class encapsulate the interest rate compounding algebra. |
| 34 | It manages day-counting conventions, compounding conventions, |
| 35 | conversion between different conventions, discount/compound factor |
| 36 | calculations, and implied/equivalent rate calculations. |
| 37 | |
| 38 | \test Converted rates are checked against known good results |
| 39 | */ |
| 40 | class InterestRate { |
| 41 | public: |
| 42 | //! \name constructors |
| 43 | //@{ |
| 44 | //! Default constructor returning a null interest rate. |
| 45 | InterestRate(); |
| 46 | //! Standard constructor |
| 47 | InterestRate(Rate r, DayCounter dc, Compounding comp, Frequency freq); |
| 48 | //@} |
| 49 | //! \name conversions |
| 50 | //@{ |
| 51 | operator Rate() const { return r_; } |
| 52 | //@} |
| 53 | //! \name inspectors |
| 54 | //@{ |
| 55 | Rate rate() const { return r_; } |
| 56 | const DayCounter& dayCounter() const { return dc_; } |
| 57 | Compounding compounding() const { return comp_; } |
| 58 | Frequency frequency() const { |
| 59 | return freqMakesSense_ ? Frequency(Integer(freq_)) : NoFrequency; |
| 60 | } |
| 61 | //@} |
| 62 | |
| 63 | //! \name discount/compound factor calculations |
| 64 | //@{ |
| 65 | //! discount factor implied by the rate compounded at time t. |
| 66 | /*! \warning Time must be measured using InterestRate's own |
| 67 | day counter. |
| 68 | */ |
| 69 | DiscountFactor discountFactor(Time t) const { |
| 70 | return 1.0/compoundFactor(t); |
| 71 | } |
| 72 | |
| 73 | //! discount factor implied by the rate compounded between two dates |
| 74 | DiscountFactor discountFactor(const Date& d1, |
| 75 | const Date& d2, |
| 76 | const Date& refStart = Date(), |
| 77 | const Date& refEnd = Date()) const { |
| 78 | QL_REQUIRE(d2>=d1, |
| 79 | "d1 (" << d1 << ") " |
| 80 | "later than d2 (" << d2 << ")" ); |
| 81 | Time t = dc_.yearFraction(d1, d2, refPeriodStart: refStart, refPeriodEnd: refEnd); |
| 82 | return discountFactor(t); |
| 83 | } |
| 84 | |
| 85 | //! compound factor implied by the rate compounded at time t. |
| 86 | /*! returns the compound (a.k.a capitalization) factor |
| 87 | implied by the rate compounded at time t. |
| 88 | |
| 89 | \warning Time must be measured using InterestRate's own |
| 90 | day counter. |
| 91 | */ |
| 92 | Real compoundFactor(Time t) const; |
| 93 | |
| 94 | //! compound factor implied by the rate compounded between two dates |
| 95 | /*! returns the compound (a.k.a capitalization) factor |
| 96 | implied by the rate compounded between two dates. |
| 97 | */ |
| 98 | Real compoundFactor(const Date& d1, |
| 99 | const Date& d2, |
| 100 | const Date& refStart = Date(), |
| 101 | const Date& refEnd = Date()) const { |
| 102 | QL_REQUIRE(d2>=d1, |
| 103 | "d1 (" << d1 << ") " |
| 104 | "later than d2 (" << d2 << ")" ); |
| 105 | Time t = dc_.yearFraction(d1, d2, refPeriodStart: refStart, refPeriodEnd: refEnd); |
| 106 | return compoundFactor(t); |
| 107 | } |
| 108 | //@} |
| 109 | |
| 110 | //! \name implied rate calculations |
| 111 | //@{ |
| 112 | |
| 113 | //! implied interest rate for a given compound factor at a given time. |
| 114 | /*! The resulting InterestRate has the day-counter provided as input. |
| 115 | |
| 116 | \warning Time must be measured using the day-counter provided |
| 117 | as input. |
| 118 | */ |
| 119 | static InterestRate impliedRate(Real compound, |
| 120 | const DayCounter& resultDC, |
| 121 | Compounding comp, |
| 122 | Frequency freq, |
| 123 | Time t); |
| 124 | |
| 125 | //! implied rate for a given compound factor between two dates. |
| 126 | /*! The resulting rate is calculated taking the required |
| 127 | day-counting rule into account. |
| 128 | */ |
| 129 | static InterestRate impliedRate(Real compound, |
| 130 | const DayCounter& resultDC, |
| 131 | Compounding comp, |
| 132 | Frequency freq, |
| 133 | const Date& d1, |
| 134 | const Date& d2, |
| 135 | const Date& refStart = Date(), |
| 136 | const Date& refEnd = Date()) { |
| 137 | QL_REQUIRE(d2>=d1, |
| 138 | "d1 (" << d1 << ") " |
| 139 | "later than d2 (" << d2 << ")" ); |
| 140 | Time t = resultDC.yearFraction(d1, d2, refPeriodStart: refStart, refPeriodEnd: refEnd); |
| 141 | return impliedRate(compound, resultDC, comp, freq, t); |
| 142 | } |
| 143 | //@} |
| 144 | |
| 145 | //! \name equivalent rate calculations |
| 146 | //@{ |
| 147 | |
| 148 | //! equivalent interest rate for a compounding period t. |
| 149 | /*! The resulting InterestRate shares the same implicit |
| 150 | day-counting rule of the original InterestRate instance. |
| 151 | |
| 152 | \warning Time must be measured using the InterestRate's |
| 153 | own day counter. |
| 154 | */ |
| 155 | InterestRate equivalentRate(Compounding comp, |
| 156 | Frequency freq, |
| 157 | Time t) const { |
| 158 | return impliedRate(compound: compoundFactor(t), resultDC: dc_, comp, freq, t); |
| 159 | } |
| 160 | |
| 161 | //! equivalent rate for a compounding period between two dates |
| 162 | /*! The resulting rate is calculated taking the required |
| 163 | day-counting rule into account. |
| 164 | */ |
| 165 | InterestRate equivalentRate(const DayCounter& resultDC, |
| 166 | Compounding comp, |
| 167 | Frequency freq, |
| 168 | Date d1, |
| 169 | Date d2, |
| 170 | const Date& refStart = Date(), |
| 171 | const Date& refEnd = Date()) const { |
| 172 | QL_REQUIRE(d2>=d1, |
| 173 | "d1 (" << d1 << ") " |
| 174 | "later than d2 (" << d2 << ")" ); |
| 175 | Time t1 = dc_.yearFraction(d1, d2, refPeriodStart: refStart, refPeriodEnd: refEnd); |
| 176 | Time t2 = resultDC.yearFraction(d1, d2, refPeriodStart: refStart, refPeriodEnd: refEnd); |
| 177 | return impliedRate(compound: compoundFactor(t: t1), resultDC, comp, freq, t: t2); |
| 178 | } |
| 179 | //@} |
| 180 | private: |
| 181 | Rate r_; |
| 182 | DayCounter dc_; |
| 183 | Compounding comp_; |
| 184 | bool freqMakesSense_; |
| 185 | Real freq_; |
| 186 | }; |
| 187 | |
| 188 | /*! \relates InterestRate */ |
| 189 | std::ostream& operator<<(std::ostream&, |
| 190 | const InterestRate&); |
| 191 | |
| 192 | } |
| 193 | |
| 194 | #endif |
| 195 | |