QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
extendedblackvariancesurface.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) 2008 Frank Hövermann
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
22#include <utility>
23
24namespace QuantLib {
25
27 const Date& referenceDate,
28 const Calendar& calendar,
29 const std::vector<Date>& dates,
30 std::vector<Real> strikes,
31 const std::vector<Handle<Quote> >& volatilities,
32 DayCounter dayCounter,
35 : BlackVarianceTermStructure(referenceDate, calendar), dayCounter_(std::move(dayCounter)),
36 maxDate_(dates.back()), volatilities_(volatilities), strikes_(std::move(strikes)),
37 lowerExtrapolation_(lowerEx), upperExtrapolation_(upperEx) {
38
39 QL_REQUIRE(dates.size()*strikes_.size()==volatilities_.size(),
40 "size mismatch between date vector and vol matrix columns "
41 "and/or between money-strike vector and vol matrix rows");
42
43 QL_REQUIRE(dates[0] > referenceDate,
44 "cannot have dates_[0] <= referenceDate_");
45
46
47 times_ = std::vector<Time>(dates.size()+1);
48 times_[0] = 0.0;
49
50 for (Size j=1; j<=dates.size(); j++) {
51 times_[j] = timeFromReference(dates[j-1]);
52 QL_REQUIRE(times_[j]>times_[j-1],
53 "dates must be sorted unique");
54 }
55
56 variances_ = Matrix(strikes_.size(), dates.size()+1);
58
59 setInterpolation<Bilinear>();
60
61 for (const auto& volatilitie : volatilities_)
62 registerWith(volatilitie);
63 }
64
66
67 for (Size i=0; i<times_.size()+1; i++) {
68 variances_[0][i] = 0.0;
69 }
70 for (Size j=1; j<=times_.size(); j++) {
71 for (Size i=0; i<strikes_.size(); i++) {
72 Volatility sigma = volatilities_[i*times_.size()+j-1]->value();
73 variances_[i][j] = times_[j] * sigma * sigma;
74 QL_REQUIRE(variances_[i][j]>=variances_[i][j-1],
75 "variance must be non-decreasing");
76 }
77 }
78 }
79
84 }
85
87 Real strike) const {
88
89 if (t==0.0) return 0.0;
90
91 // enforce constant extrapolation when required
92 if (strike < strikes_.front()
94 strike = strikes_.front();
95 if (strike > strikes_.back()
97 strike = strikes_.back();
98
99 if (t<=times_.back())
100 return varianceSurface_(t, strike, true);
101 else // t>times_.back() || extrapolate
102 return varianceSurface_(times_.back(), strike, true) *
103 t/times_.back();
104 }
105
106}
107
bilinear interpolation between discrete points
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
const std::vector< Handle< Quote > > & volatilities_
ExtendedBlackVarianceSurface(const Date &referenceDate, const Calendar &calendar, const std::vector< Date > &dates, std::vector< Real > strikes, const std::vector< Handle< Quote > > &volatilities, DayCounter dayCounter, Extrapolation lowerExtrapolation=InterpolatorDefaultExtrapolation, Extrapolation upperExtrapolation=InterpolatorDefaultExtrapolation)
Real blackVarianceImpl(Time t, Real strike) const override
Black variance calculation.
Shared handle to an observable.
Definition: handle.hpp:41
Matrix used in linear algebra.
Definition: matrix.hpp:41
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
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
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Black volatility surface modelled as variance surface.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
std::size_t Size
size of a container
Definition: types.hpp:58
Real sigma
Definition: any.hpp:35
STL namespace.