| 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 | |
| 26 | namespace QuantLib { |
| 27 | |
| 28 | // constructors |
| 29 | |
| 30 | InterestRate::InterestRate() |
| 31 | : r_(Null<Real>()) {} |
| 32 | |
| 33 | InterestRate::InterestRate(Rate r, DayCounter dc, Compounding comp, Frequency freq) |
| 34 | : r_(r), dc_(std::move(dc)), comp_(comp), freqMakesSense_(false) { |
| 35 | |
| 36 | if (comp_==Compounded || comp_==SimpleThenCompounded || comp_==CompoundedThenSimple) { |
| 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 | |
| 44 | Real InterestRate::compoundFactor(Time t) const { |
| 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(x: 1.0+r_/freq_, y: freq_*t); |
| 53 | case Continuous: |
| 54 | return std::exp(x: r_*t); |
| 55 | case SimpleThenCompounded: |
| 56 | if (t<=1.0/Real(freq_)) |
| 57 | return 1.0 + r_*t; |
| 58 | else |
| 59 | return std::pow(x: 1.0+r_/freq_, y: freq_*t); |
| 60 | case CompoundedThenSimple: |
| 61 | if (t>1.0/Real(freq_)) |
| 62 | return 1.0 + r_*t; |
| 63 | else |
| 64 | return std::pow(x: 1.0+r_/freq_, y: freq_*t); |
| 65 | default: |
| 66 | QL_FAIL("unknown compounding convention" ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | InterestRate InterestRate::impliedRate(Real compound, |
| 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(x: compound, y: 1.0/(Real(freq)*t))-1.0)*Real(freq); |
| 90 | break; |
| 91 | case Continuous: |
| 92 | r = std::log(x: compound)/t; |
| 93 | break; |
| 94 | case SimpleThenCompounded: |
| 95 | if (t<=1.0/Real(freq)) |
| 96 | r = (compound - 1.0)/t; |
| 97 | else |
| 98 | r = (std::pow(x: compound, y: 1.0/(Real(freq)*t))-1.0)*Real(freq); |
| 99 | break; |
| 100 | case CompoundedThenSimple: |
| 101 | if (t>1.0/Real(freq)) |
| 102 | r = (compound - 1.0)/t; |
| 103 | else |
| 104 | r = (std::pow(x: compound, y: 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(r: 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; |
| 137 | case SimpleThenCompounded: |
| 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; |
| 149 | case CompoundedThenSimple: |
| 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 | } |
| 169 | |