QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
cpivolatilitystructure.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009, 2011 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#include <ql/termstructures/volatility/inflation/cpivolatilitystructure.hpp>
21#include <ql/termstructures/inflationtermstructure.hpp>
22
23namespace QuantLib {
24
26 const Calendar& cal,
28 const DayCounter& dc,
29 const Period& observationLag,
30 Frequency frequency,
31 bool indexIsInterpolated)
32 : VolatilityTermStructure(settlementDays, cal, bdc, dc),
33 baseLevel_(Null<Volatility>()), observationLag_(observationLag),
34 frequency_(frequency), indexIsInterpolated_(indexIsInterpolated)
35 {}
36
37
39 // Depends on interpolation, or not, of observed index
40 // and observation lag with which it was built.
41 // We want this to work even if the index does not
42 // have a term structure.
43 if (indexIsInterpolated()) {
44 return referenceDate() - observationLag();
45 } else {
47 frequency()).first;
48 }
49 }
50
51
53 bool extrapolate) const {
54 QL_REQUIRE(d >= baseDate(),
55 "date (" << d << ") is before base date");
56 QL_REQUIRE(extrapolate || allowsExtrapolation() || d <= maxDate(),
57 "date (" << d << ") is past max curve date ("
58 << maxDate() << ")");
59 QL_REQUIRE(extrapolate || allowsExtrapolation() ||
60 (strike >= minStrike() && strike <= maxStrike()),
61 "strike (" << strike << ") is outside the curve domain ["
62 << minStrike() << "," << maxStrike()<< "]] at date = " << d);
63 }
64
65
67 bool extrapolate) const {
68 QL_REQUIRE(t >= timeFromReference(baseDate()),
69 "time (" << t << ") is before base date");
70 QL_REQUIRE(extrapolate || allowsExtrapolation() || t <= maxTime(),
71 "time (" << t << ") is past max curve time ("
72 << maxTime() << ")");
73 QL_REQUIRE(extrapolate || allowsExtrapolation() ||
74 (strike >= minStrike() && strike <= maxStrike()),
75 "strike (" << strike << ") is outside the curve domain ["
76 << minStrike() << "," << maxStrike()<< "] at time = " << t);
77 }
78
79
81 Rate strike,
82 const Period& obsLag,
83 bool extrapolate) const {
84
85 Period useLag = obsLag;
86 if (obsLag==Period(-1,Days)) {
87 useLag = observationLag();
88 }
89
90 if (indexIsInterpolated()) {
91 checkRange(maturityDate-useLag, strike, extrapolate);
92 Time t = timeFromReference(maturityDate-useLag);
93 return volatilityImpl(t,strike);
94 } else {
95 std::pair<Date,Date> dd =
96 inflationPeriod(maturityDate-useLag, frequency());
97 checkRange(dd.first, strike, extrapolate);
98 Time t = timeFromReference(dd.first);
99 return volatilityImpl(t,strike);
100 }
101 }
102
103
105 Rate strike,
106 const Period& obsLag,
107 bool extrapolate) const {
108 Date maturityDate = optionDateFromTenor(optionTenor);
109 return volatility(maturityDate, strike, obsLag, extrapolate);
110 }
111
113 return volatilityImpl(time, strike);
114 }
115
118 const Period& obsLag) const {
119 Period useLag = obsLag;
120 if (obsLag==Period(-1,Days)) {
121 useLag = observationLag();
122 }
123
124 Date useDate;
125 if (indexIsInterpolated()) {
126 useDate = maturityDate - useLag;
127 } else {
128 useDate = inflationPeriod(maturityDate - useLag,
129 frequency()).first;
130 }
131
132 // This assumes that the inflation term structure starts
133 // as late as possible given the inflation index definition,
134 // which is the usual case.
135 return dayCounter().yearFraction(baseDate(), useDate);
136 }
137
138
140 Rate strike,
141 const Period& obsLag,
142 bool extrapolate) const {
143 Volatility vol = volatility(maturityDate, strike, obsLag, extrapolate);
144 Time t = timeFromBase(maturityDate, obsLag);
145 return vol*vol*t;
146 }
147
148
150 Rate strike,
151 const Period& obsLag,
152 bool extrap) const {
153 Date maturityDate = optionDateFromTenor(tenor);
154 return totalVariance(maturityDate, strike, obsLag, extrap);
155 }
156
157}
158
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
CPIVolatilitySurface(Natural settlementDays, const Calendar &, BusinessDayConvention bdc, const DayCounter &dc, const Period &observationLag, Frequency frequency, bool indexIsInterpolated)
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
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
Returns the volatility for a given maturity date and strike rate.
calendar class
Definition: calendar.hpp:61
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
template class providing a null value for a given type.
Definition: null.hpp:76
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
Frequency
Frequency of events.
Definition: frequency.hpp:37
BusinessDayConvention
Business Day conventions.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
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
Definition: any.hpp:35
std::pair< Date, Date > inflationPeriod(const Date &d, Frequency frequency)
utility function giving the inflation period for a given date