QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
yieldtermstructure.cpp
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, 2009 Ferdinando Ametrano
5 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
6 Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
24#include <utility>
25
26namespace QuantLib {
27
28 namespace {
29 // time interval used in finite differences
30 const Time dt = 0.0001;
31 }
32
34
36 const Calendar& cal,
37 const DayCounter& dc,
38 std::vector<Handle<Quote> > jumps,
39 const std::vector<Date>& jumpDates)
40 : TermStructure(referenceDate, cal, dc), jumps_(std::move(jumps)), jumpDates_(jumpDates),
41 jumpTimes_(jumpDates.size()), nJumps_(jumps_.size()) {
43 for (Size i=0; i<nJumps_; ++i)
45 }
46
48 const Calendar& cal,
49 const DayCounter& dc,
50 std::vector<Handle<Quote> > jumps,
51 const std::vector<Date>& jumpDates)
52 : TermStructure(settlementDays, cal, dc), jumps_(std::move(jumps)), jumpDates_(jumpDates),
53 jumpTimes_(jumpDates.size()), nJumps_(jumps_.size()) {
55 for (Size i=0; i<nJumps_; ++i)
57 }
58
59 void YieldTermStructure::setJumps(const Date& referenceDate) {
60 if (jumpDates_.empty() && !jumps_.empty()) { // turn of year dates
61 jumpDates_.resize(nJumps_);
62 jumpTimes_.resize(nJumps_);
64 for (Size i=0; i<nJumps_; ++i)
65 jumpDates_[i] = Date(31, December, y+i);
66 } else { // fixed dates
68 "mismatch between number of jumps (" << nJumps_ <<
69 ") and jump dates (" << jumpDates_.size() << ")");
70 }
71 for (Size i=0; i<nJumps_; ++i)
74 }
75
77 bool extrapolate) const {
78 checkRange(t, extrapolate);
79
80 if (jumps_.empty())
81 return discountImpl(t);
82
83 DiscountFactor jumpEffect = 1.0;
84 for (Size i=0; i<nJumps_; ++i) {
85 if (jumpTimes_[i]>0 && jumpTimes_[i]<t) {
86 QL_REQUIRE(jumps_[i]->isValid(),
87 "invalid " << io::ordinal(i+1) << " jump quote");
88 DiscountFactor thisJump = jumps_[i]->value();
89 QL_REQUIRE(thisJump > 0.0,
90 "invalid " << io::ordinal(i+1) << " jump value: " <<
91 thisJump);
92 jumpEffect *= thisJump;
93 }
94 }
95 return jumpEffect * discountImpl(t);
96 }
97
99 const DayCounter& dayCounter,
100 Compounding comp,
101 Frequency freq,
102 bool extrapolate) const {
104 if (t == 0) {
105 Real compound = 1.0/discount(dt, extrapolate);
106 // t has been calculated with a possibly different daycounter
107 // but the difference should not matter for very small times
108 return InterestRate::impliedRate(compound,
109 dayCounter, comp, freq,
110 dt);
111 }
112 Real compound = 1.0/discount(t, extrapolate);
113 return InterestRate::impliedRate(compound,
114 dayCounter, comp, freq,
115 referenceDate(), d);
116 }
117
119 Compounding comp,
120 Frequency freq,
121 bool extrapolate) const {
122 if (t==0.0) t = dt;
123 Real compound = 1.0/discount(t, extrapolate);
124 return InterestRate::impliedRate(compound,
125 dayCounter(), comp, freq,
126 t);
127 }
128
130 const Date& d2,
131 const DayCounter& dayCounter,
132 Compounding comp,
133 Frequency freq,
134 bool extrapolate) const {
135 if (d1==d2) {
136 checkRange(d1, extrapolate);
137 Time t1 = std::max(timeFromReference(d1) - dt/2.0, 0.0);
138 Time t2 = t1 + dt;
139 Real compound =
140 discount(t1, true)/discount(t2, true);
141 // times have been calculated with a possibly different daycounter
142 // but the difference should not matter for very small times
143 return InterestRate::impliedRate(compound,
144 dayCounter, comp, freq,
145 dt);
146 }
147 QL_REQUIRE(d1 < d2, d1 << " later than " << d2);
148 Real compound = discount(d1, extrapolate)/discount(d2, extrapolate);
149 return InterestRate::impliedRate(compound,
150 dayCounter, comp, freq,
151 d1, d2);
152 }
153
155 Time t2,
156 Compounding comp,
157 Frequency freq,
158 bool extrapolate) const {
159 Real compound;
160 if (t2==t1) {
161 checkRange(t1, extrapolate);
162 t1 = std::max(t1 - dt/2.0, 0.0);
163 t2 = t1 + dt;
164 compound = discount(t1, true)/discount(t2, true);
165 } else {
166 QL_REQUIRE(t2>t1, "t2 (" << t2 << ") < t1 (" << t2 << ")");
167 compound = discount(t1, extrapolate)/discount(t2, extrapolate);
168 }
169 return InterestRate::impliedRate(compound,
170 dayCounter(), comp, freq,
171 t2-t1);
172 }
173
176 Date newReference = Date();
177 try {
178 newReference = referenceDate();
179 if (newReference != latestReference_)
180 setJumps(newReference);
181 } catch (Error&) {
182 if (newReference == Date()) {
183 // the curve couldn't calculate the reference
184 // date. Most of the times, this is because some
185 // underlying handle wasn't set, so we can just absorb
186 // the exception and continue; the jumps will be set
187 // correctly when a valid underlying is set.
188 return;
189 } else {
190 // something else happened during the call to
191 // setJumps(), so we let the exception bubble up.
192 throw;
193 }
194 }
195 }
196
197}
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
Year year() const
Definition: date.cpp:93
day counter class
Definition: daycounter.hpp:44
Base error class.
Definition: errors.hpp:39
Shared handle to an observable.
Definition: handle.hpp:41
Concrete interest rate class.
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.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Basic term-structure functionality.
virtual const Date & referenceDate() const
the date at which discount = 1.0 and/or variance = 0.0
void update() override
Time timeFromReference(const Date &date) const
date/time conversion
void checkRange(const Date &d, bool extrapolate) const
date-range check
virtual DayCounter dayCounter() const
the day counter used for date/time conversion
DiscountFactor discount(const Date &d, bool extrapolate=false) const
YieldTermStructure(const DayCounter &dc=DayCounter())
InterestRate zeroRate(const Date &d, const DayCounter &resultDayCounter, Compounding comp, Frequency freq=Annual, bool extrapolate=false) const
std::vector< Handle< Quote > > jumps_
void setJumps(const Date &referenceDate)
InterestRate forwardRate(const Date &d1, const Date &d2, const DayCounter &resultDayCounter, Compounding comp, Frequency freq=Annual, bool extrapolate=false) const
virtual DiscountFactor discountImpl(Time) const =0
discount factor calculation
output manipulators
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
Frequency
Frequency of events.
Definition: frequency.hpp:37
Integer Year
Year number.
Definition: date.hpp:87
@ December
Definition: date.hpp:68
detail::ordinal_holder ordinal(Size)
outputs naturals as 1st, 2nd, 3rd...
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
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Compounding
Interest rate coumpounding rule.
Definition: compounding.hpp:32
STL namespace.
Interest-rate term structure.