QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
blackvoltermstructure.hpp
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
25#ifndef quantlib_black_vol_term_structures_hpp
26#define quantlib_black_vol_term_structures_hpp
27
28#include <ql/termstructures/voltermstructure.hpp>
29#include <ql/patterns/visitor.hpp>
30
31namespace QuantLib {
32
34
41 public:
48
53 const DayCounter& dc = DayCounter());
56 const Calendar& cal = Calendar(),
58 const DayCounter& dc = DayCounter());
61 const Calendar&,
63 const DayCounter& dc = DayCounter());
65 ~BlackVolTermStructure() override = default;
67
68
69 Volatility blackVol(const Date& maturity,
70 Real strike,
71 bool extrapolate = false) const;
73 Volatility blackVol(Time maturity,
74 Real strike,
75 bool extrapolate = false) const;
77 Real blackVariance(const Date& maturity,
78 Real strike,
79 bool extrapolate = false) const;
81 Real blackVariance(Time maturity,
82 Real strike,
83 bool extrapolate = false) const;
85 Volatility blackForwardVol(const Date& date1,
86 const Date& date2,
87 Real strike,
88 bool extrapolate = false) const;
91 Time time2,
92 Real strike,
93 bool extrapolate = false) const;
95 Real blackForwardVariance(const Date& date1,
96 const Date& date2,
97 Real strike,
98 bool extrapolate = false) const;
101 Time time2,
102 Real strike,
103 bool extrapolate = false) const;
105
107 virtual void accept(AcyclicVisitor&);
109 protected:
119 virtual Real blackVarianceImpl(Time t, Real strike) const = 0;
121 virtual Volatility blackVolImpl(Time t, Real strike) const = 0;
123 };
124
126
133 public:
140
145 const DayCounter& dc = DayCounter());
148 const Calendar& cal = Calendar(),
150 const DayCounter& dc = DayCounter());
153 const Calendar& cal,
155 const DayCounter& dc = DayCounter());
157
159 void accept(AcyclicVisitor&) override;
161 protected:
165 Real blackVarianceImpl(Time maturity, Real strike) const override;
166 };
167
168
170
178 public:
185
190 const DayCounter& dc = DayCounter());
193 const Calendar& cal = Calendar(),
195 const DayCounter& dc = DayCounter());
198 const Calendar&,
200 const DayCounter& dc = DayCounter());
202
204 void accept(AcyclicVisitor&) override;
206 protected:
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);
222 Time t = timeFromReference(d);
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);
239 Time t = timeFromReference(d);
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
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