Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/termstructures/volatility/equityfx/blackvariancesurface.hpp
Line
Count
Source (jump to first uncovered line)
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
/*! \file blackvariancesurface.hpp
22
    \brief Black volatility surface modelled as variance surface
23
*/
24
25
#ifndef quantlib_black_variance_surface_hpp
26
#define quantlib_black_variance_surface_hpp
27
28
#include <ql/termstructures/volatility/equityfx/blackvoltermstructure.hpp>
29
#include <ql/math/matrix.hpp>
30
#include <ql/math/interpolations/interpolation2d.hpp>
31
#include <ql/time/daycounters/actual365fixed.hpp>
32
33
namespace QuantLib {
34
35
    //! Black volatility surface modelled as variance surface
36
    /*! This class calculates time/strike dependent Black volatilities
37
        using as input a matrix of Black volatilities observed in the
38
        market.
39
40
        The calculation is performed interpolating on the variance
41
        surface.  Bilinear interpolation is used as default; this can
42
        be changed by the setInterpolation() method.
43
44
        \todo check time extrapolation
45
46
    */
47
    class BlackVarianceSurface : public BlackVarianceTermStructure {
48
      public:
49
        enum Extrapolation { ConstantExtrapolation,
50
                             InterpolatorDefaultExtrapolation };
51
        BlackVarianceSurface(const Date& referenceDate,
52
                             const Calendar& cal,
53
                             const std::vector<Date>& dates,
54
                             std::vector<Real> strikes,
55
                             const Matrix& blackVolMatrix,
56
                             DayCounter dayCounter,
57
                             Extrapolation lowerExtrapolation = InterpolatorDefaultExtrapolation,
58
                             Extrapolation upperExtrapolation = InterpolatorDefaultExtrapolation);
59
        //! \name TermStructure interface
60
        //@{
61
0
        DayCounter dayCounter() const override { return dayCounter_; }
62
0
        Date maxDate() const override { return maxDate_; }
63
        //@}
64
        //! \name VolatilityTermStructure interface
65
        //@{
66
0
        Real minStrike() const override { return strikes_.front(); }
67
0
        Real maxStrike() const override { return strikes_.back(); }
68
        //@}
69
        //! \name Modifiers
70
        //@{
71
        template <class Interpolator>
72
0
        void setInterpolation(const Interpolator& i = Interpolator()) {
73
0
            varianceSurface_ =
74
0
                i.interpolate(times_.begin(), times_.end(),
75
0
                              strikes_.begin(), strikes_.end(),
76
0
                              variances_);
77
0
            notifyObservers();
78
0
        }
79
        //@}
80
        //! \name Visitability
81
        //@{
82
        void accept(AcyclicVisitor&) override;
83
        //@}
84
      protected:
85
        Real blackVarianceImpl(Time t, Real strike) const override;
86
87
      private:
88
        DayCounter dayCounter_;
89
        Date maxDate_;
90
        std::vector<Real> strikes_;
91
        std::vector<Time> times_;
92
        Matrix variances_;
93
        Interpolation2D varianceSurface_;
94
        Extrapolation lowerExtrapolation_, upperExtrapolation_;
95
    };
96
97
98
    // inline definitions
99
100
0
    inline void BlackVarianceSurface::accept(AcyclicVisitor& v) {
101
0
        auto* v1 = dynamic_cast<Visitor<BlackVarianceSurface>*>(&v);
102
0
        if (v1 != nullptr)
103
0
            v1->visit(*this);
104
0
        else
105
0
            BlackVarianceTermStructure::accept(v);
106
0
    }
107
108
}
109
110
111
#endif