• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 4127568404

pending completion
4127568404

Pull #1473

github

GitHub
Merge 115fd75c4 into 94b218ead
Pull Request #1473: Move clang-analyzer-optin.cplusplus.VirtualCall suppressions to comments

65 of 65 new or added lines in 24 files covered. (100.0%)

53766 of 74813 relevant lines covered (71.87%)

10600601.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.22
/ql/processes/hestonslvprocess.cpp
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/processes/hestonslvprocess.hpp>
26
#include <ql/math/distributions/normaldistribution.hpp>
27
#include <ql/methods/finitedifferences/utilities/squarerootprocessrndcalculator.hpp>
28
#include <utility>
29

30
namespace QuantLib {
31

32
    HestonSLVProcess::HestonSLVProcess(const ext::shared_ptr<HestonProcess>& hestonProcess,
4✔
33
                                       ext::shared_ptr<LocalVolTermStructure> leverageFct,
34
                                       const Real mixingFactor)
4✔
35
    : mixingFactor_(mixingFactor), hestonProcess_(hestonProcess),
4✔
36
      leverageFct_(std::move(leverageFct)) {
4✔
37
        registerWith(hestonProcess);
8✔
38
        setParameters();
4✔
39
    };
4✔
40

41
    void HestonSLVProcess::update() {
×
42
        setParameters();
×
43
        StochasticProcess::update();
×
44
    }
×
45

46
    Array HestonSLVProcess::drift(Time t, const Array& x) const {
669,320✔
47
        Array tmp(2);
48

49
        const Volatility vol =
50
           std::max(1e-8, std::sqrt(x[1])*leverageFct_->localVol(t, x[0], true));
669,320✔
51

52
        tmp[0] = riskFreeRate()->forwardRate(t, t, Continuous).rate()
1,338,640✔
53
               - dividendYield()->forwardRate(t, t, Continuous).rate()
1,338,640✔
54
               - 0.5*vol*vol;
669,320✔
55

56
        tmp[1] = kappa_*(theta_ - x[1]);
669,320✔
57

58
        return tmp;
669,320✔
59
    }
60

61
    Matrix HestonSLVProcess::diffusion(Time t, const Array& x) const {
669,320✔
62

63
        const Real vol =
64
            std::max(1e-8, std::sqrt(x[1])*leverageFct_->localVol(t, x[0], true));
669,320✔
65

66
        const Real sigma2 = mixedSigma_ * std::sqrt(x[1]);
669,320✔
67
        const Real sqrhov = std::sqrt(1.0 - rho_*rho_);
669,320✔
68

69
        Matrix tmp(2,2);
70
        tmp[0][0] = vol;          tmp[0][1] = 0.0;
669,320✔
71
        tmp[1][0] = rho_*sigma2;  tmp[1][1] = sqrhov*sigma2;
669,320✔
72

73
        return tmp;
669,320✔
74
    }
75

76
    Array HestonSLVProcess::evolve(
11,280,000✔
77
        Time t0, const Array& x0, Time dt, const Array& dw) const {
78
        Array retVal(2);
79

80
        const Real ex = std::exp(-kappa_*dt);
11,280,000✔
81

82
        const Real m  =  theta_+(x0[1]-theta_)*ex;
11,280,000✔
83
        const Real s2 =  x0[1]*mixedSigma_*mixedSigma_*ex/kappa_*(1-ex)
11,280,000✔
84
                       + theta_*mixedSigma_*mixedSigma_/(2*kappa_)*(1-ex)*(1-ex);
11,280,000✔
85
        const Real psi = s2/(m*m);
11,280,000✔
86

87
        if (psi < 1.5) {
11,280,000✔
88
            const Real b2 = 2/psi-1+std::sqrt(2/psi*(2/psi-1));
10,563,971✔
89
            const Real b  = std::sqrt(b2);
10,563,971✔
90
            const Real a  = m/(1+b2);
10,563,971✔
91

92
            retVal[1] = a*(b+dw[1])*(b+dw[1]);
10,563,971✔
93
        }
94
        else {
95
            const Real p = (psi-1)/(psi+1);
716,029✔
96
            const Real beta = (1-p)/m;
716,029✔
97
            const Real u = CumulativeNormalDistribution()(dw[1]);
716,029✔
98

99
            retVal[1] = ((u <= p) ? Real(0.0) : std::log((1-p)/(1-u))/beta);
716,029✔
100
        }
101

102
        const Real mu = riskFreeRate()->forwardRate(t0, t0+dt, Continuous).rate()
11,280,000✔
103
             - dividendYield()->forwardRate(t0, t0+dt, Continuous).rate();
22,560,000✔
104

105
        const Real rho1 = std::sqrt(1-rho_*rho_);
11,280,000✔
106

107
        const Volatility l_0 = leverageFct_->localVol(t0, x0[0], true);
11,280,000✔
108
        const Real v_0 = 0.5*(x0[1]+retVal[1])*l_0*l_0;
11,280,000✔
109

110
        retVal[0] = x0[0]*std::exp(mu*dt - 0.5*v_0*dt
11,280,000✔
111
            + rho_/mixedSigma_*l_0 * (
11,280,000✔
112
                  retVal[1] - kappa_*theta_*dt
11,280,000✔
113
                  + 0.5*(x0[1]+retVal[1])*kappa_*dt - x0[1])
11,280,000✔
114
            + rho1*std::sqrt(v_0*dt)*dw[0]);
11,280,000✔
115

116
        return retVal;
11,280,000✔
117
    }
118

119
    void HestonSLVProcess::setParameters() {
4✔
120
        v0_    = hestonProcess_->v0();
4✔
121
        kappa_ = hestonProcess_->kappa();
4✔
122
        theta_ = hestonProcess_->theta();
4✔
123
        sigma_ = hestonProcess_->sigma();
4✔
124
        rho_   = hestonProcess_->rho();
4✔
125
        mixedSigma_ = mixingFactor_ * sigma_;
4✔
126
    }
4✔
127

128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc