1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*
4  Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
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 Cox-Ingersoll-Ross model
22 */
23 
24 #ifndef quantlib_cox_ingersoll_ross_hpp
25 #define quantlib_cox_ingersoll_ross_hpp
26 
27 #include <ql/models/shortrate/onefactormodel.hpp>
28 #include <ql/stochasticprocess.hpp>
29 #include <ql/processes/eulerdiscretization.hpp>
30 
31 namespace QuantLib {
32 
33     //! Cox-Ingersoll-Ross model class.
34     /*! This class implements the Cox-Ingersoll-Ross model defined by
35         \f[
36             dr_t = k(\theta - r_t)dt + \sqrt{r_t}\sigma dW_t .
37         \f]
38 
39         \bug this class was not tested enough to guarantee
40              its functionality.
41 
42         \ingroup shortrate
43     */
44     class CoxIngersollRoss : public OneFactorAffineModel {
45       public:
46         CoxIngersollRoss(Rate r0 = 0.05,
47                          Real theta = 0.1,
48                          Real k = 0.1,
49                          Real sigma = 0.1,
50                          bool withFellerConstraint = true);
51 
52         virtual Real discountBondOption(Option::Type type,
53                                         Real strike,
54                                         Time maturity,
55                                         Time bondMaturity) const;
56 
57         virtual ext::shared_ptr<ShortRateDynamics> dynamics() const;
58 
59         ext::shared_ptr<Lattice> tree(const TimeGrid& grid) const;
60 
61         class Dynamics;
62       protected:
63         Real A(Time t, Time T) const;
64         Real B(Time t, Time T) const;
65 
theta() const66         Real theta() const { return theta_(0.0); }
k() const67         Real k() const { return k_(0.0); }
sigma() const68         Real sigma() const { return sigma_(0.0); }
x0() const69         Real x0() const { return r0_(0.0); }
70 
71       private:
72         class VolatilityConstraint;
73         class HelperProcess;
74 
75         Parameter& theta_;
76         Parameter& k_;
77         Parameter& sigma_;
78         Parameter& r0_;
79     };
80 
81     class CoxIngersollRoss::HelperProcess : public StochasticProcess1D {
82       public:
HelperProcess(Real theta,Real k,Real sigma,Real y0)83         HelperProcess(Real theta, Real k, Real sigma, Real y0)
84         : y0_(y0), theta_(theta), k_(k), sigma_(sigma) {
85             discretization_ =
86                 ext::shared_ptr<discretization>(new EulerDiscretization);
87         }
88 
x0() const89         Real x0() const {
90             return y0_;
91         }
drift(Time,Real y) const92         Real drift(Time, Real y) const {
93             return (0.5*theta_*k_ - 0.125*sigma_*sigma_)/y
94                 - 0.5*k_*y;
95         }
diffusion(Time,Real) const96         Real diffusion(Time, Real) const {
97             return 0.5*sigma_;
98         }
99 
100       private:
101         Real y0_, theta_, k_, sigma_;
102     };
103 
104     //! %Dynamics of the short-rate under the Cox-Ingersoll-Ross model
105     /*! The state variable \f$ y_t \f$ will here be the square-root of the
106         short-rate. It satisfies the following stochastic equation
107         \f[
108             dy_t=\left[
109                     (\frac{k\theta }{2}+\frac{\sigma ^2}{8})\frac{1}{y_t}-
110                     \frac{k}{2}y_t \right] d_t+ \frac{\sigma }{2}dW_{t}
111         \f].
112     */
113     class CoxIngersollRoss::Dynamics :
114         public OneFactorModel::ShortRateDynamics {
115       public:
Dynamics(Real theta,Real k,Real sigma,Real x0)116         Dynamics(Real theta,
117                  Real k,
118                  Real sigma,
119                  Real x0)
120         : ShortRateDynamics(ext::shared_ptr<StochasticProcess1D>(
121                         new HelperProcess(theta, k, sigma, std::sqrt(x0)))) {}
122 
variable(Time,Rate r) const123         virtual Real variable(Time, Rate r) const {
124             return std::sqrt(r);
125         }
shortRate(Time,Real y) const126         virtual Real shortRate(Time, Real y) const {
127             return y*y;
128         }
129     };
130 
131 }
132 
133 
134 #endif
135 
136