Coverage Report

Created: 2026-09-28 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/termstructures/volatility/equityfx/blackvariancesurface.cpp
Line
Count
Source
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
 <https://www.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
25
namespace QuantLib {
26
27
    BlackVarianceSurface::BlackVarianceSurface(const Date& referenceDate,
28
                                               const Calendar& cal,
29
                                               const std::vector<Date>& dates,
30
                                               std::vector<Real> strikes,
31
                                               const Matrix& blackVolMatrix,
32
                                               DayCounter dayCounter,
33
                                               BlackVarianceSurface::Extrapolation lowerEx,
34
                                               BlackVarianceSurface::Extrapolation upperEx)
35
0
    : BlackVarianceTermStructure(referenceDate, cal), dayCounter_(std::move(dayCounter)),
36
0
      maxDate_(dates.back()), strikes_(std::move(strikes)), lowerExtrapolation_(lowerEx),
37
0
      upperExtrapolation_(upperEx) {
38
39
0
        QL_REQUIRE(dates.size()==blackVolMatrix.columns(),
40
0
                   "mismatch between date vector and vol matrix colums");
41
0
        QL_REQUIRE(strikes_.size()==blackVolMatrix.rows(),
42
0
                   "mismatch between money-strike vector and vol matrix rows");
43
44
0
        QL_REQUIRE(dates[0]>=referenceDate,
45
0
                   "cannot have dates[0] < referenceDate");
46
47
0
        Size j, i;
48
0
        times_ = std::vector<Time>(dates.size()+1);
49
0
        times_[0] = 0.0;
50
0
        variances_ = Matrix(strikes_.size(), dates.size()+1);
51
0
        for (i=0; i<blackVolMatrix.rows(); i++) {
52
0
            variances_[i][0] = 0.0;
53
0
        }
54
0
        for (j=1; j<=blackVolMatrix.columns(); j++) {
55
0
            times_[j] = timeFromReference(dates[j-1]);
56
0
            QL_REQUIRE(times_[j]>times_[j-1],
57
0
                       "dates must be sorted unique!");
58
0
            for (i=0; i<blackVolMatrix.rows(); i++) {
59
0
                variances_[i][j] = times_[j] *
60
0
                    blackVolMatrix[i][j-1]*blackVolMatrix[i][j-1];
61
0
            }
62
0
        }
63
        // default: bilinear interpolation
64
0
        setInterpolation<Bilinear>();
65
0
    }
Unexecuted instantiation: QuantLib::BlackVarianceSurface::BlackVarianceSurface(QuantLib::Date const&, QuantLib::Calendar const&, std::__1::vector<QuantLib::Date, std::__1::allocator<QuantLib::Date> > const&, std::__1::vector<double, std::__1::allocator<double> >, QuantLib::Matrix const&, QuantLib::DayCounter, QuantLib::BlackVarianceSurface::Extrapolation, QuantLib::BlackVarianceSurface::Extrapolation)
Unexecuted instantiation: QuantLib::BlackVarianceSurface::BlackVarianceSurface(QuantLib::Date const&, QuantLib::Calendar const&, std::__1::vector<QuantLib::Date, std::__1::allocator<QuantLib::Date> > const&, std::__1::vector<double, std::__1::allocator<double> >, QuantLib::Matrix const&, QuantLib::DayCounter, QuantLib::BlackVarianceSurface::Extrapolation, QuantLib::BlackVarianceSurface::Extrapolation)
66
67
0
    Real BlackVarianceSurface::blackVarianceImpl(Time t, Real strike) const {
68
69
0
        if (t==0.0) return 0.0;
70
71
        // enforce constant extrapolation when required
72
0
        if (strike < strikes_.front()
73
0
            && lowerExtrapolation_ == ConstantExtrapolation)
74
0
            strike = strikes_.front();
75
0
        if (strike > strikes_.back()
76
0
            && upperExtrapolation_ == ConstantExtrapolation)
77
0
            strike = strikes_.back();
78
79
0
        if (t<=times_.back())
80
0
            return varianceSurface_(t, strike, true);
81
0
        else // t>times_.back() || extrapolate
82
0
            return varianceSurface_(times_.back(), strike, true) *
83
0
                t/times_.back();
84
0
    }
85
86
}
87