QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
blackvoltermstructure.hpp
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) 2002, 2003 Ferdinando Ametrano
5 Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file blackvoltermstructure.hpp
22 \brief Black volatility term structure base classes
23*/
24
25#ifndef quantlib_black_vol_term_structures_hpp
26#define quantlib_black_vol_term_structures_hpp
27
30
31namespace QuantLib {
32
33 //! Black-volatility term structure
34 /*! This abstract class defines the interface of concrete
35 Black-volatility term structures which will be derived from
36 this one.
37
38 Volatilities are assumed to be expressed on an annual basis.
39 */
41 public:
42 /*! \name Constructors
43 See the TermStructure documentation for issues regarding
44 constructors.
45 */
46 //@{
47 //! default constructor
48 /*! \warning term structures initialized by means of this
49 constructor must manage their own reference date
50 by overriding the referenceDate() method.
51 */
53 const DayCounter& dc = DayCounter());
54 //! initialize with a fixed reference date
56 const Calendar& cal = Calendar(),
58 const DayCounter& dc = DayCounter());
59 //! calculate the reference date based on the global evaluation date
61 const Calendar&,
63 const DayCounter& dc = DayCounter());
64 //@}
65 ~BlackVolTermStructure() override = default;
66 //! \name Black Volatility
67 //@{
68 //! spot volatility
69 Volatility blackVol(const Date& maturity,
70 Real strike,
71 bool extrapolate = false) const;
72 //! spot volatility
73 Volatility blackVol(Time maturity,
74 Real strike,
75 bool extrapolate = false) const;
76 //! spot variance
77 Real blackVariance(const Date& maturity,
78 Real strike,
79 bool extrapolate = false) const;
80 //! spot variance
81 Real blackVariance(Time maturity,
82 Real strike,
83 bool extrapolate = false) const;
84 //! forward (at-the-money) volatility
85 Volatility blackForwardVol(const Date& date1,
86 const Date& date2,
87 Real strike,
88 bool extrapolate = false) const;
89 //! forward (at-the-money) volatility
91 Time time2,
92 Real strike,
93 bool extrapolate = false) const;
94 //! forward (at-the-money) variance
95 Real blackForwardVariance(const Date& date1,
96 const Date& date2,
97 Real strike,
98 bool extrapolate = false) const;
99 //! forward (at-the-money) variance
101 Time time2,
102 Real strike,
103 bool extrapolate = false) const;
104 //@}
105 //! \name Visitability
106 //@{
107 virtual void accept(AcyclicVisitor&);
108 //@}
109 protected:
110 /*! \name Calculations
111
112 These methods must be implemented in derived classes to perform
113 the actual volatility calculations. When they are called,
114 range check has already been performed; therefore, they must
115 assume that extrapolation is required.
116 */
117 //@{
118 //! Black variance calculation
119 virtual Real blackVarianceImpl(Time t, Real strike) const = 0;
120 //! Black volatility calculation
121 virtual Volatility blackVolImpl(Time t, Real strike) const = 0;
122 //@}
123 };
124
125 //! Black-volatility term structure
126 /*! This abstract class acts as an adapter to BlackVolTermStructure
127 allowing the programmer to implement only the
128 <tt>blackVolImpl(Time, Real, bool)</tt> method in derived classes.
129
130 Volatility are assumed to be expressed on an annual basis.
131 */
133 public:
134 /*! \name Constructors
135 See the TermStructure documentation for issues regarding
136 constructors.
137 */
138 //@{
139 //! default constructor
140 /*! \warning term structures initialized by means of this
141 constructor must manage their own reference date
142 by overriding the referenceDate() method.
143 */
145 const DayCounter& dc = DayCounter());
146 //! initialize with a fixed reference date
148 const Calendar& cal = Calendar(),
150 const DayCounter& dc = DayCounter());
151 //! calculate the reference date based on the global evaluation date
153 const Calendar& cal,
155 const DayCounter& dc = DayCounter());
156 //@}
157 //! \name Visitability
158 //@{
159 void accept(AcyclicVisitor&) override;
160 //@}
161 protected:
162 /*! Returns the variance for the given strike and date calculating it
163 from the volatility.
164 */
165 Real blackVarianceImpl(Time maturity, Real strike) const override;
166 };
167
168
169 //! Black variance term structure
170 /*! This abstract class acts as an adapter to VolTermStructure allowing
171 the programmer to implement only the
172 <tt>blackVarianceImpl(Time, Real, bool)</tt> method in derived
173 classes.
174
175 Volatility are assumed to be expressed on an annual basis.
176 */
178 public:
179 /*! \name Constructors
180 See the TermStructure documentation for issues regarding
181 constructors.
182 */
183 //@{
184 //! default constructor
185 /*! \warning term structures initialized by means of this
186 constructor must manage their own reference date
187 by overriding the referenceDate() method.
188 */
190 const DayCounter& dc = DayCounter());
191 //! initialize with a fixed reference date
193 const Calendar& cal = Calendar(),
195 const DayCounter& dc = DayCounter());
196 //! calculate the reference date based on the global evaluation date
198 const Calendar&,
200 const DayCounter& dc = DayCounter());
201 //@}
202 //! \name Visitability
203 //@{
204 void accept(AcyclicVisitor&) override;
205 //@}
206 protected:
207 /*! Returns the volatility for the given strike and date calculating it
208 from the variance.
209 */
210 Volatility blackVolImpl(Time t, Real strike) const override;
211 };
212
213
214
215 // inline definitions
216
218 Real strike,
219 bool extrapolate) const {
220 checkRange(d, extrapolate);
221 checkStrike(strike, extrapolate);
223 return blackVolImpl(t, strike);
224 }
225
227 Real strike,
228 bool extrapolate) const {
229 checkRange(t, extrapolate);
230 checkStrike(strike, extrapolate);
231 return blackVolImpl(t, strike);
232 }
233
235 Real strike,
236 bool extrapolate) const {
237 checkRange(d, extrapolate);
238 checkStrike(strike, extrapolate);
240 return blackVarianceImpl(t, strike);
241 }
242
244 Real strike,
245 bool extrapolate) const {
246 checkRange(t, extrapolate);
247 checkStrike(strike, extrapolate);
248 return blackVarianceImpl(t, strike);
249 }
250
252 auto* v1 = dynamic_cast<Visitor<BlackVolTermStructure>*>(&v);
253 if (v1 != nullptr)
254 v1->visit(*this);
255 else
256 QL_FAIL("not a Black-volatility term structure visitor");
257 }
258
259 inline
261 Real strike) const {
262 Volatility vol = blackVolImpl(t, strike);
263 return vol*vol*t;
264 }
265
267 auto* v1 = dynamic_cast<Visitor<BlackVolatilityTermStructure>*>(&v);
268 if (v1 != nullptr)
269 v1->visit(*this);
270 else
272 }
273
274 inline
275 Volatility BlackVarianceTermStructure ::blackVolImpl(Time t,
276 Real strike) const {
277 Time nonZeroMaturity = (t==0.0 ? 0.00001 : t);
278 Real var = blackVarianceImpl(nonZeroMaturity, strike);
279 return std::sqrt(var/nonZeroMaturity);
280 }
281
283 auto* v1 = dynamic_cast<Visitor<BlackVarianceTermStructure>*>(&v);
284 if (v1 != nullptr)
285 v1->visit(*this);
286 else
288 }
289
290}
291
292#endif
degenerate base class for the Acyclic Visitor pattern
Definition: visitor.hpp:33
void accept(AcyclicVisitor &) override
Volatility blackVolImpl(Time t, Real strike) const override
Black-volatility term structure.
~BlackVolTermStructure() override=default
virtual void accept(AcyclicVisitor &)
virtual Volatility blackVolImpl(Time t, Real strike) const =0
Black volatility calculation.
Volatility blackVol(const Date &maturity, Real strike, bool extrapolate=false) const
spot volatility
virtual Real blackVarianceImpl(Time t, Real strike) const =0
Black variance calculation.
Real blackForwardVariance(const Date &date1, const Date &date2, Real strike, bool extrapolate=false) const
forward (at-the-money) variance
Volatility blackForwardVol(const Date &date1, const Date &date2, Real strike, bool extrapolate=false) const
forward (at-the-money) volatility
Real blackVariance(const Date &maturity, Real strike, bool extrapolate=false) const
spot variance
void accept(AcyclicVisitor &) override
Real blackVarianceImpl(Time maturity, Real strike) const override
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
virtual Natural settlementDays() const
the settlementDays used for reference date calculation
virtual const Date & referenceDate() const
the date at which discount = 1.0 and/or variance = 0.0
Time timeFromReference(const Date &date) const
date/time conversion
void checkRange(const Date &d, bool extrapolate) const
date-range check
Visitor for a specific class
Definition: visitor.hpp:40
virtual void visit(T &)=0
Volatility term structure.
void checkStrike(Rate strike, bool extrapolate) const
strike-range check
const DefaultType & t
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Date d
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
Definition: any.hpp:35
ext::shared_ptr< BlackVolTermStructure > v
degenerate base class for the Acyclic Visitor pattern
Volatility term structure.