QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
yoyinflationoptionletvolatilitystructure.cpp
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) 2009 Chris Kenyon
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18 */
19
20/*! \file yoyoptionletvolatilitystructures.cpp
21 \brief yoy inflation volatility structures
22 */
23
27#include <utility>
28
29namespace QuantLib {
30
33 const Calendar &cal,
35 const DayCounter& dc,
36 const Period& observationLag,
37 Frequency frequency,
38 bool indexIsInterpolated,
39 VolatilityType volType,
40 Real displacement)
41 : VolatilityTermStructure(settlementDays, cal, bdc, dc),
42 baseLevel_(Null<Volatility>()), observationLag_(observationLag),
43 frequency_(frequency), indexIsInterpolated_(indexIsInterpolated),
44 volType_(volType), displacement_(displacement) {
46 "YoYOptionletVolatilitySurface: displacement (" << displacement
47 << ") must be 0 or 1");
48 }
49
50
51 Date
53
54 // Depends on interpolation, or not, of observed index
55 // and observation lag with which it was built.
56 // We want this to work even if the index does not
57 // have a yoy term structure.
58 if (indexIsInterpolated()) {
59 return referenceDate() - observationLag();
60 } else {
62 frequency()).first;
63 }
64 }
65
66
68 bool extrapolate) const {
70 "date (" << d << ") is before base date");
71 QL_REQUIRE(extrapolate || allowsExtrapolation() || d <= maxDate(),
72 "date (" << d << ") is past max curve date ("
73 << maxDate() << ")");
74 QL_REQUIRE(extrapolate || allowsExtrapolation() ||
75 (strike >= minStrike() && strike <= maxStrike()),
76 "strike (" << strike << ") is outside the curve domain ["
77 << minStrike() << "," << maxStrike()<< "]] at date = " << d);
78 }
79
80
82 bool extrapolate) const {
84 "time (" << t << ") is before base date");
85 QL_REQUIRE(extrapolate || allowsExtrapolation() || t <= maxTime(),
86 "time (" << t << ") is past max curve time ("
87 << maxTime() << ")");
88 QL_REQUIRE(extrapolate || allowsExtrapolation() ||
89 (strike >= minStrike() && strike <= maxStrike()),
90 "strike (" << strike << ") is outside the curve domain ["
91 << minStrike() << "," << maxStrike()<< "] at time = " << t);
92 }
93
94
97 Rate strike,
98 const Period &obsLag,
99 bool extrapolate) const {
100
101 Period useLag = obsLag;
102 if (obsLag==Period(-1,Days)) {
103 useLag = observationLag();
104 }
105
106 if (indexIsInterpolated()) {
107 YoYOptionletVolatilitySurface::checkRange(maturityDate-useLag, strike, extrapolate);
108 Time t = timeFromReference(maturityDate-useLag);
109 return volatilityImpl(t,strike);
110 } else {
111 std::pair<Date,Date> dd = inflationPeriod(maturityDate-useLag, frequency());
112 YoYOptionletVolatilitySurface::checkRange(dd.first, strike, extrapolate);
113 Time t = timeFromReference(dd.first);
114 return volatilityImpl(t,strike);
115 }
116 }
117
118
121 Rate strike,
122 const Period &obsLag,
123 bool extrapolate) const {
124 Date maturityDate = optionDateFromTenor(optionTenor);
125 return volatility(maturityDate, strike, obsLag, extrapolate);
126 }
127
129 return volatilityImpl(time, strike);
130 }
131
132 //! needed for total variance calculations
133 Time
135 const Period& obsLag) const {
136
137 Period useLag = obsLag;
138 if (obsLag==Period(-1,Days)) {
139 useLag = observationLag();
140 }
141
142 Date useDate;
143 if (indexIsInterpolated()) {
144 useDate = maturityDate - useLag;
145 } else {
146 useDate = inflationPeriod(maturityDate - useLag,
147 frequency()).first;
148 }
149
150 // This assumes that the inflation term structure starts
151 // as late as possible given the inflation index definition,
152 // which is the usual case.
153 return dayCounter().yearFraction(baseDate(), useDate);
154 }
155
156
159 Rate strike,
160 const Period &obsLag,
161 bool extrapolate) const {
162
163 Volatility vol = volatility(maturityDate, strike, obsLag, extrapolate);
164 Time t = timeFromBase(maturityDate, obsLag);
165 return vol*vol*t;
166 }
167
168
171 Rate strike,
172 const Period &obsLag,
173 bool extrap) const {
174 Date maturityDate = optionDateFromTenor(tenor);
175 return totalVariance(maturityDate, strike, obsLag, extrap);
176 }
177
178
179
180 //========================================================================
181 // constant yoy vol surface
182 //========================================================================
183
186 Natural settlementDays,
187 const Calendar& cal,
189 const DayCounter& dc,
190 const Period &observationLag,
191 Frequency frequency,
192 bool indexIsInterpolated,
193 Rate minStrike,
194 Rate maxStrike,
195 VolatilityType volType,
196 Real displacement)
197 : YoYOptionletVolatilitySurface(settlementDays, cal, bdc, dc,
198 observationLag, frequency, indexIsInterpolated,
199 volType, displacement),
200 volatility_(ext::make_shared<SimpleQuote>(v)), minStrike_(minStrike), maxStrike_(maxStrike) {}
201
203 Natural settlementDays,
204 const Calendar& cal,
206 const DayCounter& dc,
207 const Period& observationLag,
208 Frequency frequency,
209 bool indexIsInterpolated,
210 Rate minStrike,
211 Rate maxStrike,
212 VolatilityType volType,
213 Real displacement)
214 : YoYOptionletVolatilitySurface(settlementDays,
215 cal,
216 bdc,
217 dc,
218 observationLag,
219 frequency,
220 indexIsInterpolated,
221 volType,
222 displacement),
223 volatility_(std::move(v)), minStrike_(minStrike), maxStrike_(maxStrike) {}
224
226 return volatility_->value();
227 }
228
229} // namespace QuantLib
calendar class
Definition: calendar.hpp:61
ConstantYoYOptionletVolatility(Volatility v, Natural settlementDays, const Calendar &, BusinessDayConvention bdc, const DayCounter &dc, const Period &observationLag, Frequency frequency, bool indexIsInterpolated, Rate minStrike=-1.0, Rate maxStrike=100.0, VolatilityType volType=ShiftedLognormal, Real displacement=0.0)
calculate the reference date based on the global evaluation date
Volatility volatilityImpl(Time length, Rate strike) const override
implements the actual volatility calculation in derived classes
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
bool allowsExtrapolation() const
tells whether extrapolation is enabled
Shared handle to an observable.
Definition: handle.hpp:41
template class providing a null value for a given type.
Definition: null.hpp:76
market element returning a stored value
Definition: simplequote.hpp:33
virtual const Date & referenceDate() const
the date at which discount = 1.0 and/or variance = 0.0
virtual Time maxTime() const
the latest time for which the curve can return values
virtual Date maxDate() const =0
the latest date for which the curve can return values
Time timeFromReference(const Date &date) const
date/time conversion
virtual DayCounter dayCounter() const
the day counter used for date/time conversion
Volatility term structure.
Date optionDateFromTenor(const Period &) const
period/date conversion
Real minStrike() const override=0
the minimum strike for which the term structure can return vols
virtual Volatility volatilityImpl(Time length, Rate strike) const =0
virtual Time timeFromBase(const Date &date, const Period &obsLag=Period(-1, Days)) const
base date will be in the past because of observation lag
YoYOptionletVolatilitySurface(Natural settlementDays, const Calendar &, BusinessDayConvention bdc, const DayCounter &dc, const Period &observationLag, Frequency frequency, bool indexIsInterpolated, VolatilityType volType=ShiftedLognormal, Real displacement=0.0)
virtual void checkRange(const Date &, Rate strike, bool extrapolate) const
virtual Volatility totalVariance(const Date &exerciseDate, Rate strike, const Period &obsLag=Period(-1, Days), bool extrapolate=false) const
Returns the total integrated variance for a given exercise date and strike rate.
virtual Real displacement() const
Returns the displacement for lognormal volatilities.
Real maxStrike() const override=0
the maximum strike for which the term structure can return vols
Volatility volatility(const Date &maturityDate, Rate strike, const Period &obsLag=Period(-1, Days), bool extrapolate=false) const
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
Frequency
Frequency of events.
Definition: frequency.hpp:37
BusinessDayConvention
Business Day conventions.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
Base classes for inflation term structures.
Definition: any.hpp:35
std::pair< Date, Date > inflationPeriod(const Date &d, Frequency frequency)
utility function giving the inflation period for a given date
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182
STL namespace.
ext::shared_ptr< BlackVolTermStructure > v
simple quote class
yoy inflation volatility structures