QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
inflationtermstructure.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) 2007, 2009 Chris Kenyon
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
22#include <utility>
23
24namespace QuantLib {
25
27
29 Date baseDate,
30 Frequency frequency,
31 const DayCounter& dayCounter,
32 ext::shared_ptr<Seasonality> seasonality,
33 Rate baseRate)
34 : TermStructure(dayCounter), seasonality_(std::move(seasonality)),
35 frequency_(frequency), baseRate_(baseRate), baseDate_(baseDate) {
36 if (seasonality_ != nullptr) {
37 QL_REQUIRE(seasonality_->isConsistent(*this),
38 "Seasonality inconsistent with inflation term structure");
39 }
40 }
41
43 const Date& referenceDate,
44 Date baseDate,
45 Frequency frequency,
46 const DayCounter& dayCounter,
47 ext::shared_ptr<Seasonality> seasonality,
48 Rate baseRate)
49 : TermStructure(referenceDate, Calendar(), dayCounter), seasonality_(std::move(seasonality)),
50 frequency_(frequency), baseRate_(baseRate), baseDate_(baseDate) {
51 if (seasonality_ != nullptr) {
52 QL_REQUIRE(seasonality_->isConsistent(*this),
53 "Seasonality inconsistent with inflation term structure");
54 }
55 }
56
58 Natural settlementDays,
59 const Calendar& calendar,
60 Date baseDate,
61 Frequency frequency,
62 const DayCounter& dayCounter,
63 ext::shared_ptr<Seasonality> seasonality,
64 Rate baseRate)
65 : TermStructure(settlementDays, calendar, dayCounter), seasonality_(std::move(seasonality)),
66 frequency_(frequency), baseRate_(baseRate), baseDate_(baseDate) {
67 if (seasonality_ != nullptr) {
68 QL_REQUIRE(seasonality_->isConsistent(*this),
69 "Seasonality inconsistent with inflation term structure");
70 }
71 }
72
74
76 return baseDate_;
77 }
78
80 const ext::shared_ptr<Seasonality>& seasonality) {
81 // always reset, whether with null or new pointer
83 if (seasonality_ != nullptr) {
84 QL_REQUIRE(seasonality_->isConsistent(*this),
85 "Seasonality inconsistent with inflation term structure");
86 }
87 update();
88 }
89
90
92 bool extrapolate) const {
94 "date (" << d << ") is before base date (" << baseDate() << ")");
95 QL_REQUIRE(extrapolate || allowsExtrapolation() || d <= maxDate(),
96 "date (" << d << ") is past max curve date ("
97 << maxDate() << ")");
98 }
99
101 bool extrapolate) const {
103 "time (" << t << ") is before base date");
104 QL_REQUIRE(extrapolate || allowsExtrapolation() || t <= maxTime(),
105 "time (" << t << ") is past max curve time ("
106 << maxTime() << ")");
107 }
108
109
111 Date baseDate,
112 Frequency frequency,
113 const DayCounter& dayCounter,
114 const ext::shared_ptr<Seasonality>& seasonality)
115 : InflationTermStructure(baseDate, frequency, dayCounter, seasonality) {}
116
118 const Date& referenceDate,
119 Date baseDate,
120 Frequency frequency,
121 const DayCounter& dayCounter,
122 const ext::shared_ptr<Seasonality>& seasonality)
123 : InflationTermStructure(referenceDate, baseDate, frequency, dayCounter, seasonality) {}
124
126 Natural settlementDays,
127 const Calendar& calendar,
128 Date baseDate,
129 Frequency frequency,
130 const DayCounter& dayCounter,
131 const ext::shared_ptr<Seasonality>& seasonality)
132 : InflationTermStructure(settlementDays, calendar, baseDate, frequency, dayCounter, seasonality) {}
133
135 bool forceLinearInterpolation,
136 bool extrapolate) const {
137
138 Period useLag = instObsLag;
139 if (instObsLag == Period(-1,Days)) {
140 useLag = Period(0, Days);
141 }
142
144 if (forceLinearInterpolation) {
145 std::pair<Date,Date> dd = inflationPeriod(d-useLag, frequency());
146 dd.second = dd.second + Period(1,Days);
147 Real dp = dd.second - dd.first;
148 Real dt = d - dd.first;
149 // if we are interpolating we only check the exact point
150 // this prevents falling off the end at curve maturity
152 Time t1 = timeFromReference(dd.first);
153 Time t2 = timeFromReference(dd.second);
154 Rate z1 = zeroRateImpl(t1);
155 Rate z2 = zeroRateImpl(t2);
156 zeroRate = z1 + (z2-z1) * (dt/dp);
157 } else {
158 std::pair<Date,Date> dd = inflationPeriod(d-useLag, frequency());
159 InflationTermStructure::checkRange(dd.first, extrapolate);
160 Time t = timeFromReference(dd.first);
162 }
163
164 if (hasSeasonality()) {
165 zeroRate = seasonality()->correctZeroRate(d-useLag, zeroRate, *this);
166 }
167 return zeroRate;
168 }
169
171 bool extrapolate) const {
172 checkRange(t, extrapolate);
173 return zeroRateImpl(t);
174 }
175
176
178
180 Date baseDate,
181 Rate baseYoYRate,
182 Frequency frequency,
183 const DayCounter& dayCounter,
184 const ext::shared_ptr<Seasonality> &seasonality)
185 : InflationTermStructure(baseDate, frequency, dayCounter, seasonality, baseYoYRate) {}
186
188 const Date& referenceDate,
189 Date baseDate,
190 Rate baseYoYRate,
191 Frequency frequency,
192 const DayCounter& dayCounter,
193 const ext::shared_ptr<Seasonality> &seasonality)
194 : InflationTermStructure(referenceDate, baseDate, frequency, dayCounter, seasonality, baseYoYRate) {}
195
197 Natural settlementDays,
198 const Calendar& calendar,
199 Date baseDate,
200 Rate baseYoYRate,
201 Frequency frequency,
202 const DayCounter& dayCounter,
203 const ext::shared_ptr<Seasonality> &seasonality)
204 : InflationTermStructure(settlementDays, calendar, baseDate, frequency, dayCounter, seasonality, baseYoYRate) {}
205
207 Date baseDate,
208 Rate baseYoYRate,
209 Frequency frequency,
210 bool indexIsInterpolated,
211 const DayCounter& dayCounter,
212 const ext::shared_ptr<Seasonality> &seasonality)
213 : YoYInflationTermStructure(baseDate, baseYoYRate, frequency, dayCounter, seasonality) {
215 }
216
218 const Date& referenceDate,
219 Date baseDate,
220 Rate baseYoYRate,
221 Frequency frequency,
222 bool indexIsInterpolated,
223 const DayCounter& dayCounter,
224 const ext::shared_ptr<Seasonality> &seasonality)
225 : YoYInflationTermStructure(referenceDate, baseDate, baseYoYRate,
226 frequency, dayCounter, seasonality) {
228 }
229
231 Natural settlementDays,
232 const Calendar& calendar,
233 Date baseDate,
234 Rate baseYoYRate,
235 Frequency frequency,
236 bool indexIsInterpolated,
237 const DayCounter& dayCounter,
238 const ext::shared_ptr<Seasonality> &seasonality)
239 : YoYInflationTermStructure(settlementDays, calendar, baseDate, baseYoYRate,
240 frequency, dayCounter, seasonality) {
242 }
243
245
247 bool forceLinearInterpolation,
248 bool extrapolate) const {
249
250 Period useLag = instObsLag;
251 if (instObsLag == Period(-1,Days)) {
252 useLag = Period(0, Days);
253 }
254
256 if (forceLinearInterpolation) {
257 std::pair<Date,Date> dd = inflationPeriod(d-useLag, frequency());
258 dd.second = dd.second + Period(1,Days);
259 Real dp = dd.second - dd.first;
260 Real dt = (d-useLag) - dd.first;
261 // if we are interpolating we only check the exact point
262 // this prevents falling off the end at curve maturity
264 Time t1 = timeFromReference(dd.first);
265 Time t2 = timeFromReference(dd.second);
266 Rate y1 = yoyRateImpl(t1);
267 Rate y2 = yoyRateImpl(t2);
268 yoyRate = y1 + (y2-y1) * (dt/dp);
269 } else {
271 if (indexIsInterpolated()) {
272 InflationTermStructure::checkRange(d-useLag, extrapolate);
273 Time t = timeFromReference(d-useLag);
275 } else {
276 std::pair<Date,Date> dd = inflationPeriod(d-useLag, frequency());
277 InflationTermStructure::checkRange(dd.first, extrapolate);
278 Time t = timeFromReference(dd.first);
280 }
282 }
283
284 if (hasSeasonality()) {
285 yoyRate = seasonality()->correctYoYRate(d-useLag, yoyRate, *this);
286 }
287 return yoyRate;
288 }
289
291 bool extrapolate) const {
292 checkRange(t, extrapolate);
293 return yoyRateImpl(t);
294 }
295
296
297
298
299 std::pair<Date,Date> inflationPeriod(const Date& d,
300 Frequency frequency) {
301
302 Month month = d.month();
303 Year year = d.year();
304
305 Month startMonth, endMonth;
306 switch (frequency) {
307 case Annual:
308 startMonth = January;
309 endMonth = December;
310 break;
311 case Semiannual:
312 if (month <= June) {
313 startMonth = January;
314 endMonth = June;
315 } else {
316 startMonth = July;
317 endMonth = December;
318 }
319 break;
320 case Quarterly:
321 if (month <= March) {
322 startMonth = January;
323 endMonth = March;
324 } else if (month <= June) {
325 startMonth = April;
326 endMonth = June;
327 } else if (month <= September) {
328 startMonth = July;
329 endMonth = September;
330 } else {
331 startMonth = October;
332 endMonth = December;
333 }
334 break;
335 case Monthly:
336 startMonth = endMonth = month;
337 break;
338 default:
339 QL_FAIL("Frequency not handled: " << frequency);
340 break;
341 };
342
343 Date startDate = Date(1, startMonth, year);
344 Date endDate = Date::endOfMonth(Date(1, endMonth, year));
345
346 return std::make_pair(startDate,endDate);
347 }
348
349
350 Time inflationYearFraction(Frequency f, bool indexIsInterpolated,
351 const DayCounter &dayCounter,
352 const Date &d1, const Date &d2) {
353
354 Time t=0;
355 if (indexIsInterpolated) {
356 // N.B. we do not use linear interpolation between flat
357 // fixing forecasts for forecasts. This avoids awkwardnesses
358 // when bootstrapping the inflation curve.
359 t = dayCounter.yearFraction(d1, d2);
360 } else {
361 // I.e. fixing is constant for the whole inflation period.
362 // Use the value for half way along the period.
363 // But the inflation time is the time between period starts
364 std::pair<Date,Date> limD1 = inflationPeriod(d1, f);
365 std::pair<Date,Date> limD2 = inflationPeriod(d2, f);
366 t = dayCounter.yearFraction(limD1.first, limD2.first);
367 }
368
369 return t;
370 }
371
372
373}
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
Month month() const
Definition: date.cpp:82
Year year() const
Definition: date.cpp:93
static Date endOfMonth(const Date &d)
last day of the month to which the given date belongs
Definition: date.hpp:424
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
bool allowsExtrapolation() const
tells whether extrapolation is enabled
Interface for inflation term structures.
ext::shared_ptr< Seasonality > seasonality_
void setSeasonality(const ext::shared_ptr< Seasonality > &seasonality)
ext::shared_ptr< Seasonality > seasonality() const
InflationTermStructure(Date baseDate, Frequency frequency, const DayCounter &dayCounter=DayCounter(), ext::shared_ptr< Seasonality > seasonality={}, Rate baseRate=Null< Rate >())
virtual Date baseDate() const
minimum (base) date
void checkRange(const Date &, bool extrapolate) const
Basic term-structure functionality.
void update() override
virtual Time maxTime() const
the latest time for which the curve can return values
virtual Date maxDate() const =0
the latest date for which the curve can return values
Time timeFromReference(const Date &date) const
date/time conversion
Base class for year-on-year inflation term structures.
YoYInflationTermStructure(Date baseDate, Rate baseYoYRate, Frequency frequency, const DayCounter &dayCounter, const ext::shared_ptr< Seasonality > &seasonality={})
virtual Rate yoyRateImpl(Time time) const =0
to be defined in derived classes
Rate yoyRate(const Date &d, const Period &instObsLag=Period(-1, Days), bool forceLinearInterpolation=false, bool extrapolate=false) const
year-on-year inflation rate.
ZeroInflationTermStructure(Date baseDate, Frequency frequency, const DayCounter &dayCounter, const ext::shared_ptr< Seasonality > &seasonality={})
virtual Rate zeroRateImpl(Time t) const =0
to be defined in derived classes
Rate zeroRate(const Date &d, const Period &instObsLag=Period(-1, Days), bool forceLinearInterpolation=false, bool extrapolate=false) const
zero-coupon inflation rate.
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Date d
Frequency
Frequency of events.
Definition: frequency.hpp:37
Integer Year
Year number.
Definition: date.hpp:87
Month
Month names.
Definition: date.hpp:57
@ Monthly
once a month
Definition: frequency.hpp:44
@ Annual
once a year
Definition: frequency.hpp:39
@ Quarterly
every third month
Definition: frequency.hpp:42
@ Semiannual
twice a year
Definition: frequency.hpp:40
@ December
Definition: date.hpp:68
@ January
Definition: date.hpp:57
@ July
Definition: date.hpp:63
@ March
Definition: date.hpp:59
@ April
Definition: date.hpp:60
@ October
Definition: date.hpp:66
@ June
Definition: date.hpp:62
@ September
Definition: date.hpp:65
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Rate
interest rates
Definition: types.hpp:70
base classes for inflation indexes
Base classes for inflation term structures.
Definition: any.hpp:37
std::pair< Date, Date > inflationPeriod(const Date &d, Frequency frequency)
utility function giving the inflation period for a given date
Time inflationYearFraction(Frequency f, bool indexIsInterpolated, const DayCounter &dayCounter, const Date &d1, const Date &d2)
STL namespace.
#define QL_DEPRECATED_DISABLE_WARNING
Definition: qldefines.hpp:216
#define QL_DEPRECATED_ENABLE_WARNING
Definition: qldefines.hpp:217