Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
zeroinflationcurveobserverstatic.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/zeroinflationcurveobserverstatic.hpp
20 \brief Observable inflation term structure based on the interpolation of zero rate quotes.
21 \ingroup termstructures
22*/
23
24#ifndef quantext_zero_inflation_curve_observer_static_hpp
25#define quantext_zero_inflation_curve_observer_static_hpp
26
27#include <ql/math/comparison.hpp>
28#include <ql/math/interpolations/linearinterpolation.hpp>
29#include <ql/patterns/lazyobject.hpp>
30#include <ql/termstructures/inflationtermstructure.hpp>
31#include <ql/termstructures/interpolatedcurve.hpp>
32
33namespace QuantExt {
34using namespace QuantLib;
35
36//! Inflation term structure based on the interpolation of zero rates.
37/*! \ingroup termstructures */
38template <class Interpolator>
39class ZeroInflationCurveObserverStatic : public ZeroInflationTermStructure,
40 protected InterpolatedCurve<Interpolator>,
41 public LazyObject {
42public:
44 const Date& referenceDate, const Calendar& calendar, const DayCounter& dayCounter, const Period& lag,
45 Frequency frequency, bool indexIsInterpolated, const std::vector<Date>& dates,
46 const std::vector<Handle<Quote>>& rates,
47 const QuantLib::ext::shared_ptr<Seasonality>& seasonality = QuantLib::ext::shared_ptr<Seasonality>(),
48 const Interpolator& interpolator = Interpolator());
49
50 //! \name InflationTermStructure interface
51 //@{
52 Date baseDate() const;
53 Date maxDate() const;
54 //@}
55
56 //! \name Inspectors
57 //@{
58 const std::vector<Date>& dates() const;
59 const std::vector<Time>& times() const;
60 const std::vector<Real>& data() const;
61 const std::vector<Rate>& rates() const;
62 std::vector<std::pair<Date, Rate> > nodes() const;
63 const std::vector<Handle<Quote> >& quotes() const { return quotes_; };
64 //@}
65
66 //! \name Observer interface
67 //@{
68 void update();
69 //@}
70
71private:
72 //! \name LazyObject interface
73 //@{
74 void performCalculations() const;
75 //@}
76
77protected:
78 //! \name ZeroInflationTermStructure Interface
79 //@{
80 Rate zeroRateImpl(Time t) const;
81 //@}
82 mutable std::vector<Date> dates_;
83 std::vector<Handle<Quote> > quotes_;
85};
86
87// template definitions
88
89template <class Interpolator>
91 const Date& referenceDate, const Calendar& calendar, const DayCounter& dayCounter, const Period& lag,
92 Frequency frequency, bool indexIsInterpolated, const std::vector<Date>& dates,
93 const std::vector<Handle<Quote>>& rates, const QuantLib::ext::shared_ptr<Seasonality>& seasonality,
94 const Interpolator& interpolator)
95 : ZeroInflationTermStructure(referenceDate, calendar, dayCounter, rates[0]->value(), lag, frequency, seasonality),
96 InterpolatedCurve<Interpolator>(std::vector<Time>(), std::vector<Real>(), interpolator), dates_(dates),
97 quotes_(rates) {
98
99 QL_REQUIRE(dates_.size() > 1, "too few dates: " << dates_.size());
100
101 /**
102 // check that the data starts from the beginning,
103 // i.e. referenceDate - lag, at least must be in the relevant
104 // period
105 std::pair<Date,Date> lim =
106 inflationPeriod(yTS->referenceDate() - this->observationLag(), frequency);
107 QL_REQUIRE(lim.first <= dates_[0] && dates_[0] <= lim.second,
108 "first data date is not in base period, date: " << dates_[0]
109 << " not within [" << lim.first << "," << lim.second << "]");
110 **/
111
112 // by convention, if the index is not interpolated we pull all the dates
113 // back to the start of their inflationPeriods
114 // otherwise the time calculations will be inconsistent
116 for (Size i = 0; i < dates_.size(); i++) {
117 dates_[i] = inflationPeriod(dates_[i], frequency).first;
118 }
119 }
120
121 QL_REQUIRE(this->quotes_.size() == dates_.size(),
122 "quotes/dates count mismatch: " << this->quotes_.size() << " vs " << dates_.size());
123
124 // initialise data vector, values are copied from quotes in performCalculations()
125 this->data_.resize(dates_.size());
126 for (Size i = 0; i < dates_.size(); i++)
127 this->data_[0] = 0.0;
128
129 this->times_.resize(dates_.size());
130 this->times_[0] = timeFromReference(dates_[0]);
131 for (Size i = 1; i < dates_.size(); i++) {
132 QL_REQUIRE(dates_[i] > dates_[i - 1], "dates not sorted");
133
134 // but must be greater than -1
135 QL_REQUIRE(this->data_[i] > -1.0, "zero inflation data < -100 %");
136
137 // this can be negative
138 this->times_[i] = timeFromReference(dates_[i]);
139 QL_REQUIRE(!close(this->times_[i], this->times_[i - 1]), "two dates correspond to the same time "
140 "under this curve's day count convention");
141 }
142
143 this->interpolation_ =
144 this->interpolator_.interpolate(this->times_.begin(), this->times_.end(), this->data_.begin());
145 this->interpolation_.update();
146
147 // register with each of the quotes
148 for (Size i = 0; i < quotes_.size(); i++)
149 registerWith(quotes_[i]);
150}
151
152template <class T> Date ZeroInflationCurveObserverStatic<T>::baseDate() const {
153 // if indexIsInterpolated we fixed the dates in the constructor
154 calculate();
155 return dates_.front();
156}
157
158template <class T> Date ZeroInflationCurveObserverStatic<T>::maxDate() const {
159 Date d;
160 if (indexIsInterpolated_) {
161 d = dates_.back();
162 } else {
163 d = inflationPeriod(dates_.back(), frequency()).second;
164 }
165 return d;
166}
167
168template <class T> inline Rate ZeroInflationCurveObserverStatic<T>::zeroRateImpl(Time t) const {
169 calculate();
170 return this->interpolation_(t, true);
171}
172
173template <class T> inline const std::vector<Time>& ZeroInflationCurveObserverStatic<T>::times() const {
174 return this->times_;
175}
176
177template <class T> inline const std::vector<Date>& ZeroInflationCurveObserverStatic<T>::dates() const { return dates_; }
178
179template <class T> inline const std::vector<Rate>& ZeroInflationCurveObserverStatic<T>::rates() const {
180 calculate();
181 return this->data_;
182}
183
184template <class T> inline const std::vector<Real>& ZeroInflationCurveObserverStatic<T>::data() const {
185 calculate();
186 return this->data_;
187}
188
189template <class T> inline std::vector<std::pair<Date, Rate> > ZeroInflationCurveObserverStatic<T>::nodes() const {
190 calculate();
191 std::vector<std::pair<Date, Rate> > results(dates_.size());
192 for (Size i = 0; i < dates_.size(); ++i)
193 results[i] = std::make_pair(dates_[i], this->data_[i]);
194 return results;
195}
196
197template <class T> inline void ZeroInflationCurveObserverStatic<T>::update() {
198 LazyObject::update();
199 ZeroInflationTermStructure::update();
200}
201
202template <class T> inline void ZeroInflationCurveObserverStatic<T>::performCalculations() const {
203 for (Size i = 0; i < dates_.size(); ++i)
204 this->data_[i] = quotes_[i]->value();
205 this->interpolation_ =
206 this->interpolator_.interpolate(this->times_.begin(), this->times_.end(), this->data_.begin());
207 this->interpolation_.update();
208}
209} // namespace QuantExt
210
211#endif
Inflation term structure based on the interpolation of zero rates.
ZeroInflationCurveObserverStatic(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