Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
yoyinflationcurveobserverstatic.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file qle/termstructures/yoyinflationcurveobserverstatic.hpp
20 \brief Observable inflation term structure with fixed reference date based on the
21 interpolation of yoy rate quotes.
22 \ingroup termstructures
23*/
24
25#ifndef quantext_yoy_inflation_curve_observer_static_hpp
26#define quantext_yoy_inflation_curve_observer_static_hpp
27
28#include <ql/math/comparison.hpp>
29#include <ql/math/interpolations/linearinterpolation.hpp>
30#include <ql/patterns/lazyobject.hpp>
31#include <ql/termstructures/inflationtermstructure.hpp>
32#include <ql/termstructures/interpolatedcurve.hpp>
33
34namespace QuantExt {
35using namespace QuantLib;
36
37//! Inflation term structure based on the interpolation of zero rates.
38/*! \ingroup inflationtermstructures */
39template <class Interpolator>
41 protected InterpolatedCurve<Interpolator>,
42 public LazyObject {
43public:
45 const Date& referenceDate, const Calendar& calendar, const DayCounter& dayCounter, const Period& lag,
46 Frequency frequency, bool indexIsInterpolated, const std::vector<Date>& dates,
47 const std::vector<Handle<Quote>>& rates,
48 const QuantLib::ext::shared_ptr<Seasonality>& seasonality = QuantLib::ext::shared_ptr<Seasonality>(),
49 const Interpolator& interpolator = Interpolator());
50
51 //! \name InflationTermStructure interface
52 //@{
53 Date baseDate() const;
54 Date maxDate() const;
55 //@}
56
57 //! \name Inspectors
58 //@{
59 const std::vector<Date>& dates() const;
60 const std::vector<Time>& times() const;
61 const std::vector<Real>& data() const;
62 const std::vector<Rate>& rates() const;
63 std::vector<std::pair<Date, Rate> > nodes() const;
64 const std::vector<Handle<Quote> >& quotes() const { return quotes_; };
65 //@}
66
67 //! \name Observer interface
68 //@{
69 void update();
70 //@}
71
72private:
73 //! \name LazyObject interface
74 //@{
75 void performCalculations() const;
76 //@}
77
78protected:
79 //! \name YoYInflationTermStructure Interface
80 //@{
81 Rate yoyRateImpl(Time t) const;
82 //@}
83 mutable std::vector<Date> dates_;
84 std::vector<Handle<Quote> > quotes_;
86};
87
88// template definitions
89
90template <class Interpolator>
92 const Date& referenceDate, const Calendar& calendar, const DayCounter& dayCounter, const Period& lag,
93 Frequency frequency, bool indexIsInterpolated, const std::vector<Date>& dates,
94 const std::vector<Handle<Quote>>& rates, const QuantLib::ext::shared_ptr<Seasonality>& seasonality,
95 const Interpolator& interpolator)
96 : YoYInflationTermStructure(referenceDate, calendar, dayCounter, rates[0]->value(), lag, frequency,
97 indexIsInterpolated, seasonality),
98 InterpolatedCurve<Interpolator>(std::vector<Time>(), std::vector<Real>(), interpolator), dates_(dates),
99 quotes_(rates), indexIsInterpolated_(indexIsInterpolated) {
100
101 QL_REQUIRE(dates_.size() > 1, "too few dates: " << dates_.size());
102
103 /**
104 // check that the data starts from the beginning,
105 // i.e. referenceDate - lag, at least must be in the relevant
106 // period
107 std::pair<Date,Date> lim =
108 inflationPeriod(yTS->referenceDate() - this->observationLag(), frequency);
109 QL_REQUIRE(lim.first <= dates_[0] && dates_[0] <= lim.second,
110 "first data date is not in base period, date: " << dates_[0]
111 << " not within [" << lim.first << "," << lim.second << "]");
112 **/
113
114 // by convention, if the index is not interpolated we pull all the dates
115 // back to the start of their inflationPeriods
116 // otherwise the time calculations will be inconsistent
118 for (Size i = 0; i < dates_.size(); i++) {
119 dates_[i] = inflationPeriod(dates_[i], frequency).first;
120 }
121 }
122
123 QL_REQUIRE(this->quotes_.size() == dates_.size(),
124 "quotes/dates count mismatch: " << this->quotes_.size() << " vs " << dates_.size());
125
126 // initialise data vector, values are copied from quotes in performCalculations()
127 this->data_.resize(dates_.size());
128 for (Size i = 0; i < dates_.size(); i++)
129 this->data_[0] = 0.0;
130
131 this->times_.resize(dates_.size());
132 this->times_[0] = timeFromReference(dates_[0]);
133 for (Size i = 1; i < dates_.size(); i++) {
134 QL_REQUIRE(dates_[i] > dates_[i - 1], "dates not sorted");
135
136 // but must be greater than -1
137 QL_REQUIRE(this->data_[i] > -1.0, "zero inflation data < -100 %");
138
139 // this can be negative
140 this->times_[i] = timeFromReference(dates_[i]);
141 QL_REQUIRE(!close(this->times_[i], this->times_[i - 1]), "two dates correspond to the same time "
142 "under this curve's day count convention");
143 }
144
145 this->interpolation_ =
146 this->interpolator_.interpolate(this->times_.begin(), this->times_.end(), this->data_.begin());
147 this->interpolation_.update();
148
149 // register with each of the quotes
150 for (Size i = 0; i < quotes_.size(); i++)
151 registerWith(quotes_[i]);
152}
153
154template <class T> Date YoYInflationCurveObserverStatic<T>::baseDate() const {
155 // if indexIsInterpolated we fixed the dates in the constructor
156 calculate();
157 return dates_.front();
158}
159
160template <class T> Date YoYInflationCurveObserverStatic<T>::maxDate() const {
161 Date d;
162 if (indexIsInterpolated()) {
163 d = dates_.back();
164 } else {
165 d = inflationPeriod(dates_.back(), frequency()).second;
166 }
167 return d;
168}
169
170template <class T> inline Rate YoYInflationCurveObserverStatic<T>::yoyRateImpl(Time t) const {
171 calculate();
172 return this->interpolation_(t, true);
173}
174
175template <class T> inline const std::vector<Time>& YoYInflationCurveObserverStatic<T>::times() const {
176 return this->times_;
177}
178
179template <class T> inline const std::vector<Date>& YoYInflationCurveObserverStatic<T>::dates() const { return dates_; }
180
181template <class T> inline const std::vector<Rate>& YoYInflationCurveObserverStatic<T>::rates() const {
182 calculate();
183 return this->data_;
184}
185
186template <class T> inline const std::vector<Real>& YoYInflationCurveObserverStatic<T>::data() const {
187 calculate();
188 return this->data_;
189}
190
191template <class T> inline std::vector<std::pair<Date, Rate> > YoYInflationCurveObserverStatic<T>::nodes() const {
192 calculate();
193 std::vector<std::pair<Date, Rate> > results(dates_.size());
194 for (Size i = 0; i < dates_.size(); ++i)
195 results[i] = std::make_pair(dates_[i], this->data_[i]);
196 return results;
197}
198
199template <class T> inline void YoYInflationCurveObserverStatic<T>::update() {
200 LazyObject::update();
201 YoYInflationTermStructure::update();
202}
203
204template <class T> inline void YoYInflationCurveObserverStatic<T>::performCalculations() const {
205 for (Size i = 0; i < dates_.size(); ++i)
206 this->data_[i] = quotes_[i]->value();
207 this->interpolation_ =
208 this->interpolator_.interpolate(this->times_.begin(), this->times_.end(), this->data_.begin());
209 this->interpolation_.update();
210}
211} // namespace QuantExt
212
213#endif
Inflation term structure based on the interpolation of zero rates.
YoYInflationCurveObserverStatic(const Date &referenceDate, const Calendar &calendar, const DayCounter &dayCounter, const Period &lag, Frequency frequency, bool indexIsInterpolated, const std::vector< Date > &dates, const std::vector< Handle< Quote > > &rates, const QuantLib::ext::shared_ptr< Seasonality > &seasonality=QuantLib::ext::shared_ptr< Seasonality >(), const Interpolator &interpolator=Interpolator())
std::vector< std::pair< Date, Rate > > nodes() const
const std::vector< Handle< Quote > > & quotes() const