1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*
4  Copyright (C) 2020 Lew Wei Hao
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 coxingersollross.hpp
21     \brief CoxIngersollRoss process
22 */
23 
24 #ifndef quantlib_coxingersollross_process_hpp
25 #define quantlib_coxingersollross_process_hpp
26 
27 #include <ql/stochasticprocess.hpp>
28 
29 namespace QuantLib {
30 
31     //! CoxIngersollRoss process class
32     /*! This class describes the CoxIngersollRoss process governed by
33         \f[
34             dx = a (r - x_t) dt + \sqrt{x_t}\sigma dW_t.
35         \f]
36 
37         \ingroup processes
38     */
39     class CoxIngersollRossProcess : public StochasticProcess1D {
40       public:
41         CoxIngersollRossProcess(Real speed,
42                                  Volatility vol,
43                                  Real x0 = 0.0,
44                                  Real level = 0.0);
45         //@{
46         Real drift(Time t,
47                    Real x) const;
48         Real diffusion(Time t,
49                        Real x) const;
50         Real expectation(Time t0,
51                          Real x0,
52                          Time dt) const;
53         Real stdDeviation(Time t0,
54                           Real x0,
55                           Time dt) const;
56         //@}
57         Real x0() const;
58         Real speed() const;
59         Real volatility() const;
60         Real level() const;
61         Real variance(Time t0,
62                       Real x0,
63                       Time dt) const;
64       private:
65         Real x0_, speed_, level_;
66         Volatility volatility_;
67     };
68 
69     // inline
70 
x0() const71     inline Real CoxIngersollRossProcess::x0() const {
72         return x0_;
73     }
74 
speed() const75     inline Real CoxIngersollRossProcess::speed() const {
76         return speed_;
77     }
78 
volatility() const79     inline Real CoxIngersollRossProcess::volatility() const {
80         return volatility_;
81     }
82 
level() const83     inline Real CoxIngersollRossProcess::level() const {
84         return level_;
85     }
86 
drift(Time,Real x) const87     inline Real CoxIngersollRossProcess::drift(Time, Real x) const {
88         return speed_ * (level_ - x);
89     }
90 
diffusion(Time,Real) const91     inline Real CoxIngersollRossProcess::diffusion(Time, Real) const {
92         return volatility_;
93     }
94 
expectation(Time,Real x0,Time dt) const95     inline Real CoxIngersollRossProcess::expectation(Time, Real x0,
96                                                Time dt) const {
97         return level_ + (x0 - level_) * std::exp(-speed_*dt);
98     }
99 
stdDeviation(Time t,Real x0,Time dt) const100     inline Real CoxIngersollRossProcess::stdDeviation(Time t, Real x0,
101                                                 Time dt) const {
102         return std::sqrt(variance(t,x0,dt));
103     }
104 
105 }
106 
107 #endif
108