QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
stochasticprocess.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2003 Ferdinando Ametrano
5 Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
6 Copyright (C) 2004, 2005 StatPro Italia srl
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22/*! \file stochasticprocess.hpp
23 \brief stochastic processes
24*/
25
26#ifndef quantlib_stochastic_process_hpp
27#define quantlib_stochastic_process_hpp
28
29#include <ql/time/date.hpp>
31#include <ql/math/matrix.hpp>
32
33namespace QuantLib {
34
35 //! multi-dimensional stochastic process class.
36 /*! This class describes a stochastic process governed by
37 \f[
38 d\mathrm{x}_t = \mu(t, x_t)\mathrm{d}t
39 + \sigma(t, \mathrm{x}_t) \cdot d\mathrm{W}_t.
40 \f]
41 */
42 class StochasticProcess : public Observer, public Observable {
43 public:
44 //! discretization of a stochastic process over a given time interval
46 public:
47 virtual ~discretization() = default;
49 Time t0,
50 const Array& x0,
51 Time dt) const = 0;
53 Time t0,
54 const Array& x0,
55 Time dt) const = 0;
57 Time t0,
58 const Array& x0,
59 Time dt) const = 0;
60 };
61 ~StochasticProcess() override = default;
62 //! \name Stochastic process interface
63 //@{
64 //! returns the number of dimensions of the stochastic process
65 virtual Size size() const = 0;
66 //! returns the number of independent factors of the process
67 virtual Size factors() const;
68 //! returns the initial values of the state variables
69 virtual Array initialValues() const = 0;
70 /*! \brief returns the drift part of the equation, i.e.,
71 \f$ \mu(t, \mathrm{x}_t) \f$
72 */
73 virtual Array drift(Time t,
74 const Array& x) const = 0;
75 /*! \brief returns the diffusion part of the equation, i.e.
76 \f$ \sigma(t, \mathrm{x}_t) \f$
77 */
79 const Array& x) const = 0;
80 /*! returns the expectation
81 \f$ E(\mathrm{x}_{t_0 + \Delta t}
82 | \mathrm{x}_{t_0} = \mathrm{x}_0) \f$
83 of the process after a time interval \f$ \Delta t \f$
84 according to the given discretization. This method can be
85 overridden in derived classes which want to hard-code a
86 particular discretization.
87 */
88 virtual Array expectation(Time t0,
89 const Array& x0,
90 Time dt) const;
91 /*! returns the standard deviation
92 \f$ S(\mathrm{x}_{t_0 + \Delta t}
93 | \mathrm{x}_{t_0} = \mathrm{x}_0) \f$
94 of the process after a time interval \f$ \Delta t \f$
95 according to the given discretization. This method can be
96 overridden in derived classes which want to hard-code a
97 particular discretization.
98 */
99 virtual Matrix stdDeviation(Time t0,
100 const Array& x0,
101 Time dt) const;
102 /*! returns the covariance
103 \f$ V(\mathrm{x}_{t_0 + \Delta t}
104 | \mathrm{x}_{t_0} = \mathrm{x}_0) \f$
105 of the process after a time interval \f$ \Delta t \f$
106 according to the given discretization. This method can be
107 overridden in derived classes which want to hard-code a
108 particular discretization.
109 */
110 virtual Matrix covariance(Time t0,
111 const Array& x0,
112 Time dt) const;
113 /*! returns the asset value after a time interval \f$ \Delta t
114 \f$ according to the given discretization. By default, it
115 returns
116 \f[
117 E(\mathrm{x}_0,t_0,\Delta t) +
118 S(\mathrm{x}_0,t_0,\Delta t) \cdot \Delta \mathrm{w}
119 \f]
120 where \f$ E \f$ is the expectation and \f$ S \f$ the
121 standard deviation.
122 */
123 virtual Array evolve(Time t0,
124 const Array& x0,
125 Time dt,
126 const Array& dw) const;
127 /*! applies a change to the asset value. By default, it
128 returns \f$ \mathrm{x} + \Delta \mathrm{x} \f$.
129 */
130 virtual Array apply(const Array& x0,
131 const Array& dx) const;
132 //@}
133
134 //! \name utilities
135 //@{
136 /*! returns the time value corresponding to the given date
137 in the reference system of the stochastic process.
138
139 \note As a number of processes might not need this
140 functionality, a default implementation is given
141 which raises an exception.
142 */
143 virtual Time time(const Date&) const;
144 //@}
145
146 //! \name Observer interface
147 //@{
148 void update() override;
149 //@}
150 protected:
151 StochasticProcess() = default;
152 explicit StochasticProcess(ext::shared_ptr<discretization>);
153 ext::shared_ptr<discretization> discretization_;
154 };
155
156
157 //! 1-dimensional stochastic process
158 /*! This class describes a stochastic process governed by
159 \f[
160 dx_t = \mu(t, x_t)dt + \sigma(t, x_t)dW_t.
161 \f]
162 */
164 public:
165 //! discretization of a 1-D stochastic process
167 public:
168 virtual ~discretization() = default;
170 Time t0, Real x0, Time dt) const = 0;
172 Time t0, Real x0, Time dt) const = 0;
174 Time t0, Real x0, Time dt) const = 0;
175 };
176 //! \name 1-D stochastic process interface
177 //@{
178 //! returns the initial value of the state variable
179 virtual Real x0() const = 0;
180 //! returns the drift part of the equation, i.e. \f$ \mu(t, x_t) \f$
181 virtual Real drift(Time t, Real x) const = 0;
182 /*! \brief returns the diffusion part of the equation, i.e.
183 \f$ \sigma(t, x_t) \f$
184 */
185 virtual Real diffusion(Time t, Real x) const = 0;
186 /*! returns the expectation
187 \f$ E(x_{t_0 + \Delta t} | x_{t_0} = x_0) \f$
188 of the process after a time interval \f$ \Delta t \f$
189 according to the given discretization. This method can be
190 overridden in derived classes which want to hard-code a
191 particular discretization.
192 */
193 virtual Real expectation(Time t0, Real x0, Time dt) const;
194 /*! returns the standard deviation
195 \f$ S(x_{t_0 + \Delta t} | x_{t_0} = x_0) \f$
196 of the process after a time interval \f$ \Delta t \f$
197 according to the given discretization. This method can be
198 overridden in derived classes which want to hard-code a
199 particular discretization.
200 */
201 virtual Real stdDeviation(Time t0, Real x0, Time dt) const;
202 /*! returns the variance
203 \f$ V(x_{t_0 + \Delta t} | x_{t_0} = x_0) \f$
204 of the process after a time interval \f$ \Delta t \f$
205 according to the given discretization. This method can be
206 overridden in derived classes which want to hard-code a
207 particular discretization.
208 */
209 virtual Real variance(Time t0, Real x0, Time dt) const;
210 /*! returns the asset value after a time interval \f$ \Delta t
211 \f$ according to the given discretization. By default, it
212 returns
213 \f[
214 E(x_0,t_0,\Delta t) + S(x_0,t_0,\Delta t) \cdot \Delta w
215 \f]
216 where \f$ E \f$ is the expectation and \f$ S \f$ the
217 standard deviation.
218 */
219 virtual Real evolve(Time t0, Real x0, Time dt, Real dw) const;
220 /*! applies a change to the asset value. By default, it
221 returns \f$ x + \Delta x \f$.
222 */
223 virtual Real apply(Real x0, Real dx) const;
224 //@}
225 protected:
227 explicit StochasticProcess1D(ext::shared_ptr<discretization>);
228 ext::shared_ptr<discretization> discretization_;
229 private:
230 // StochasticProcess interface implementation
231 Size size() const override;
232 Array initialValues() const override;
233 Array drift(Time t, const Array& x) const override;
234 Matrix diffusion(Time t, const Array& x) const override;
235 Array expectation(Time t0, const Array& x0, Time dt) const override;
236 Matrix stdDeviation(Time t0, const Array& x0, Time dt) const override;
237 Matrix covariance(Time t0, const Array& x0, Time dt) const override;
238 Array evolve(Time t0, const Array& x0, Time dt, const Array& dw) const override;
239 Array apply(const Array& x0, const Array& dx) const override;
240 };
241
242
243 // inline definitions
244
246 return 1;
247 }
248
250 Array a(1, x0());
251 return a;
252 }
253
254 inline Array StochasticProcess1D::drift(Time t, const Array& x) const {
255 #if defined(QL_EXTRA_SAFETY_CHECKS)
256 QL_REQUIRE(x.size() == 1, "1-D array required");
257 #endif
258 Array a(1, drift(t, x[0]));
259 return a;
260 }
261
263 #if defined(QL_EXTRA_SAFETY_CHECKS)
264 QL_REQUIRE(x.size() == 1, "1-D array required");
265 #endif
266 Matrix m(1, 1, diffusion(t, x[0]));
267 return m;
268 }
269
271 Time t0, const Array& x0, Time dt) const {
272 #if defined(QL_EXTRA_SAFETY_CHECKS)
273 QL_REQUIRE(x0.size() == 1, "1-D array required");
274 #endif
275 Array a(1, expectation(t0, x0[0], dt));
276 return a;
277 }
278
280 Time t0, const Array& x0, Time dt) const {
281 #if defined(QL_EXTRA_SAFETY_CHECKS)
282 QL_REQUIRE(x0.size() == 1, "1-D array required");
283 #endif
284 Matrix m(1, 1, stdDeviation(t0, x0[0], dt));
285 return m;
286 }
287
289 Time t0, const Array& x0, Time dt) const {
290 #if defined(QL_EXTRA_SAFETY_CHECKS)
291 QL_REQUIRE(x0.size() == 1, "1-D array required");
292 #endif
293 Matrix m(1, 1, variance(t0, x0[0], dt));
294 return m;
295 }
296
298 Time dt, const Array& dw) const {
299 #if defined(QL_EXTRA_SAFETY_CHECKS)
300 QL_REQUIRE(x0.size() == 1, "1-D array required");
301 QL_REQUIRE(dw.size() == 1, "1-D array required");
302 #endif
303 Array a(1, evolve(t0,x0[0],dt,dw[0]));
304 return a;
305 }
306
308 const Array& dx) const {
309 #if defined(QL_EXTRA_SAFETY_CHECKS)
310 QL_REQUIRE(x0.size() == 1, "1-D array required");
311 QL_REQUIRE(dx.size() == 1, "1-D array required");
312 #endif
313 Array a(1, apply(x0[0],dx[0]));
314 return a;
315 }
316
317}
318
319
320#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Concrete date class.
Definition: date.hpp:125
Matrix used in linear algebra.
Definition: matrix.hpp:41
Object that notifies its changes to a set of observers.
Definition: observable.hpp:62
Object that gets notified when a given observable changes.
Definition: observable.hpp:116
discretization of a 1-D stochastic process
virtual Real diffusion(const StochasticProcess1D &, Time t0, Real x0, Time dt) const =0
virtual Real variance(const StochasticProcess1D &, Time t0, Real x0, Time dt) const =0
virtual Real drift(const StochasticProcess1D &, Time t0, Real x0, Time dt) const =0
1-dimensional stochastic process
virtual Real apply(Real x0, Real dx) const
Size size() const override
returns the number of dimensions of the stochastic process
ext::shared_ptr< discretization > discretization_
virtual Real drift(Time t, Real x) const =0
returns the drift part of the equation, i.e.
Matrix covariance(Time t0, const Array &x0, Time dt) const override
virtual Real evolve(Time t0, Real x0, Time dt, Real dw) const
Array initialValues() const override
returns the initial values of the state variables
virtual Real x0() const =0
returns the initial value of the state variable
virtual Real stdDeviation(Time t0, Real x0, Time dt) const
virtual Real expectation(Time t0, Real x0, Time dt) const
virtual Real diffusion(Time t, Real x) const =0
returns the diffusion part of the equation, i.e.
discretization of a stochastic process over a given time interval
virtual Matrix covariance(const StochasticProcess &, Time t0, const Array &x0, Time dt) const =0
virtual Matrix diffusion(const StochasticProcess &, Time t0, const Array &x0, Time dt) const =0
virtual Array drift(const StochasticProcess &, Time t0, const Array &x0, Time dt) const =0
multi-dimensional stochastic process class.
virtual Size size() const =0
returns the number of dimensions of the stochastic process
~StochasticProcess() override=default
virtual Matrix stdDeviation(Time t0, const Array &x0, Time dt) const
virtual Array drift(Time t, const Array &x) const =0
returns the drift part of the equation, i.e.,
virtual Array evolve(Time t0, const Array &x0, Time dt, const Array &dw) const
virtual Matrix covariance(Time t0, const Array &x0, Time dt) const
ext::shared_ptr< discretization > discretization_
virtual Array apply(const Array &x0, const Array &dx) const
virtual Array expectation(Time t0, const Array &x0, Time dt) const
virtual Size factors() const
returns the number of independent factors of the process
virtual Matrix diffusion(Time t, const Array &x) const =0
returns the diffusion part of the equation, i.e.
virtual Time time(const Date &) const
virtual Array initialValues() const =0
returns the initial values of the state variables
date- and time-related classes, typedefs and enumerations
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
LinearInterpolation variance
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
matrix used in linear algebra.
Definition: any.hpp:35
observer/observable pattern