Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
interpolatedcpivolatilitysurface.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 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/interpolatedcpivolatilitysurface.hpp
20 \brief zero inflation volatility structure interpolated on a expiry/strike matrix of quotes
21 */
22
23#ifndef quantext_interpolated_cpi_volatility_structure_hpp
24#define quantext_interpolated_cpi_volatility_structure_hpp
25
26#include <ql/indexes/inflationindex.hpp>
27#include <ql/math/interpolations/interpolation2d.hpp>
28#include <ql/math/matrix.hpp>
29#include <ql/quote.hpp>
30#include <ql/termstructures/volatility/inflation/constantcpivolatility.hpp>
33
34namespace QuantExt {
35using namespace QuantLib;
36
37//! Interpolated zero inflation volatility structure
38/*!
39 The surface provides interpolated CPI Black volatilities.
40 Volatility data is passed in as a vector of vector of quote Handles.
41 When performCalculations is called, current quote values are copied to a matrix and the interpolator is updated.
42 */
43template <class Interpolator2D>
45public:
46 InterpolatedCPIVolatilitySurface(const std::vector<Period>& optionTenors, const std::vector<Real>& strikes,
47 const std::vector<std::vector<Handle<Quote>>> quotes,
48 const QuantLib::ext::shared_ptr<QuantLib::ZeroInflationIndex>& index,
49 const bool quotedInstrumentsAreInterpolated,
50 const Natural settlementDays, const Calendar& cal, BusinessDayConvention bdc,
51 const DayCounter& dc, const Period& observationLag,
52 const Date& capFloorStartDate = Date(),
53 const Interpolator2D& interpolator2d = Interpolator2D(),
54 const QuantLib::VolatilityType volType = QuantLib::ShiftedLognormal,
55 const double displacement = 0.0);
56
57
58 QL_DEPRECATED
59 InterpolatedCPIVolatilitySurface(const std::vector<Period>& optionTenors, const std::vector<Real>& strikes,
60 const std::vector<std::vector<Handle<Quote>>> quotes,
61 const QuantLib::ext::shared_ptr<QuantLib::ZeroInflationIndex>& index,
62 const Natural settlementDays, const Calendar& cal, BusinessDayConvention bdc,
63 const DayCounter& dc, const Period& observationLag,
64 const Date& capFloorStartDate = Date(),
65 const Interpolator2D& interpolator2d = Interpolator2D(),
66 const QuantLib::VolatilityType volType = QuantLib::ShiftedLognormal,
67 const double displacement = 0.0);
68
69 //! \name LazyObject interface
70 //@{
71 void performCalculations() const override;
72 void update() override {
73 LazyObject::update();
74 CPIVolatilitySurface::update();
75 }
76 //@}
77
78 //! \name Limits
79 //@{
80 //! the minimum strike for which the term structure can return vols
81 QuantLib::Real minStrike() const override;
82 //! the maximum strike for which the term structure can return vols
83 QuantLib::Real maxStrike() const override;
84 //! maximum date for which the term structure can return vols
85 QuantLib::Date maxDate() const override;
86 //@}
87
88 //! \name Inspectors
89 //@{
90 const std::vector<QuantLib::Real>& strikes() { return strikes_; }
91 const std::vector<QuantLib::Period>& optionTenors() { return optionTenors_; }
92 const std::vector<std::vector<Handle<Quote> > >& quotes() { return quotes_; }
93 const QuantLib::Matrix& volData() { return volData_; }
94 //@}
95
96 QuantLib::Volatility volatilityImpl(QuantLib::Time length, QuantLib::Rate strike) const override;
97
98 QuantLib::Real atmStrike(const QuantLib::Date& maturity,
99 const QuantLib::Period& obsLag = QuantLib::Period(-1, QuantLib::Days)) const override;
100
101private:
102
103 mutable std::vector<QuantLib::Period> optionTenors_;
104 mutable std::vector<QuantLib::Time> optionTimes_;
105 mutable std::vector<QuantLib::Rate> strikes_;
106 mutable std::vector<std::vector<Handle<Quote> > > quotes_;
107 QuantLib::ext::shared_ptr<QuantLib::ZeroInflationIndex> index_;
108 mutable Matrix volData_;
109 mutable QuantLib::Interpolation2D vols_;
110 Interpolator2D interpolator2d_;
111
112};
113
114template <class Interpolator2D>
116 const std::vector<Period>& optionTenors, const std::vector<Real>& strikes,
117 const std::vector<std::vector<Handle<Quote>>> quotes, const QuantLib::ext::shared_ptr<QuantLib::ZeroInflationIndex>& index,
118 const bool quotedInstrumentsOberserveInterpolated, const Natural settlementDays, const Calendar& cal, BusinessDayConvention bdc, const DayCounter& dc,
119 const Period& observationLag, const Date& capFloorStartDate, const Interpolator2D& interpolator2d,
120 const QuantLib::VolatilityType volType, const double displacement)
121 : CPIVolatilitySurface(settlementDays, cal, bdc, dc, observationLag, index->frequency(),
122 quotedInstrumentsOberserveInterpolated,
123 capFloorStartDate, volType, displacement),
124 optionTenors_(optionTenors), strikes_(strikes), quotes_(quotes), index_(index), interpolator2d_(interpolator2d) {
125 for (Size i = 0; i < optionTenors_.size(); ++i) {
126 QL_REQUIRE(quotes_[i].size() == strikes_.size(), "quotes row " << i << " length does not match strikes size");
127 for (Size j = 0; j < strikes_.size(); ++j)
128 registerWith(quotes_[i][j]);
129 }
130}
131
132
133QL_DEPRECATED_DISABLE_WARNING
134template <class Interpolator2D>
136 const std::vector<Period>& optionTenors, const std::vector<Real>& strikes,
137 const std::vector<std::vector<Handle<Quote>>> quotes, const QuantLib::ext::shared_ptr<QuantLib::ZeroInflationIndex>& index,
138 const Natural settlementDays, const Calendar& cal, BusinessDayConvention bdc, const DayCounter& dc,
139 const Period& observationLag, const Date& capFloorStartDate, const Interpolator2D& interpolator2d,
140 const QuantLib::VolatilityType volType, const double displacement)
141 : CPIVolatilitySurface(settlementDays, cal, bdc, dc, observationLag, index->frequency(), index->interpolated(),
142 capFloorStartDate, volType, displacement),
143 optionTenors_(optionTenors), strikes_(strikes), quotes_(quotes), index_(index), interpolator2d_(interpolator2d) {
144 for (Size i = 0; i < optionTenors_.size(); ++i) {
145 QL_REQUIRE(quotes_[i].size() == strikes_.size(), "quotes row " << i << " length does not match strikes size");
146 for (Size j = 0; j < strikes_.size(); ++j)
147 registerWith(quotes_[i][j]);
148 }
149}
150QL_DEPRECATED_ENABLE_WARNING
151
153 volData_ = QuantLib::Matrix(strikes_.size(), optionTenors_.size(), QuantLib::Null<QuantLib::Real>());
154 QL_REQUIRE(quotes_.size() == optionTenors_.size(), "quotes rows does not match option tenors size");
155 optionTimes_.clear();
156 for (Size i = 0; i < optionTenors_.size(); ++i) {
157 QuantLib::Date d = optionDateFromTenor(optionTenors_[i]);
158 // Save the vols at their fixing times and not maturity
159 optionTimes_.push_back(fixingTime(d));
160 for (QuantLib::Size j = 0; j < strikes_.size(); j++)
161 volData_[j][i] = quotes_[i][j]->value();
162 }
163
164 vols_ = interpolator2d_.interpolate(optionTimes_.begin(), optionTimes_.end(), strikes_.begin(), strikes_.end(),
165 volData_);
166 // FIXME ?
167 vols_.enableExtrapolation();
168 vols_.update();
169}
170
171template <class Interpolator2D> QuantLib::Real InterpolatedCPIVolatilitySurface<Interpolator2D>::minStrike() const {
172 return strikes_.front() - QL_EPSILON;
173}
174
175template <class Interpolator2D> QuantLib::Real InterpolatedCPIVolatilitySurface<Interpolator2D>::maxStrike() const {
176 return strikes_.back() + QL_EPSILON;
177}
178
179template <class Interpolator2D> QuantLib::Date InterpolatedCPIVolatilitySurface<Interpolator2D>::maxDate() const {
180 QuantLib::Date today = QuantLib::Settings::instance().evaluationDate();
181 return today + optionTenors_.back();
182}
183
184template <class Interpolator2D>
186 QuantLib::Rate strike) const {
187 calculate();
188 return vols_(length, strike);
189}
190
191template <class Interpolator2D>
192QuantLib::Real InterpolatedCPIVolatilitySurface<Interpolator2D>::atmStrike(const QuantLib::Date& maturity,
193 const QuantLib::Period& obsLag) const {
194 QuantLib::Period lag = obsLag == -1 * QuantLib::Days ? observationLag() : obsLag;
195 QuantLib::Date fixingDate = ZeroInflation::fixingDate(maturity, lag, frequency(), indexIsInterpolated());
196 double forwardCPI = ZeroInflation::cpiFixing(index_, maturity, lag, indexIsInterpolated());
197 double baseCPI = ZeroInflation::cpiFixing(
198 index_, capFloorStartDate(), observationLag(), indexIsInterpolated());
199 double atm = forwardCPI / baseCPI;
200 double ttm =
201 QuantLib::inflationYearFraction(frequency(), indexIsInterpolated(), dayCounter(), baseDate(), fixingDate);
202 return std::pow(atm, 1.0 / ttm) - 1.0;
203}
204
205} // namespace QuantExt
206
207#endif
double displacement() const
Returns the displacement for lognormal volatilities.
QuantLib::Date capFloorStartDate() const
Interpolated zero inflation volatility structure.
InterpolatedCPIVolatilitySurface(const std::vector< Period > &optionTenors, const std::vector< Real > &strikes, const std::vector< std::vector< Handle< Quote > > > quotes, const QuantLib::ext::shared_ptr< QuantLib::ZeroInflationIndex > &index, const bool quotedInstrumentsAreInterpolated, const Natural settlementDays, const Calendar &cal, BusinessDayConvention bdc, const DayCounter &dc, const Period &observationLag, const Date &capFloorStartDate=Date(), const Interpolator2D &interpolator2d=Interpolator2D(), const QuantLib::VolatilityType volType=QuantLib::ShiftedLognormal, const double displacement=0.0)
QuantLib::Real minStrike() const override
the minimum strike for which the term structure can return vols
const std::vector< std::vector< Handle< Quote > > > & quotes()
QuantLib::Real maxStrike() const override
the maximum strike for which the term structure can return vols
QuantLib::Date maxDate() const override
maximum date for which the term structure can return vols
QuantLib::ext::shared_ptr< QuantLib::ZeroInflationIndex > index_
std::vector< std::vector< Handle< Quote > > > quotes_
const std::vector< QuantLib::Period > & optionTenors()
QuantLib::Real atmStrike(const QuantLib::Date &maturity, const QuantLib::Period &obsLag=QuantLib::Period(-1, QuantLib::Days)) const override
QuantLib::Volatility volatilityImpl(QuantLib::Time length, QuantLib::Rate strike) const override
interpolated correlation term structure
some inflation related utilities.
QuantLib::Date fixingDate(const QuantLib::Date &d, const QuantLib::Period obsLag, const QuantLib::Frequency freq, bool interpolated)
Definition: inflation.cpp:183
QuantLib::Rate cpiFixing(const QuantLib::ext::shared_ptr< QuantLib::ZeroInflationIndex > &index, const QuantLib::Date &maturity, const QuantLib::Period &obsLag, bool interpolated)
Computes a CPI fixing giving an zeroIndex, with interpolation if needed.
Definition: inflation.cpp:166
vector< Real > strikes