| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2007, 2008 Klaus Spanderen |
| 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 jointstochasticprocess.hpp |
| 21 | \brief multi model process for hybrid products |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_joint_stochastic_process_hpp |
| 25 | #define quantlib_joint_stochastic_process_hpp |
| 26 | |
| 27 | #include <ql/utilities/null.hpp> |
| 28 | #include <ql/stochasticprocess.hpp> |
| 29 | #include <vector> |
| 30 | #include <map> |
| 31 | |
| 32 | namespace QuantLib { |
| 33 | |
| 34 | class JointStochasticProcess : public StochasticProcess { |
| 35 | public: |
| 36 | JointStochasticProcess(std::vector<ext::shared_ptr<StochasticProcess> > l, |
| 37 | Size factors = Null<Size>()); |
| 38 | |
| 39 | Size size() const override; |
| 40 | Size factors() const override; |
| 41 | |
| 42 | Array initialValues() const override; |
| 43 | Array drift(Time t, const Array& x) const override; |
| 44 | Array expectation(Time t0, const Array& x0, Time dt) const override; |
| 45 | |
| 46 | Matrix diffusion(Time t, const Array& x) const override; |
| 47 | Matrix covariance(Time t0, const Array& x0, Time dt) const override; |
| 48 | Matrix stdDeviation(Time t0, const Array& x0, Time dt) const override; |
| 49 | |
| 50 | Array apply(const Array& x0, const Array& dx) const override; |
| 51 | Array evolve(Time t0, const Array& x0, Time dt, const Array& dw) const override; |
| 52 | |
| 53 | virtual void preEvolve(Time t0, const Array& x0, |
| 54 | Time dt, const Array& dw) const = 0; |
| 55 | virtual Array postEvolve(Time t0, const Array& x0, |
| 56 | Time dt, const Array& dw, |
| 57 | const Array& y0) const = 0; |
| 58 | |
| 59 | virtual DiscountFactor numeraire(Time t, const Array& x) const = 0; |
| 60 | virtual bool correlationIsStateDependent() const = 0; |
| 61 | virtual Matrix crossModelCorrelation(Time t0, const Array& x0) const = 0; |
| 62 | |
| 63 | const std::vector<ext::shared_ptr<StochasticProcess> > & |
| 64 | constituents() const; |
| 65 | |
| 66 | void update() override; |
| 67 | Time time(const Date& date) const override; |
| 68 | |
| 69 | protected: |
| 70 | std::vector<ext::shared_ptr<StochasticProcess> > l_; |
| 71 | Array slice(const Array& x, Size i) const; |
| 72 | |
| 73 | private: |
| 74 | typedef |
| 75 | std::vector<ext::shared_ptr<StochasticProcess> >::const_iterator |
| 76 | const_iterator; |
| 77 | |
| 78 | typedef std::vector<ext::shared_ptr<StochasticProcess> >::iterator |
| 79 | iterator; |
| 80 | |
| 81 | Size size_ = 0, factors_, modelFactors_ = 0; |
| 82 | std::vector<Size> vsize_, vfactors_; |
| 83 | |
| 84 | struct CachingKey { |
| 85 | CachingKey(const Time t0, const Time dt) |
| 86 | : t0_(t0), dt_(dt) {} |
| 87 | |
| 88 | bool operator<(const CachingKey& key) const { |
| 89 | return t0_ < key.t0_ |
| 90 | || ( t0_ == key.t0_ && dt_ < key.dt_); |
| 91 | } |
| 92 | Time t0_; |
| 93 | Time dt_; |
| 94 | }; |
| 95 | |
| 96 | mutable std::map<CachingKey, Matrix> correlationCache_; |
| 97 | }; |
| 98 | |
| 99 | } |
| 100 | |
| 101 | |
| 102 | #endif |
| 103 | |