1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*
4  Copyright (C) 2015 Johannes Göttker-Schnetmann
5  Copyright (C) 2015 Klaus Spanderen
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 hestonslvprocess.cpp
22     \brief Heston stochastic local volatility process
23 */
24 
25 #include <ql/math/functional.hpp>
26 #include <ql/math/distributions/normaldistribution.hpp>
27 #include <ql/experimental/processes/hestonslvprocess.hpp>
28 #include <ql/methods/finitedifferences/utilities/squarerootprocessrndcalculator.hpp>
29 
30 namespace QuantLib {
31 
HestonSLVProcess(const ext::shared_ptr<HestonProcess> & hestonProcess,const ext::shared_ptr<LocalVolTermStructure> & leverageFct,const Real mixingFactor)32     HestonSLVProcess::HestonSLVProcess(
33         const ext::shared_ptr<HestonProcess>& hestonProcess,
34         const ext::shared_ptr<LocalVolTermStructure>& leverageFct,
35         const Real mixingFactor)
36     : mixingFactor_(mixingFactor),
37       hestonProcess_(hestonProcess),
38       leverageFct_(leverageFct) {
39         registerWith(hestonProcess);
40         update();
41     };
42 
update()43     void HestonSLVProcess::update() {
44         v0_    = hestonProcess_->v0();
45         kappa_ = hestonProcess_->kappa();
46         theta_ = hestonProcess_->theta();
47         sigma_ = hestonProcess_->sigma();
48         rho_   = hestonProcess_->rho();
49         mixedSigma_ = mixingFactor_ * sigma_;
50     }
51 
drift(Time t,const Array & x) const52     Disposable<Array> HestonSLVProcess::drift(Time t, const Array& x) const {
53         Array tmp(2);
54 
55         const Volatility vol =
56            std::max(1e-8, std::sqrt(x[1])*leverageFct_->localVol(t, x[0], true));
57 
58         tmp[0] = riskFreeRate()->forwardRate(t, t, Continuous)
59                - dividendYield()->forwardRate(t, t, Continuous)
60                - 0.5*vol*vol;
61 
62         tmp[1] = kappa_*(theta_ - x[1]);
63 
64         return tmp;
65     }
66 
diffusion(Time t,const Array & x) const67     Disposable<Matrix> HestonSLVProcess::diffusion(Time t, const Array& x)
68     const {
69 
70         const Real vol =
71             std::max(1e-8, std::sqrt(x[1])*leverageFct_->localVol(t, x[0], true));
72 
73         const Real sigma2 = mixedSigma_ * std::sqrt(x[1]);
74         const Real sqrhov = std::sqrt(1.0 - rho_*rho_);
75 
76         Matrix tmp(2,2);
77         tmp[0][0] = vol;          tmp[0][1] = 0.0;
78         tmp[1][0] = rho_*sigma2;  tmp[1][1] = sqrhov*sigma2;
79 
80         return tmp;
81     }
82 
evolve(Time t0,const Array & x0,Time dt,const Array & dw) const83     Disposable<Array> HestonSLVProcess::evolve(
84         Time t0, const Array& x0, Time dt, const Array& dw) const {
85         Array retVal(2);
86 
87         const Real ex = std::exp(-kappa_*dt);
88 
89         const Real m  =  theta_+(x0[1]-theta_)*ex;
90         const Real s2 =  x0[1]*mixedSigma_*mixedSigma_*ex/kappa_*(1-ex)
91                        + theta_*mixedSigma_*mixedSigma_/(2*kappa_)*(1-ex)*(1-ex);
92         const Real psi = s2/(m*m);
93 
94         if (psi < 1.5) {
95             const Real b2 = 2/psi-1+std::sqrt(2/psi*(2/psi-1));
96             const Real b  = std::sqrt(b2);
97             const Real a  = m/(1+b2);
98 
99             retVal[1] = a*(b+dw[1])*(b+dw[1]);
100         }
101         else {
102             const Real p = (psi-1)/(psi+1);
103             const Real beta = (1-p)/m;
104             const Real u = CumulativeNormalDistribution()(dw[1]);
105 
106             retVal[1] = ((u <= p) ? 0.0 : std::log((1-p)/(1-u))/beta);
107         }
108 
109         const Real mu = riskFreeRate()->forwardRate(t0, t0+dt, Continuous)
110              - dividendYield()->forwardRate(t0, t0+dt, Continuous);
111 
112         const Real rho1 = std::sqrt(1-rho_*rho_);
113 
114         const Volatility l_0 = leverageFct_->localVol(t0, x0[0], true);
115         const Real v_0 = 0.5*(x0[1]+retVal[1])*l_0*l_0;
116 
117         retVal[0] = x0[0]*std::exp(mu*dt - 0.5*v_0*dt
118             + rho_/mixedSigma_*l_0 * (
119                   retVal[1] - kappa_*theta_*dt
120                   + 0.5*(x0[1]+retVal[1])*kappa_*dt - x0[1])
121             + rho1*std::sqrt(v_0*dt)*dw[0]);
122 
123         return retVal;
124     }
125 }
126