Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/termstructures/volatility/equityfx/localvolcurve.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 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
20
/*! \file localvolcurve.hpp
21
    \brief Local volatility curve derived from a Black curve
22
*/
23
24
#ifndef quantlib_localvolcurve_hpp
25
#define quantlib_localvolcurve_hpp
26
27
#include <ql/termstructures/volatility/equityfx/blackvariancecurve.hpp>
28
#include <ql/termstructures/volatility/equityfx/localvoltermstructure.hpp>
29
30
namespace QuantLib {
31
32
    //! Local volatility curve derived from a Black curve
33
    class LocalVolCurve : public LocalVolTermStructure {
34
      public:
35
        LocalVolCurve(const Handle<BlackVarianceCurve>& curve)
36
0
        : LocalVolTermStructure(curve->businessDayConvention(),
37
0
                                curve->dayCounter()),
38
0
          blackVarianceCurve_(curve) {
39
0
            registerWith(blackVarianceCurve_);
40
0
        }
41
        //! \name TermStructure interface
42
        //@{
43
0
        const Date& referenceDate() const override { return blackVarianceCurve_->referenceDate(); }
44
0
        Calendar calendar() const override { return blackVarianceCurve_->calendar(); }
45
0
        DayCounter dayCounter() const override { return blackVarianceCurve_->dayCounter(); }
46
0
        Date maxDate() const override { return blackVarianceCurve_->maxDate(); }
47
        //@}
48
        //! \name VolatilityTermStructure interface
49
        //@{
50
0
        Real minStrike() const override { return QL_MIN_REAL; }
51
0
        Real maxStrike() const override { return QL_MAX_REAL; }
52
        //@}
53
        //! \name Visitability
54
        //@{
55
        void accept(AcyclicVisitor&) override;
56
        //@}
57
      protected:
58
        Volatility localVolImpl(Time, Real) const override;
59
60
      private:
61
        Handle<BlackVarianceCurve> blackVarianceCurve_;
62
    };
63
64
65
66
    // inline definitions
67
68
0
    inline void LocalVolCurve::accept(AcyclicVisitor& v) {
69
0
        auto* v1 = dynamic_cast<Visitor<LocalVolCurve>*>(&v);
70
0
        if (v1 != nullptr)
71
0
            v1->visit(*this);
72
0
        else
73
0
            LocalVolTermStructure::accept(v);
74
0
    }
75
76
    /*! The relation
77
        \f[
78
            \int_0^T \sigma_L^2(t)dt = \sigma_B^2 T
79
        \f]
80
        holds, where \f$ \sigma_L(t) \f$ is the local volatility at
81
        time \f$ t \f$ and \f$ \sigma_B(T) \f$ is the Black
82
        volatility for maturity \f$ T \f$. From the above, the formula
83
        \f[
84
            \sigma_L(t) = \sqrt{\frac{\mathrm{d}}{\mathrm{d}t}\sigma_B^2(t)t}
85
        \f]
86
        can be deduced which is here implemented.
87
    */
88
0
    inline Volatility LocalVolCurve::localVolImpl(Time t, Real dummy) const {
89
90
0
        Time dt = (1.0/365.0);
91
0
        Real var1 = blackVarianceCurve_->blackVariance(t, dummy, true);
92
0
        Real var2 = blackVarianceCurve_->blackVariance(t+dt, dummy, true);
93
0
        Real derivative = (var2-var1)/dt;
94
0
        return std::sqrt(derivative);
95
0
    }
96
97
}
98
99
100
#endif