Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/processes/coxingersollrossprocess.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) 2020 Lew Wei Hao
5
 Copyright (C) 2021 Magnus Mencke
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
 <https://www.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 coxingersollrossprocess.hpp
22
    \brief CoxIngersollRoss process
23
*/
24
25
#ifndef quantlib_coxingersollross_process_hpp
26
#define quantlib_coxingersollross_process_hpp
27
28
#include <ql/stochasticprocess.hpp>
29
#include <ql/math/distributions/normaldistribution.hpp>
30
31
namespace QuantLib {
32
33
    //! CoxIngersollRoss process class
34
    /*! This class describes the CoxIngersollRoss process governed by
35
        \f[
36
            dx(t) = k (\theta - x(t)) dt + \sigma \sqrt{x(t)} dW(t).
37
        \f]
38
39
        The process is discretized using the Quadratic Exponential scheme.
40
        For details see Leif Andersen,
41
        Efficient Simulation of the Heston Stochastic Volatility Model.
42
43
        \ingroup processes
44
    */
45
    class CoxIngersollRossProcess : public StochasticProcess1D {
46
      public:
47
48
        CoxIngersollRossProcess(Real speed,
49
                                 Volatility vol,
50
                                 Real x0 = 0.0,
51
                                 Real level = 0.0);
52
        //@{
53
        Real drift(Time t, Real x) const override;
54
        Real diffusion(Time t, Real x) const override;
55
        Real expectation(Time t0, Real x0, Time dt) const override;
56
        Real stdDeviation(Time t0, Real x0, Time dt) const override;
57
        //@}
58
        Real x0() const override;
59
        Real speed() const;
60
        Real volatility() const;
61
        Real level() const;
62
        Real variance(Time t0, Real x0, Time dt) const override;
63
        Real evolve (Time t0,
64
                     Real x0,
65
                     Time dt,
66
                     Real dw) const override;
67
      private:
68
        Real x0_, speed_, level_;
69
        Volatility volatility_;
70
    };
71
72
    // inline
73
74
0
    inline Real CoxIngersollRossProcess::x0() const {
75
0
        return x0_;
76
0
    }
77
78
0
    inline Real CoxIngersollRossProcess::speed() const {
79
0
        return speed_;
80
0
    }
81
82
0
    inline Real CoxIngersollRossProcess::volatility() const {
83
0
        return volatility_;
84
0
    }
85
86
0
    inline Real CoxIngersollRossProcess::level() const {
87
0
        return level_;
88
0
    }
89
90
0
    inline Real CoxIngersollRossProcess::drift(Time, Real x) const {
91
0
        return speed_ * (level_ - x);
92
0
    }
93
94
0
    inline Real CoxIngersollRossProcess::diffusion(Time, Real) const {
95
0
        return volatility_;
96
0
    }
97
98
    inline Real CoxIngersollRossProcess::expectation(Time, Real x0,
99
0
                                               Time dt) const {
100
0
        return level_ + (x0 - level_) * std::exp(-speed_*dt);
101
0
    }
102
103
    inline Real CoxIngersollRossProcess::stdDeviation(Time t, Real x0,
104
0
                                                Time dt) const {
105
0
        return std::sqrt(variance(t,x0,dt));
106
0
    }
107
108
    inline Real CoxIngersollRossProcess::evolve (Time t0,
109
      Real x0,
110
                                    Time dt,
111
0
                                    Real dw) const {
112
0
        Real result;
113
114
0
        const Real ex = std::exp(-speed_*dt);
115
116
0
        const Real m  =  level_+(x0-level_)*ex;
117
0
        const Real s2 =  x0*volatility_*volatility_*ex/speed_*(1-ex)
118
0
                       + level_*volatility_*volatility_/(2*speed_)*(1-ex)*(1-ex);
119
0
        const Real psi = s2/(m*m);
120
121
0
        if (psi <= 1.5) {
122
0
            const Real b2 = 2/psi-1+std::sqrt(2/psi*(2/psi-1));
123
0
            const Real b  = std::sqrt(b2);
124
0
            const Real a  = m/(1+b2);
125
126
0
            result = a*(b+dw)*(b+dw);
127
0
        }
128
0
        else {
129
0
            const Real p = (psi-1)/(psi+1);
130
0
            const Real beta = (1-p)/m;
131
132
0
            const Real u = CumulativeNormalDistribution()(dw);
133
134
0
            result = ((u <= p) ? 0.0 : Real(std::log((1-p)/(1-u))/beta));
135
0
        }
136
137
0
        return result;
138
0
    }
139
140
}
141
142
#endif