QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
interestrate.cpp
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#include <ql/interestrate.hpp>
21#include <ql/utilities/dataformatters.hpp>
22#include <iomanip>
23#include <sstream>
24#include <utility>
25
26namespace QuantLib {
27
28 // constructors
29
31 : r_(Null<Real>()) {}
32
34 : r_(r), dc_(std::move(dc)), comp_(comp), freqMakesSense_(false) {
35
37 freqMakesSense_ = true;
38 QL_REQUIRE(freq!=Once && freq!=NoFrequency,
39 "frequency not allowed for this interest rate");
40 freq_ = Real(freq);
41 }
42 }
43
45
46 QL_REQUIRE(t>=0.0, "negative time (" << t << ") not allowed");
47 QL_REQUIRE(r_ != Null<Rate>(), "null interest rate");
48 switch (comp_) {
49 case Simple:
50 return 1.0 + r_*t;
51 case Compounded:
52 return std::pow(1.0+r_/freq_, freq_*t);
53 case Continuous:
54 return std::exp(r_*t);
56 if (t<=1.0/Real(freq_))
57 return 1.0 + r_*t;
58 else
59 return std::pow(1.0+r_/freq_, freq_*t);
61 if (t>1.0/Real(freq_))
62 return 1.0 + r_*t;
63 else
64 return std::pow(1.0+r_/freq_, freq_*t);
65 default:
66 QL_FAIL("unknown compounding convention");
67 }
68 }
69
71 const DayCounter& resultDC,
72 Compounding comp,
73 Frequency freq,
74 Time t) {
75
76 QL_REQUIRE(compound>0.0, "positive compound factor required");
77
78 Rate r;
79 if (compound==1.0) {
80 QL_REQUIRE(t>=0.0, "non negative time (" << t << ") required");
81 r = 0.0;
82 } else {
83 QL_REQUIRE(t>0.0, "positive time (" << t << ") required");
84 switch (comp) {
85 case Simple:
86 r = (compound - 1.0)/t;
87 break;
88 case Compounded:
89 r = (std::pow(compound, 1.0/(Real(freq)*t))-1.0)*Real(freq);
90 break;
91 case Continuous:
92 r = std::log(compound)/t;
93 break;
95 if (t<=1.0/Real(freq))
96 r = (compound - 1.0)/t;
97 else
98 r = (std::pow(compound, 1.0/(Real(freq)*t))-1.0)*Real(freq);
99 break;
101 if (t>1.0/Real(freq))
102 r = (compound - 1.0)/t;
103 else
104 r = (std::pow(compound, 1.0/(Real(freq)*t))-1.0)*Real(freq);
105 break;
106 default:
107 QL_FAIL("unknown compounding convention ("
108 << Integer(comp) << ")");
109 }
110 }
111 return InterestRate(r, resultDC, comp, freq);
112 }
113
114
115 std::ostream& operator<<(std::ostream& out, const InterestRate& ir) {
116 if (ir.rate() == Null<Rate>())
117 return out << "null interest rate";
118
119 out << io::rate(ir.rate()) << " " << ir.dayCounter().name() << " ";
120 switch (ir.compounding()) {
121 case Simple:
122 out << "simple compounding";
123 break;
124 case Compounded:
125 switch (ir.frequency()) {
126 case NoFrequency:
127 case Once:
128 QL_FAIL(ir.frequency() << " frequency not allowed "
129 "for this interest rate");
130 default:
131 out << ir.frequency() <<" compounding";
132 }
133 break;
134 case Continuous:
135 out << "continuous compounding";
136 break;
138 switch (ir.frequency()) {
139 case NoFrequency:
140 case Once:
141 QL_FAIL(ir.frequency() << " frequency not allowed "
142 "for this interest rate");
143 default:
144 out << "simple compounding up to "
145 << Integer(12/ir.frequency()) << " months, then "
146 << ir.frequency() << " compounding";
147 }
148 break;
150 switch (ir.frequency()) {
151 case NoFrequency:
152 case Once:
153 QL_FAIL(ir.frequency() << " frequency not allowed "
154 "for this interest rate");
155 default:
156 out << "compounding up to "
157 << Integer(12/ir.frequency()) << " months, then "
158 << ir.frequency() << " simple compounding";
159 }
160 break;
161 default:
162 QL_FAIL("unknown compounding convention ("
163 << Integer(ir.compounding()) << ")");
164 }
165 return out;
166 }
167
168}
day counter class
Definition: daycounter.hpp:44
std::string name() const
Returns the name of the day counter.
Definition: daycounter.hpp:117
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()
Default constructor returning a null interest rate.
Real compoundFactor(Time t) const
compound factor implied by the rate compounded at time t.
Frequency frequency() const
template class providing a null value for a given type.
Definition: null.hpp:76
Frequency
Frequency of events.
Definition: frequency.hpp:37
@ Once
only once, e.g., a zero-coupon
Definition: frequency.hpp:38
@ NoFrequency
null frequency
Definition: frequency.hpp:37
detail::percent_holder rate(Rate)
output rates and spreads as percentages
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
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
@ CompoundedThenSimple
Compounded up to the first period then Simple.
Definition: compounding.hpp:36
@ SimpleThenCompounded
Simple up to the first period then Compounded.
Definition: compounding.hpp:35
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
STL namespace.