QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
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 class YoYInflationIndex;
38
39 struct CPI {
40
41 //! when you observe an index, how do you interpolate between fixings?
43 AsIndex, //!< same interpolation as index
44 Flat, //!< flat from previous fixing
45 Linear //!< linearly between bracketing fixings
46 };
47
48 //! interpolated inflation fixing
49 /*! \param index The index whose fixing should be retrieved
50 \param date The date without lag; usually, the payment
51 date for some inflation-based coupon.
52 \param observationLag The observation lag to be subtracted from the
53 passed date; for instance, if the passed date is
54 in May and the lag is three months, the inflation
55 fixing from February (and March, in case of
56 interpolation) will be observed.
57 \param interpolationType The interpolation type (flat or linear)
58 */
59 static Real laggedFixing(const ext::shared_ptr<ZeroInflationIndex>& index,
60 const Date& date,
61 const Period& observationLag,
62 InterpolationType interpolationType);
63
64
65 //! interpolated year-on-year inflation rate
66 /*! \param index The index whose fixing should be retrieved
67 \param date The date without lag; usually, the payment
68 date for some inflation-based coupon.
69 \param observationLag The observation lag to be subtracted from the
70 passed date; for instance, if the passed date is
71 in May and the lag is three months, the year-on-year
72 rate from February (and March, in case of
73 interpolation) will be observed.
74 \param interpolationType The interpolation type (flat or linear)
75 */
76 static Real laggedYoYRate(const ext::shared_ptr<YoYInflationIndex>& index,
77 const Date& date,
78 const Period& observationLag,
79 InterpolationType interpolationType);
80 };
81
82
83 //! Base class for inflation-rate indexes,
84 class InflationIndex : public Index {
85 public:
86 InflationIndex(std::string familyName,
88 bool revised,
90 const Period& availabilitiyLag,
92
93 //! \name Index interface
94 //@{
95 std::string name() const override;
96
97 /*! Inflation indices are not associated to a particular day,
98 but to months or quarters. Therefore, they do not have
99 fixing calendars. Since we're forced by the base `Index`
100 interface to add one, this method returns a NullCalendar
101 instance.
102 */
103 Calendar fixingCalendar() const override;
104 bool isValidFixingDate(const Date&) const override { return true; }
105
106 /*! Forecasting index values requires an inflation term
107 structure, with a base date that is earlier than its asof
108 date. This must be so because indices are available only
109 with a lag. Usually, it makes sense for the base date to
110 be the first day of the month of the last published
111 fixing.
112 */
113 Real fixing(const Date& fixingDate, bool forecastTodaysFixing = false) const override = 0;
114
115 //! returns a past fixing at the given date
116 Real pastFixing(const Date& fixingDate) const override = 0;
117
118 void addFixing(const Date& fixingDate, Rate fixing, bool forceOverwrite = false) override;
119 //@}
120
121 //! \name Inspectors
122 //@{
123 std::string familyName() const;
124 Region region() const;
125 bool revised() const;
126 Frequency frequency() const;
127 /*! The availability lag describes when the index might be
128 available; for instance, the inflation value for January
129 may only be available in April. This doesn't mean that
130 that inflation value is considered as the April fixing; it
131 remains the January fixing, independently of the lag in
132 availability.
133 */
134 Period availabilityLag() const;
135 Currency currency() const;
136 //@}
137
138 protected:
140 std::string familyName_;
146
147 private:
148 std::string name_;
149 };
150
151
152 //! Base class for zero inflation indices.
154 public:
156 const std::string& familyName,
157 const Region& region,
158 bool revised,
160 const Period& availabilityLag,
161 const Currency& currency,
163
164 //! \name Index interface
165 //@{
166 /*! \warning the forecastTodaysFixing parameter (required by
167 the Index interface) is currently ignored.
168 */
169 Real fixing(const Date& fixingDate, bool forecastTodaysFixing = false) const override;
170 Real pastFixing(const Date& fixingDate) const override;
171 //@}
172 //! \name Other methods
173 //@{
174 Date lastFixingDate() const;
176 ext::shared_ptr<ZeroInflationIndex> clone(const Handle<ZeroInflationTermStructure>& h) const;
177 bool needsForecast(const Date& fixingDate) const;
178 //@}
179 private:
180 Real forecastFixing(const Date& fixingDate) const;
182 };
183
184
185 //! Base class for year-on-year inflation indices.
186 /*! These may be quoted indices published on, say, Bloomberg, or can be
187 defined as the ratio of an index at different time points.
188 */
190 public:
191 //! \name Constructors
192 //@{
193 //! Constructor for year-on-year indices defined as a ratio.
194 /*! An index build with this constructor won't store
195 past fixings of its own; they will be calculated as a
196 ratio from the past fixings stored in the underlying index.
197 */
198 explicit YoYInflationIndex(
199 const ext::shared_ptr<ZeroInflationIndex>& underlyingIndex,
201
202 /*! \deprecated Use the similar overload without the interpolated parameter.
203 Deprecated in version 1.38.
204 */
205 [[deprecated("Use the similar overload without the interpolated parameter")]]
207 const ext::shared_ptr<ZeroInflationIndex>& underlyingIndex,
208 bool interpolated,
210
211 //! Constructor for quoted year-on-year indices.
212 /*! An index built with this constructor needs its past
213 fixings (i.e., the past year-on-year values) to be stored
214 via the `addFixing` or `addFixings` method.
215 */
217 const std::string& familyName,
218 const Region& region,
219 bool revised,
221 const Period& availabilityLag,
222 const Currency& currency,
224
225 /*! \deprecated Use the similar overload without the interpolated parameter.
226 Deprecated in version 1.38.
227 */
228 [[deprecated("Use the similar overload without the interpolated parameter")]]
230 const std::string& familyName,
231 const Region& region,
232 bool revised,
233 bool interpolated,
235 const Period& availabilityLag,
236 const Currency& currency,
238 //@}
239
240 //! \name Index interface
241 //@{
242 /*! \warning the forecastTodaysFixing parameter (required by
243 the Index interface) is currently ignored.
244 */
245 Rate fixing(const Date& fixingDate, bool forecastTodaysFixing = false) const override;
246 Real pastFixing(const Date& fixingDate) const override;
247 //@}
248
249 //! \name Other methods
250 //@{
251 Date lastFixingDate() const;
252 bool interpolated() const;
253 bool ratio() const;
254 ext::shared_ptr<ZeroInflationIndex> underlyingIndex() const;
256
257 ext::shared_ptr<YoYInflationIndex> clone(const Handle<YoYInflationTermStructure>& h) const;
258 bool needsForecast(const Date& fixingDate) const;
259 //@}
260
261 protected:
263
264 private:
265 Rate forecastFixing(const Date& fixingDate) const;
266 bool ratio_;
267 ext::shared_ptr<ZeroInflationIndex> underlyingIndex_;
269 };
270
271
272 namespace detail::CPI {
273
274 // Returns either CPI::Flat or CPI::Linear depending on the combination of index and
275 // CPI::InterpolationType.
278
281 const ext::shared_ptr<YoYInflationIndex>& index);
282
283 // checks whether the combination of index and CPI::InterpolationType results
284 // effectively in CPI::Linear
286
288 const ext::shared_ptr<YoYInflationIndex>& index);
289
290 }
291
292
293 // inline
294
295 inline std::string InflationIndex::name() const {
296 return name_;
297 }
298
299 inline std::string InflationIndex::familyName() const {
300 return familyName_;
301 }
302
304 return region_;
305 }
306
307 inline bool InflationIndex::revised() const {
308 return revised_;
309 }
310
312 return frequency_;
313 }
314
316 return availabilityLag_;
317 }
318
320 return currency_;
321 }
322
325 return zeroInflation_;
326 }
327
329 return interpolated_;
330 }
331
332 inline bool YoYInflationIndex::ratio() const {
333 return ratio_;
334 }
335
336 inline ext::shared_ptr<ZeroInflationIndex> YoYInflationIndex::underlyingIndex() const {
337 return underlyingIndex_;
338 }
339
342 return yoyInflation_;
343 }
344
345}
346
347#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:45
Base class for inflation-rate indexes,.
Real pastFixing(const Date &fixingDate) const override=0
returns a past fixing at the given date
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
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_
bool needsForecast(const Date &fixingDate) const
Real pastFixing(const Date &fixingDate) const override
returns a past fixing at the given date
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 pastFixing(const Date &fixingDate) const override
returns a past fixing at the given date
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.
bool isInterpolated(const QuantLib::CPI::InterpolationType &type)
QuantLib::CPI::InterpolationType effectiveInterpolationType(const QuantLib::CPI::InterpolationType &type)
Definition: any.hpp:37
Region, i.e. geographical area, specification.
static Real laggedYoYRate(const ext::shared_ptr< YoYInflationIndex > &index, const Date &date, const Period &observationLag, InterpolationType interpolationType)
interpolated year-on-year inflation rate
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
@ Flat
flat from previous fixing