QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
interestrate.hpp
Go to the documentation of this file.
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>
29
30namespace 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 */
41 public:
42 //! \name constructors
43 //@{
44 //! Default constructor returning a null interest rate.
46 //! Standard constructor
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_; }
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 */
70 return 1.0/compoundFactor(t);
71 }
72
73 //! discount factor implied by the rate compounded between two dates
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, refStart, 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 */
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 */
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, refStart, 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 */
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, refStart, 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 */
156 Frequency freq,
157 Time t) const {
158 return impliedRate(compoundFactor(t), 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 */
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, refStart, refEnd);
176 Time t2 = resultDC.yearFraction(d1, d2, refStart, refEnd);
177 return impliedRate(compoundFactor(t1), resultDC, comp, freq, t2);
178 }
179 //@}
180 private:
186 };
187
188 /*! \relates InterestRate */
189 std::ostream& operator<<(std::ostream&,
190 const InterestRate&);
191
192}
193
194#endif
Actual/365 (Fixed) day counter.
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
Concrete interest rate class.
const DayCounter & dayCounter() const
Compounding compounding() const
static InterestRate impliedRate(Real compound, const DayCounter &resultDC, Compounding comp, Frequency freq, Time t)
implied interest rate for a given compound factor at a given time.
InterestRate equivalentRate(const DayCounter &resultDC, Compounding comp, Frequency freq, Date d1, Date d2, const Date &refStart=Date(), const Date &refEnd=Date()) const
equivalent rate for a compounding period between two dates
static InterestRate impliedRate(Real compound, const DayCounter &resultDC, Compounding comp, Frequency freq, const Date &d1, const Date &d2, const Date &refStart=Date(), const Date &refEnd=Date())
implied rate for a given compound factor between two dates.
DiscountFactor discountFactor(Time t) const
discount factor implied by the rate compounded at time t.
InterestRate()
Default constructor returning a null interest rate.
Real compoundFactor(Time t) const
compound factor implied by the rate compounded at time t.
Real compoundFactor(const Date &d1, const Date &d2, const Date &refStart=Date(), const Date &refEnd=Date()) const
compound factor implied by the rate compounded between two dates
DiscountFactor discountFactor(const Date &d1, const Date &d2, const Date &refStart=Date(), const Date &refEnd=Date()) const
discount factor implied by the rate compounded between two dates
Frequency frequency() const
InterestRate equivalentRate(Compounding comp, Frequency freq, Time t) const
equivalent interest rate for a compounding period t.
Compounding enumeration.
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Frequency
Frequency of events.
Definition: frequency.hpp:37
@ NoFrequency
null frequency
Definition: frequency.hpp:37
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Real Rate
interest rates
Definition: types.hpp:70
Definition: any.hpp:35
Compounding
Interest rate coumpounding rule.
Definition: compounding.hpp:32
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
ext::shared_ptr< YieldTermStructure > r