/src/quantlib/ql/termstructures/inflationtermstructure.hpp
Line | Count | Source |
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 | | <https://www.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 | | /*! \file inflationtermstructure.hpp |
21 | | \brief Base classes for inflation term structures. |
22 | | */ |
23 | | |
24 | | #ifndef quantlib_inflation_termstructure_hpp |
25 | | #define quantlib_inflation_termstructure_hpp |
26 | | |
27 | | #include <ql/termstructures/yieldtermstructure.hpp> |
28 | | #include <ql/termstructures/inflation/seasonality.hpp> |
29 | | |
30 | | namespace QuantLib { |
31 | | |
32 | | class InflationIndex; |
33 | | |
34 | | //! Interface for inflation term structures. |
35 | | /*! \ingroup inflationtermstructures */ |
36 | | class InflationTermStructure : public TermStructure { |
37 | | public: |
38 | | //! \name Constructors |
39 | | //@{ |
40 | | InflationTermStructure(Date baseDate, |
41 | | Frequency frequency, |
42 | | const DayCounter& dayCounter = DayCounter(), |
43 | | ext::shared_ptr<Seasonality> seasonality = {}, |
44 | | Rate baseRate = Null<Rate>()); |
45 | | |
46 | | InflationTermStructure(const Date& referenceDate, |
47 | | Date baseDate, |
48 | | Frequency frequency, |
49 | | const DayCounter& dayCounter = DayCounter(), |
50 | | ext::shared_ptr<Seasonality> seasonality = {}, |
51 | | Rate baseRate = Null<Rate>()); |
52 | | |
53 | | InflationTermStructure(Natural settlementDays, |
54 | | const Calendar& calendar, |
55 | | Date baseDate, |
56 | | Frequency frequency, |
57 | | const DayCounter& dayCounter = DayCounter(), |
58 | | ext::shared_ptr<Seasonality> seasonality = {}, |
59 | | Rate baseRate = Null<Rate>()); |
60 | | //@} |
61 | | |
62 | | QL_DEPRECATED_DISABLE_WARNING |
63 | | ~InflationTermStructure() override = default; |
64 | | QL_DEPRECATED_ENABLE_WARNING |
65 | | |
66 | | //! \name Inflation interface |
67 | | //@{ |
68 | | /*! \deprecated Do not use; inflation curves always have an explicit |
69 | | base date now. |
70 | | Deprecated in version 1.39. |
71 | | */ |
72 | | [[deprecated("Do not use; inflation curves always have an explicit base date now.")]] |
73 | | virtual Period observationLag() const; |
74 | | |
75 | | virtual Frequency frequency() const; |
76 | | virtual Rate baseRate() const; |
77 | | |
78 | | //! minimum (base) date |
79 | | /*! The last date for which we have information. */ |
80 | | virtual Date baseDate() const; |
81 | | |
82 | | /*! \deprecated Do not use; inflation curves always have an explicit |
83 | | base date now. |
84 | | Deprecated in version 1.39. |
85 | | */ |
86 | | [[deprecated("Do not use; inflation curves always have an explicit base date now.")]] |
87 | 0 | bool hasExplicitBaseDate() const { |
88 | 0 | return true; |
89 | 0 | } |
90 | | //@} |
91 | | |
92 | | //! \name Seasonality |
93 | | //@{ |
94 | | void setSeasonality(const ext::shared_ptr<Seasonality>& seasonality); |
95 | | ext::shared_ptr<Seasonality> seasonality() const; |
96 | | bool hasSeasonality() const; |
97 | | //@} |
98 | | |
99 | | protected: |
100 | | void checkRange(const Date&, |
101 | | bool extrapolate) const; |
102 | | void checkRange(Time t, |
103 | | bool extrapolate) const; |
104 | | |
105 | | ext::shared_ptr<Seasonality> seasonality_; |
106 | | |
107 | | /*! \deprecated Do not use; inflation curves always have an explicit |
108 | | base date now. |
109 | | Deprecated in version 1.39. |
110 | | */ |
111 | | [[deprecated("Do not use; inflation curves always have an explicit base date now.")]] |
112 | | Period observationLag_; |
113 | | |
114 | | Frequency frequency_; |
115 | | mutable Rate baseRate_; |
116 | | // Can be set by subclasses that don't have baseDate available in constructors. |
117 | | Date baseDate_; |
118 | | }; |
119 | | |
120 | | //! Interface for zero inflation term structures. |
121 | | class ZeroInflationTermStructure : public InflationTermStructure { |
122 | | public: |
123 | | //! \name Constructors |
124 | | //@{ |
125 | | ZeroInflationTermStructure(Date baseDate, |
126 | | Frequency frequency, |
127 | | const DayCounter& dayCounter, |
128 | | const ext::shared_ptr<Seasonality>& seasonality = {}); |
129 | | |
130 | | ZeroInflationTermStructure(const Date& referenceDate, |
131 | | Date baseDate, |
132 | | Frequency frequency, |
133 | | const DayCounter& dayCounter, |
134 | | const ext::shared_ptr<Seasonality>& seasonality = {}); |
135 | | |
136 | | ZeroInflationTermStructure(Natural settlementDays, |
137 | | const Calendar& calendar, |
138 | | Date baseDate, |
139 | | Frequency frequency, |
140 | | const DayCounter& dayCounter, |
141 | | const ext::shared_ptr<Seasonality>& seasonality = {}); |
142 | | //@} |
143 | | |
144 | | //! \name Inspectors |
145 | | //@{ |
146 | | //! zero-coupon inflation rate. |
147 | | /*! Essentially the fair rate for a zero-coupon inflation swap |
148 | | (by definition), i.e. the zero term structure uses yearly |
149 | | compounding, which is assumed for ZCIIS instrument quotes. |
150 | | |
151 | | \note by default you get the same as lag and interpolation |
152 | | as the term structure. |
153 | | If you want to get predictions of RPI/CPI/etc then use an |
154 | | index. |
155 | | */ |
156 | | Rate zeroRate(const Date& d, const Period& instObsLag = Period(-1,Days), |
157 | | bool forceLinearInterpolation = false, |
158 | | bool extrapolate = false) const; |
159 | | //! zero-coupon inflation rate. |
160 | | /*! \warning Since inflation is highly linked to dates (lags, |
161 | | interpolation, months for seasonality, etc) this |
162 | | method cannot account for all effects. If you |
163 | | call it, You'll have to manage lag, seasonality |
164 | | etc. yourself. |
165 | | */ |
166 | | Rate zeroRate(Time t, |
167 | | bool extrapolate = false) const; |
168 | | //@} |
169 | | protected: |
170 | | //! to be defined in derived classes |
171 | | virtual Rate zeroRateImpl(Time t) const = 0; |
172 | | }; |
173 | | |
174 | | |
175 | | //! Base class for year-on-year inflation term structures. |
176 | | class YoYInflationTermStructure : public InflationTermStructure { |
177 | | public: |
178 | | //! \name Constructors |
179 | | //@{ |
180 | | YoYInflationTermStructure(Date baseDate, |
181 | | Rate baseYoYRate, |
182 | | Frequency frequency, |
183 | | const DayCounter& dayCounter, |
184 | | const ext::shared_ptr<Seasonality>& seasonality = {}); |
185 | | |
186 | | YoYInflationTermStructure(const Date& referenceDate, |
187 | | Date baseDate, |
188 | | Rate baseYoYRate, |
189 | | Frequency frequency, |
190 | | const DayCounter& dayCounter, |
191 | | const ext::shared_ptr<Seasonality>& seasonality = {}); |
192 | | |
193 | | YoYInflationTermStructure(Natural settlementDays, |
194 | | const Calendar& calendar, |
195 | | Date baseDate, |
196 | | Rate baseYoYRate, |
197 | | Frequency frequency, |
198 | | const DayCounter& dayCounter, |
199 | | const ext::shared_ptr<Seasonality>& seasonality = {}); |
200 | | |
201 | | /*! \deprecated Use an overload with an explicit base date and without indexIsInterpolated. |
202 | | Deprecated in version 1.37. |
203 | | */ |
204 | | [[deprecated("Use an overload with an explicit base date and without indexIsInterpolated")]] |
205 | | YoYInflationTermStructure(Date baseDate, |
206 | | Rate baseYoYRate, |
207 | | Frequency frequency, |
208 | | bool indexIsInterpolated, |
209 | | const DayCounter& dayCounter, |
210 | | const ext::shared_ptr<Seasonality>& seasonality = {}); |
211 | | |
212 | | /*! \deprecated Use an overload with an explicit base date and without indexIsInterpolated. |
213 | | Deprecated in version 1.37. |
214 | | */ |
215 | | [[deprecated("Use an overload with an explicit base date and without indexIsInterpolated")]] |
216 | | YoYInflationTermStructure(const Date& referenceDate, |
217 | | Date baseDate, |
218 | | Rate baseYoYRate, |
219 | | Frequency frequency, |
220 | | bool indexIsInterpolated, |
221 | | const DayCounter& dayCounter, |
222 | | const ext::shared_ptr<Seasonality>& seasonality = {}); |
223 | | |
224 | | /*! \deprecated Use an overload with an explicit base date and without indexIsInterpolated. |
225 | | Deprecated in version 1.37. |
226 | | */ |
227 | | [[deprecated("Use an overload with an explicit base date and without indexIsInterpolated")]] |
228 | | YoYInflationTermStructure(Natural settlementDays, |
229 | | const Calendar& calendar, |
230 | | Date baseDate, |
231 | | Rate baseYoYRate, |
232 | | Frequency frequency, |
233 | | bool indexIsInterpolated, |
234 | | const DayCounter& dayCounter, |
235 | | const ext::shared_ptr<Seasonality>& seasonality = {}); |
236 | | //@} |
237 | | |
238 | | QL_DEPRECATED_DISABLE_WARNING |
239 | | ~YoYInflationTermStructure() override = default; |
240 | | QL_DEPRECATED_ENABLE_WARNING |
241 | | |
242 | | //! \name Inspectors |
243 | | //@{ |
244 | | //! year-on-year inflation rate. |
245 | | /*! The forceLinearInterpolation parameter is relative to the |
246 | | frequency of the TS. |
247 | | |
248 | | \note this is not the year-on-year swap (YYIIS) rate. |
249 | | */ |
250 | | Rate yoyRate(const Date& d, const Period& instObsLag = Period(-1,Days), |
251 | | bool forceLinearInterpolation = false, |
252 | | bool extrapolate = false) const; |
253 | | //! year-on-year inflation rate. |
254 | | /*! \warning Since inflation is highly linked to dates (lags, |
255 | | interpolation, months for seasonality, etc) this |
256 | | method cannot account for all effects. If you |
257 | | call it, You'll have to manage lag, seasonality |
258 | | etc. yourself. |
259 | | */ |
260 | | Rate yoyRate(Time t, |
261 | | bool extrapolate = false) const; |
262 | | //@} |
263 | | |
264 | | /*! \deprecated This method will disappear. When it does, the curve will behave as if it returned false. |
265 | | Deprecated in version 1.37. |
266 | | */ |
267 | | [[deprecated("This method will disappear. When it does, the curve will behave as if it returned false")]] |
268 | | virtual bool indexIsInterpolated() const; |
269 | | protected: |
270 | | //! to be defined in derived classes |
271 | | virtual Rate yoyRateImpl(Time time) const = 0; |
272 | | |
273 | | /*! \deprecated This data member will disappear. When it does, the curve will behave as if it was false. |
274 | | Deprecated in version 1.37. |
275 | | */ |
276 | | [[deprecated("This data member will disappear. When it does, the curve will behave as if it was false")]] |
277 | | bool indexIsInterpolated_ = false; |
278 | | }; |
279 | | |
280 | | |
281 | | //! utility function giving the inflation period for a given date |
282 | | std::pair<Date,Date> inflationPeriod(const Date&, |
283 | | Frequency); |
284 | | |
285 | | //! utility function giving the time between two dates depending on |
286 | | //! index frequency and interpolation, and a day counter |
287 | | Time inflationYearFraction(Frequency , |
288 | | bool indexIsInterpolated, |
289 | | const DayCounter&, |
290 | | const Date&, const Date&); |
291 | | |
292 | | |
293 | | // inline |
294 | | |
295 | 0 | inline Period InflationTermStructure::observationLag() const { |
296 | 0 | QL_DEPRECATED_DISABLE_WARNING |
297 | 0 | return observationLag_; |
298 | 0 | QL_DEPRECATED_ENABLE_WARNING |
299 | 0 | } |
300 | | |
301 | 0 | inline Frequency InflationTermStructure::frequency() const { |
302 | 0 | return frequency_; |
303 | 0 | } |
304 | | |
305 | 0 | inline Rate InflationTermStructure::baseRate() const { |
306 | 0 | QL_REQUIRE(baseRate_ != Null<Real>(), "base rate not available"); |
307 | 0 | return baseRate_; |
308 | 0 | } |
309 | | |
310 | 0 | inline ext::shared_ptr<Seasonality> InflationTermStructure::seasonality() const { |
311 | 0 | return seasonality_; |
312 | 0 | } |
313 | | |
314 | 0 | inline bool InflationTermStructure::hasSeasonality() const { |
315 | 0 | return static_cast<bool>(seasonality_); |
316 | 0 | } |
317 | | |
318 | 0 | inline bool YoYInflationTermStructure::indexIsInterpolated() const { |
319 | 0 | QL_DEPRECATED_DISABLE_WARNING |
320 | 0 | return indexIsInterpolated_; |
321 | 0 | QL_DEPRECATED_ENABLE_WARNING |
322 | 0 | } |
323 | | |
324 | | } |
325 | | |
326 | | #endif |