/src/quantlib/ql/interestrate.hpp
Line | Count | Source |
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 | | <https://www.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 | 0 | operator Rate() const { return r_; } |
52 | | //@} |
53 | | //! \name inspectors |
54 | | //@{ |
55 | 139k | Rate rate() const { return r_; } |
56 | 139k | const DayCounter& dayCounter() const { return dc_; } |
57 | 139k | Compounding compounding() const { return comp_; } |
58 | 139k | Frequency frequency() const { |
59 | 139k | return freqMakesSense_ ? Frequency(Integer(freq_)) : NoFrequency; |
60 | 139k | } |
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 | 0 | DiscountFactor discountFactor(Time t) const { |
70 | 0 | return 1.0/compoundFactor(t); |
71 | 0 | } |
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 | 0 | const Date& refEnd = Date()) const { |
78 | 0 | QL_REQUIRE(d2>=d1, |
79 | 0 | "d1 (" << d1 << ") " |
80 | 0 | "later than d2 (" << d2 << ")"); |
81 | 0 | Time t = dc_.yearFraction(d1, d2, refStart, refEnd); |
82 | 0 | return discountFactor(t); |
83 | 0 | } |
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 | 25.1M | const Date& refEnd = Date()) const { |
102 | 25.1M | QL_REQUIRE(d2>=d1, |
103 | 25.1M | "d1 (" << d1 << ") " |
104 | 25.1M | "later than d2 (" << d2 << ")"); |
105 | 25.1M | Time t = dc_.yearFraction(d1, d2, refStart, refEnd); |
106 | 25.1M | return compoundFactor(t); |
107 | 25.1M | } |
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 | 0 | const Date& refEnd = Date()) { |
137 | 0 | QL_REQUIRE(d2>=d1, |
138 | 0 | "d1 (" << d1 << ") " |
139 | 0 | "later than d2 (" << d2 << ")"); |
140 | 0 | Time t = resultDC.yearFraction(d1, d2, refStart, refEnd); |
141 | 0 | return impliedRate(compound, resultDC, comp, freq, t); |
142 | 0 | } Unexecuted instantiation: QuantLib::InterestRate::impliedRate(double, QuantLib::DayCounter const&, QuantLib::Compounding, QuantLib::Frequency, QuantLib::Date const&, QuantLib::Date const&, QuantLib::Date const&, QuantLib::Date const&) Unexecuted instantiation: QuantLib::InterestRate::impliedRate(double, QuantLib::DayCounter const&, QuantLib::Compounding, QuantLib::Frequency, QuantLib::Date const&, QuantLib::Date const&, QuantLib::Date const&, QuantLib::Date const&) |
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 | 0 | Time t) const { |
158 | 0 | return impliedRate(compoundFactor(t), dc_, comp, freq, t); |
159 | 0 | } |
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 | 0 | const Date& refEnd = Date()) const { |
172 | 0 | QL_REQUIRE(d2>=d1, |
173 | 0 | "d1 (" << d1 << ") " |
174 | 0 | "later than d2 (" << d2 << ")"); |
175 | 0 | Time t1 = dc_.yearFraction(d1, d2, refStart, refEnd); |
176 | 0 | Time t2 = resultDC.yearFraction(d1, d2, refStart, refEnd); |
177 | 0 | return impliedRate(compoundFactor(t1), resultDC, comp, freq, t2); |
178 | 0 | } Unexecuted instantiation: QuantLib::InterestRate::equivalentRate(QuantLib::DayCounter const&, QuantLib::Compounding, QuantLib::Frequency, QuantLib::Date, QuantLib::Date, QuantLib::Date const&, QuantLib::Date const&) const Unexecuted instantiation: QuantLib::InterestRate::equivalentRate(QuantLib::DayCounter const&, QuantLib::Compounding, QuantLib::Frequency, QuantLib::Date, QuantLib::Date, QuantLib::Date const&, QuantLib::Date const&) const |
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 |