1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2004 Ferdinando Ametrano
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ql/interestrate.hpp>
#include <ql/utilities/dataformatters.hpp>
#include <sstream>
#include <iomanip>
namespace QuantLib {
// constructors
InterestRate::InterestRate()
: r_(Null<Real>()) {}
InterestRate::InterestRate(Rate r,
const DayCounter& dc,
Compounding comp,
Frequency freq)
: r_(r), dc_(dc), comp_(comp), freqMakesSense_(false) {
if (comp_==Compounded || comp_==SimpleThenCompounded) {
freqMakesSense_ = true;
QL_REQUIRE(freq!=Once && freq!=NoFrequency,
"frequency not allowed for this interest rate");
freq_ = Real(freq);
}
}
Real InterestRate::compoundFactor(Time t) const {
QL_REQUIRE(t>=0.0, "negative time not allowed");
QL_REQUIRE(r_ != Null<Rate>(), "null interest rate");
switch (comp_) {
case Simple:
return 1.0 + r_*t;
case Compounded:
return std::pow(1.0+r_/freq_, freq_*t);
case Continuous:
return std::exp(r_*t);
case SimpleThenCompounded:
if (t<=1.0/Real(freq_))
return 1.0 + r_*t;
else
return std::pow(1.0+r_/freq_, freq_*t);
default:
QL_FAIL("unknown compounding convention");
}
}
InterestRate InterestRate::impliedRate(Real compound,
const DayCounter& resultDC,
Compounding comp,
Frequency freq,
Time t) {
QL_REQUIRE(compound>0.0, "positive compound factor required");
Rate r;
if (compound==1.0) {
QL_REQUIRE(t>=0.0, "non negative time (" << t << ") required");
r = 0.0;
} else {
QL_REQUIRE(t>0.0, "positive time (" << t << ") required");
switch (comp) {
case Simple:
r = (compound - 1.0)/t;
break;
case Compounded:
r = (std::pow(compound, 1.0/(Real(freq)*t))-1.0)*Real(freq);
break;
case Continuous:
r = std::log(compound)/t;
break;
case SimpleThenCompounded:
if (t<=1.0/Real(freq))
r = (compound - 1.0)/t;
else
r = (std::pow(compound, 1.0/(Real(freq)*t))-1.0)*Real(freq);
break;
default:
QL_FAIL("unknown compounding convention ("
<< Integer(comp) << ")");
}
}
return InterestRate(r, resultDC, comp, freq);
}
std::ostream& operator<<(std::ostream& out, const InterestRate& ir) {
if (ir.rate() == Null<Rate>())
return out << "null interest rate";
out << io::rate(ir.rate()) << " " << ir.dayCounter().name() << " ";
switch (ir.compounding()) {
case Simple:
out << "simple compounding";
break;
case Compounded:
switch (ir.frequency()) {
case NoFrequency:
case Once:
QL_FAIL(ir.frequency() << " frequency not allowed "
"for this interest rate");
default:
out << ir.frequency() <<" compounding";
}
break;
case Continuous:
out << "continuous compounding";
break;
case SimpleThenCompounded:
switch (ir.frequency()) {
case NoFrequency:
case Once:
QL_FAIL(ir.frequency() << " frequency not allowed "
"for this interest rate");
default:
out << "simple compounding up to "
<< Integer(12/ir.frequency()) << " months, then "
<< ir.frequency() << " compounding";
}
break;
default:
QL_FAIL("unknown compounding convention ("
<< Integer(ir.compounding()) << ")");
}
return out;
}
}
|