| 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 | #include <ql/stochasticprocess.hpp> |
| 23 | #include <utility> |
| 24 | |
| 25 | namespace QuantLib { |
| 26 | |
| 27 | // base class |
| 28 | |
| 29 | StochasticProcess::StochasticProcess(ext::shared_ptr<discretization> disc) |
| 30 | : discretization_(std::move(disc)) {} |
| 31 | |
| 32 | Size StochasticProcess::factors() const { |
| 33 | return size(); |
| 34 | } |
| 35 | |
| 36 | Array StochasticProcess::expectation(Time t0, |
| 37 | const Array& x0, |
| 38 | Time dt) const { |
| 39 | return apply(x0, dx: discretization_->drift(*this, t0, x0, dt)); |
| 40 | } |
| 41 | |
| 42 | Matrix StochasticProcess::stdDeviation(Time t0, |
| 43 | const Array& x0, |
| 44 | Time dt) const { |
| 45 | return discretization_->diffusion(*this, t0, x0, dt); |
| 46 | } |
| 47 | |
| 48 | Matrix StochasticProcess::covariance(Time t0, |
| 49 | const Array& x0, |
| 50 | Time dt) const { |
| 51 | return discretization_->covariance(*this, t0, x0, dt); |
| 52 | } |
| 53 | |
| 54 | Array StochasticProcess::evolve(Time t0, const Array& x0, |
| 55 | Time dt, const Array& dw) const { |
| 56 | return apply(x0: expectation(t0,x0,dt), dx: stdDeviation(t0,x0,dt)*dw); |
| 57 | } |
| 58 | |
| 59 | Array StochasticProcess::apply(const Array& x0, |
| 60 | const Array& dx) const { |
| 61 | return x0 + dx; |
| 62 | } |
| 63 | |
| 64 | Time StochasticProcess::time(const Date& ) const { |
| 65 | QL_FAIL("date/time conversion not supported" ); |
| 66 | } |
| 67 | |
| 68 | void StochasticProcess::update() { |
| 69 | notifyObservers(); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | // 1-D specialization |
| 74 | |
| 75 | StochasticProcess1D::StochasticProcess1D(ext::shared_ptr<discretization> disc) |
| 76 | : discretization_(std::move(disc)) {} |
| 77 | |
| 78 | Real StochasticProcess1D::expectation(Time t0, Real x0, Time dt) const { |
| 79 | return apply(x0, dx: discretization_->drift(*this, t0, x0, dt)); |
| 80 | } |
| 81 | |
| 82 | Real StochasticProcess1D::stdDeviation(Time t0, Real x0, Time dt) const { |
| 83 | return discretization_->diffusion(*this, t0, x0, dt); |
| 84 | } |
| 85 | |
| 86 | Real StochasticProcess1D::variance(Time t0, Real x0, Time dt) const { |
| 87 | return discretization_->variance(*this, t0, x0, dt); |
| 88 | } |
| 89 | |
| 90 | Real StochasticProcess1D::evolve(Time t0, Real x0, |
| 91 | Time dt, Real dw) const { |
| 92 | return apply(x0: expectation(t0,x0,dt), dx: stdDeviation(t0,x0,dt)*dw); |
| 93 | } |
| 94 | |
| 95 | Real StochasticProcess1D::apply(Real x0, Real dx) const { |
| 96 | return x0 + dx; |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |