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:
LocalVolCurve(const Handle<BlackVarianceCurve> & curve)35         LocalVolCurve(const Handle<BlackVarianceCurve>& curve)
36         : LocalVolTermStructure(curve->businessDayConvention(),
37                                 curve->dayCounter()),
38           blackVarianceCurve_(curve) {
39             registerWith(blackVarianceCurve_);
40         }
41         //! \name TermStructure interface
42         //@{
referenceDate() const43         const Date& referenceDate() const {
44             return blackVarianceCurve_->referenceDate();
45         }
calendar() const46         Calendar calendar() const {
47             return blackVarianceCurve_->calendar();
48         }
dayCounter() const49         DayCounter dayCounter() const {
50             return blackVarianceCurve_->dayCounter();
51         }
maxDate() const52         Date maxDate() const {
53             return blackVarianceCurve_->maxDate();
54         }
55         //@}
56         //! \name VolatilityTermStructure interface
57         //@{
minStrike() const58         Real minStrike() const {
59             return QL_MIN_REAL;
60         }
maxStrike() const61         Real maxStrike() const {
62             return QL_MAX_REAL;
63         }
64         //@}
65         //! \name Visitability
66         //@{
67         virtual void accept(AcyclicVisitor&);
68         //@}
69       protected:
70         Volatility localVolImpl(Time, Real) const;
71       private:
72         Handle<BlackVarianceCurve> blackVarianceCurve_;
73     };
74 
75 
76 
77     // inline definitions
78 
accept(AcyclicVisitor & v)79     inline void LocalVolCurve::accept(AcyclicVisitor& v) {
80         Visitor<LocalVolCurve>* v1 =
81             dynamic_cast<Visitor<LocalVolCurve>*>(&v);
82         if (v1 != 0)
83             v1->visit(*this);
84         else
85             LocalVolTermStructure::accept(v);
86     }
87 
88     /*! The relation
89         \f[
90             \int_0^T \sigma_L^2(t)dt = \sigma_B^2 T
91         \f]
92         holds, where \f$ \sigma_L(t) \f$ is the local volatility at
93         time \f$ t \f$ and \f$ \sigma_B(T) \f$ is the Black
94         volatility for maturity \f$ T \f$. From the above, the formula
95         \f[
96             \sigma_L(t) = \sqrt{\frac{\mathrm{d}}{\mathrm{d}t}\sigma_B^2(t)t}
97         \f]
98         can be deduced which is here implemented.
99     */
localVolImpl(Time t,Real dummy) const100     inline Volatility LocalVolCurve::localVolImpl(Time t, Real dummy) const {
101 
102         Time dt = (1.0/365.0);
103         Real var1 = blackVarianceCurve_->blackVariance(t, dummy, true);
104         Real var2 = blackVarianceCurve_->blackVariance(t+dt, dummy, true);
105         Real derivative = (var2-var1)/dt;
106         return std::sqrt(derivative);
107     }
108 
109 }
110 
111 
112 #endif
113