QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
inflationindex.hpp
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 Chris Kenyon
5 Copyright (C) 2021 Ralf Konrad Eckel
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file inflationindex.hpp
22 \brief base classes for inflation indexes
23*/
24
25#ifndef quantlib_inflation_index_hpp
26#define quantlib_inflation_index_hpp
27
28#include <ql/currency.hpp>
29#include <ql/handle.hpp>
30#include <ql/index.hpp>
31#include <ql/indexes/region.hpp>
33
34namespace QuantLib {
35
36 class ZeroInflationIndex;
37
38 struct CPI {
39 //! when you observe an index, how do you interpolate between fixings?
41 AsIndex, //!< same interpolation as index
42 Flat, //!< flat from previous fixing
43 Linear //!< linearly between bracketing fixings
44 };
45
46 //! interpolated inflation fixing
47 /*! \param index The index whose fixing should be retrieved
48 \param date The date without lag; usually, the payment
49 date for some inflation-based coupon.
50 \param observationLag The observation lag to be subtracted from the
51 passed date; for instance, if the passed date is
52 in May and the lag is three months, the inflation
53 fixing from February (and March, in case of
54 interpolation) will be observed.
55 \param interpolationType The interpolation type (flat or linear)
56 */
57 static Real laggedFixing(const ext::shared_ptr<ZeroInflationIndex>& index,
58 const Date& date,
59 const Period& observationLag,
60 InterpolationType interpolationType);
61 };
62
63
64 //! Base class for inflation-rate indexes,
65 class InflationIndex : public Index, public Observer {
66 public:
67 InflationIndex(std::string familyName,
69 bool revised,
71 const Period& availabilitiyLag,
73
74 //! \name Index interface
75 //@{
76 std::string name() const override;
77
78 /*! Inflation indices do not have fixing calendars. An
79 inflation index value is valid for every day (including
80 weekends) of a calendar period. I.e. it uses the
81 NullCalendar as its fixing calendar.
82 */
83 Calendar fixingCalendar() const override;
84 bool isValidFixingDate(const Date&) const override { return true; }
85
86 /*! Forecasting index values requires an inflation term
87 structure. The inflation term structure (ITS) defines the
88 usual lag (not the index). I.e. an ITS is always relatve
89 to a base date that is earlier than its asof date. This
90 must be so because indices are available only with a lag.
91 However, the index availability lag only sets a minimum
92 lag for the ITS. An ITS may be relative to an earlier
93 date, e.g. an index may have a 2-month delay in
94 publication but the inflation swaps may take as their base
95 the index 3 months before.
96 */
97 Real fixing(const Date& fixingDate, bool forecastTodaysFixing = false) const override = 0;
98
99 /*! this method creates all the "fixings" for the relevant
100 period of the index. E.g. for monthly indices it will put
101 the same value in every calendar day in the month.
102 */
103 void addFixing(const Date& fixingDate, Rate fixing, bool forceOverwrite = false) override;
104 //@}
105
106 //! \name Observer interface
107 //@{
108 void update() override;
109 //@}
110
111 //! \name Inspectors
112 //@{
113 std::string familyName() const;
114 Region region() const;
115 bool revised() const;
116 /*! Forecasting index values using an inflation term structure
117 uses the interpolation of the inflation term structure
118 unless interpolation is set to false. In this case the
119 extrapolated values are constant within each period taking
120 the mid-period extrapolated value.
121 */
122
123 Frequency frequency() const;
124 /*! The availability lag describes when the index is
125 <i>available</i>, not how it is used. Specifically the
126 fixing for, say, January, may only be available in April
127 but the index will always return the index value
128 applicable for January as its January fixing (independent
129 of the lag in availability).
130 */
131 Period availabilityLag() const;
132 Currency currency() const;
133 //@}
134
135 protected:
137 std::string familyName_;
143
144 private:
145 std::string name_;
146 };
147
148
149 //! Base class for zero inflation indices.
151 public:
153 const std::string& familyName,
154 const Region& region,
155 bool revised,
157 const Period& availabilityLag,
158 const Currency& currency,
160
161 //! \name Index interface
162 //@{
163 /*! \warning the forecastTodaysFixing parameter (required by
164 the Index interface) is currently ignored.
165 */
166 Real fixing(const Date& fixingDate, bool forecastTodaysFixing = false) const override;
167 //@}
168 //! \name Other methods
169 //@{
170 Date lastFixingDate() const;
172 ext::shared_ptr<ZeroInflationIndex> clone(const Handle<ZeroInflationTermStructure>& h) const;
173 //@}
174 private:
175 bool needsForecast(const Date& fixingDate) const;
176 Real forecastFixing(const Date& fixingDate) const;
178 };
179
180
181 //! Base class for year-on-year inflation indices.
182 /*! These may be quoted indices published on, say, Bloomberg, or can be
183 defined as the ratio of an index at different time points.
184 */
186 public:
187 //! \name Constructors
188 //@{
189 //! Constructor for year-on-year indices defined as a ratio.
190 /*! An index build with this constructor doesn't need to store
191 past fixings of its own; they will be calculated as a
192 ratio from the past fixings stored in the underlying index.
193 */
195 const ext::shared_ptr<ZeroInflationIndex>& underlyingIndex,
196 bool interpolated,
198
199 //! Constructor for quoted year-on-year indices.
200 /*! An index built with this constructor needs its past
201 fixings (i.e., the past year-on-year values) to be stored
202 via the `addFixing` or `addFixings` method.
203 */
205 const std::string& familyName,
206 const Region& region,
207 bool revised,
208 bool interpolated,
210 const Period& availabilityLag,
211 const Currency& currency,
213
214 //! Old generic constructor for year-on-year indices.
215 /*! An index built with this constructor needs its past
216 fixings to be stored via the `addFixing` or `addFixings`
217 method. Care must be taken about what to store: if
218 `ratio` is false, the stored values must be the
219 year-on-year values; if `ratio` is true, they must be the
220 past fixings of the underlying index.
221
222 \deprecated Use one of the other constructors instead.
223 Deprecated in version 1.31.
224 */
227 const std::string& familyName,
228 const Region& region,
229 bool revised,
230 bool interpolated,
231 bool ratio, // is this one a genuine index or a ratio?
233 const Period& availabilityLag,
234 const Currency& currency,
236 //@}
237
238 //! \name Index interface
239 //@{
240 /*! \warning the forecastTodaysFixing parameter (required by
241 the Index interface) is currently ignored.
242 */
243 Rate fixing(const Date& fixingDate, bool forecastTodaysFixing = false) const override;
244
245 //@}
246 //! \name Other methods
247 //@{
248 // Override the deprecation above
249 bool interpolated() const;
250 bool ratio() const;
251 ext::shared_ptr<ZeroInflationIndex> underlyingIndex() const;
253
254 ext::shared_ptr<YoYInflationIndex> clone(const Handle<YoYInflationTermStructure>& h) const;
255 //@}
256
257 protected:
258 // Override the deprecation above
260
261 private:
262 Rate forecastFixing(const Date& fixingDate) const;
263 bool ratio_;
264 ext::shared_ptr<ZeroInflationIndex> underlyingIndex_;
266 };
267
268
269 namespace detail {
270 namespace CPI {
271 // Returns either CPI::Flat or CPI::Linear depending on the combination of index and
272 // CPI::InterpolationType.
275
276
277 // checks whether the combination of index and CPI::InterpolationType results
278 // effectively in CPI::Linear
280 }
281 }
282
283
284 // inline
285
286 inline std::string InflationIndex::name() const {
287 return name_;
288 }
289
292 }
293
294 inline std::string InflationIndex::familyName() const {
295 return familyName_;
296 }
297
299 return region_;
300 }
301
302 inline bool InflationIndex::revised() const {
303 return revised_;
304 }
305
307 return frequency_;
308 }
309
311 return availabilityLag_;
312 }
313
315 return currency_;
316 }
317
320 return zeroInflation_;
321 }
322
324 return interpolated_;
325 }
326
327 inline bool YoYInflationIndex::ratio() const {
328 return ratio_;
329 }
330
331 inline ext::shared_ptr<ZeroInflationIndex> YoYInflationIndex::underlyingIndex() const {
332 return underlyingIndex_;
333 }
334
337 return yoyInflation_;
338 }
339
342 }
343
344}
345
346#endif
calendar class
Definition: calendar.hpp:61
Currency specification
Definition: currency.hpp:36
Concrete date class.
Definition: date.hpp:125
Shared handle to an observable.
Definition: handle.hpp:41
purely virtual base class for indexes
Definition: index.hpp:44
Base class for inflation-rate indexes,.
Calendar fixingCalendar() const override
std::string name() const override
Returns the name of the index.
void addFixing(const Date &fixingDate, Rate fixing, bool forceOverwrite=false) override
bool isValidFixingDate(const Date &) const override
returns TRUE if the fixing date is a valid one
Frequency frequency() const
Period availabilityLag() const
std::string familyName() const
Real fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const override=0
Linear-interpolation factory and traits
Object that gets notified when a given observable changes.
Definition: observable.hpp:116
Region class, used for inflation applicability.
Definition: region.hpp:36
Base class for year-on-year inflation indices.
Handle< YoYInflationTermStructure > yoyInflation_
ext::shared_ptr< YoYInflationIndex > clone(const Handle< YoYInflationTermStructure > &h) const
Rate forecastFixing(const Date &fixingDate) const
Handle< YoYInflationTermStructure > yoyInflationTermStructure() const
Rate fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const override
ext::shared_ptr< ZeroInflationIndex > underlyingIndex() const
ext::shared_ptr< ZeroInflationIndex > underlyingIndex_
Base class for zero inflation indices.
ext::shared_ptr< ZeroInflationIndex > clone(const Handle< ZeroInflationTermStructure > &h) const
Handle< ZeroInflationTermStructure > zeroInflation_
Real forecastFixing(const Date &fixingDate) const
Handle< ZeroInflationTermStructure > zeroInflationTermStructure() const
bool needsForecast(const Date &fixingDate) const
Real fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const override
Currency specification.
Frequency
Frequency of events.
Definition: frequency.hpp:37
QL_REAL Real
real number
Definition: types.hpp:50
Real Rate
interest rates
Definition: types.hpp:70
Globally accessible relinkable pointer.
virtual base class for indexes
Base classes for inflation term structures.
QuantLib::CPI::InterpolationType effectiveInterpolationType(const QuantLib::CPI::InterpolationType &type=QuantLib::CPI::AsIndex)
bool isInterpolated(const QuantLib::CPI::InterpolationType &type=QuantLib::CPI::AsIndex)
Definition: any.hpp:35
#define QL_DEPRECATED
Definition: qldefines.hpp:215
Region, i.e. geographical area, specification.
static Real laggedFixing(const ext::shared_ptr< ZeroInflationIndex > &index, const Date &date, const Period &observationLag, InterpolationType interpolationType)
interpolated inflation fixing
InterpolationType
when you observe an index, how do you interpolate between fixings?
@ AsIndex
same interpolation as index
@ Linear
linearly between bracketing fixings
@ Flat
flat from previous fixing