QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
blackvariancesurface.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2002, 2003, 2004 Ferdinando Ametrano
5 Copyright (C) 2003, 2004 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#include <ql/math/interpolations/bilinearinterpolation.hpp>
22#include <ql/termstructures/volatility/equityfx/blackvariancesurface.hpp>
23#include <utility>
24
25namespace QuantLib {
26
28 const Calendar& cal,
29 const std::vector<Date>& dates,
30 std::vector<Real> strikes,
31 const Matrix& blackVolMatrix,
32 DayCounter dayCounter,
35 : BlackVarianceTermStructure(referenceDate, cal), dayCounter_(std::move(dayCounter)),
36 maxDate_(dates.back()), strikes_(std::move(strikes)), lowerExtrapolation_(lowerEx),
37 upperExtrapolation_(upperEx) {
38
39 QL_REQUIRE(dates.size()==blackVolMatrix.columns(),
40 "mismatch between date vector and vol matrix colums");
41 QL_REQUIRE(strikes_.size()==blackVolMatrix.rows(),
42 "mismatch between money-strike vector and vol matrix rows");
43
44 QL_REQUIRE(dates[0]>=referenceDate,
45 "cannot have dates[0] < referenceDate");
46
47 Size j, i;
48 times_ = std::vector<Time>(dates.size()+1);
49 times_[0] = 0.0;
50 variances_ = Matrix(strikes_.size(), dates.size()+1);
51 for (i=0; i<blackVolMatrix.rows(); i++) {
52 variances_[i][0] = 0.0;
53 }
54 for (j=1; j<=blackVolMatrix.columns(); j++) {
55 times_[j] = timeFromReference(dates[j-1]);
56 QL_REQUIRE(times_[j]>times_[j-1],
57 "dates must be sorted unique!");
58 for (i=0; i<blackVolMatrix.rows(); i++) {
59 variances_[i][j] = times_[j] *
60 blackVolMatrix[i][j-1]*blackVolMatrix[i][j-1];
61 }
62 }
63 // default: bilinear interpolation
64 setInterpolation<Bilinear>();
65 }
66
68
69 if (t==0.0) return 0.0;
70
71 // enforce constant extrapolation when required
72 if (strike < strikes_.front()
74 strike = strikes_.front();
75 if (strike > strikes_.back()
77 strike = strikes_.back();
78
79 if (t<=times_.back())
80 return varianceSurface_(t, strike, true);
81 else // t>times_.back() || extrapolate
82 return varianceSurface_(times_.back(), strike, true) *
83 t/times_.back();
84 }
85
86}
87
Real blackVarianceImpl(Time t, Real strike) const override
Black variance calculation.
BlackVarianceSurface(const Date &referenceDate, const Calendar &cal, const std::vector< Date > &dates, std::vector< Real > strikes, const Matrix &blackVolMatrix, DayCounter dayCounter, Extrapolation lowerExtrapolation=InterpolatorDefaultExtrapolation, Extrapolation upperExtrapolation=InterpolatorDefaultExtrapolation)
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Matrix used in linear algebra.
Definition: matrix.hpp:41
Size rows() const
Definition: matrix.hpp:504
Size columns() const
Definition: matrix.hpp:508
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
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.