QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
blackvoltermstructure.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) 2002, 2003 Ferdinando Ametrano
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
21
22namespace QuantLib {
23
25 const DayCounter& dc)
26 : VolatilityTermStructure(bdc, dc) {}
27
29 const Calendar& cal,
31 const DayCounter& dc)
32 : VolatilityTermStructure(refDate, cal, bdc, dc) {}
33
35 const Calendar& cal,
37 const DayCounter& dc)
38 : VolatilityTermStructure(settlDays, cal, bdc, dc) {}
39
41 const Date& date2,
42 Real strike,
43 bool extrapolate) const {
44 // (redundant) date-based checks
45 QL_REQUIRE(date1 <= date2,
46 date1 << " later than " << date2);
47 checkRange(date2, extrapolate);
48
49 // using the time implementation
50 Time time1 = timeFromReference(date1);
51 Time time2 = timeFromReference(date2);
52 return blackForwardVol(time1, time2, strike, extrapolate);
53 }
54
56 Time time2,
57 Real strike,
58 bool extrapolate) const {
59 QL_REQUIRE(time1 <= time2,
60 time1 << " later than " << time2);
61 checkRange(time2, extrapolate);
62 checkStrike(strike, extrapolate);
63 if (time2==time1) {
64 if (time1==0.0) {
65 Time epsilon = 1.0e-5;
66 Real var = blackVarianceImpl(epsilon, strike);
67 return std::sqrt(var/epsilon);
68 } else {
69 Time epsilon = std::min<Time>(1.0e-5, time1);
70 Real var1 = blackVarianceImpl(time1-epsilon, strike);
71 Real var2 = blackVarianceImpl(time1+epsilon, strike);
72 QL_ENSURE(var2>=var1,
73 "variances must be non-decreasing");
74 return std::sqrt((var2-var1)/(2*epsilon));
75 }
76 } else {
77 Real var1 = blackVarianceImpl(time1, strike);
78 Real var2 = blackVarianceImpl(time2, strike);
79 QL_ENSURE(var2 >= var1,
80 "variances must be non-decreasing");
81 return std::sqrt((var2-var1)/(time2-time1));
82 }
83 }
84
86 const Date& date2,
87 Real strike,
88 bool extrapolate)
89 const {
90 // (redundant) date-based checks
91 QL_REQUIRE(date1 <= date2,
92 date1 << " later than " << date2);
93 checkRange(date2, extrapolate);
94
95 // using the time implementation
96 Time time1 = timeFromReference(date1);
97 Time time2 = timeFromReference(date2);
98 return blackForwardVariance(time1, time2, strike, extrapolate);
99 }
100
102 Time time2,
103 Real strike,
104 bool extrapolate) const {
105 QL_REQUIRE(time1 <= time2,
106 time1 << " later than " << time2);
107 checkRange(time2, extrapolate);
108 checkStrike(strike, extrapolate);
109 Real v1 = blackVarianceImpl(time1, strike);
110 Real v2 = blackVarianceImpl(time2, strike);
111 QL_ENSURE(v2 >= v1,
112 "variances must be non-decreasing");
113 return v2-v1;
114 }
115
118 const DayCounter& dc)
119 : BlackVolTermStructure(bdc, dc) {}
120
122 const Date& refDate,
123 const Calendar& cal,
125 const DayCounter& dc)
126 : BlackVolTermStructure(refDate, cal, bdc, dc) {}
127
129 Natural settlementDays,
130 const Calendar& cal,
132 const DayCounter& dc)
133 : BlackVolTermStructure(settlementDays, cal, bdc, dc) {}
134
137 const DayCounter& dc)
138 : BlackVolTermStructure(bdc, dc) {}
139
141 const Date& refDate,
142 const Calendar& cal,
144 const DayCounter& dc)
145 : BlackVolTermStructure(refDate, cal, bdc, dc) {}
146
148 Natural settlementDays,
149 const Calendar& cal,
151 const DayCounter& dc)
152 : BlackVolTermStructure(settlementDays, cal, bdc, dc) {}
153
154}
Black volatility term structure base classes.
BlackVarianceTermStructure(BusinessDayConvention bdc=Following, const DayCounter &dc=DayCounter())
default constructor
Black-volatility term structure.
BlackVolTermStructure(BusinessDayConvention bdc=Following, const DayCounter &dc=DayCounter())
default constructor
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
BlackVolatilityTermStructure(BusinessDayConvention bdc=Following, const DayCounter &dc=DayCounter())
default constructor
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time timeFromReference(const Date &date) const
date/time conversion
void checkRange(const Date &d, bool extrapolate) const
date-range check
Volatility term structure.
void checkStrike(Rate strike, bool extrapolate) const
strike-range check
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
Definition: errors.hpp:130
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
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