| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2011 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 | #include <ql/experimental/processes/extendedornsteinuhlenbeckprocess.hpp> |
| 21 | #include <ql/experimental/processes/extouwithjumpsprocess.hpp> |
| 22 | #include <ql/experimental/processes/klugeextouprocess.hpp> |
| 23 | #include <ql/termstructures/yieldtermstructure.hpp> |
| 24 | #include <utility> |
| 25 | |
| 26 | namespace QuantLib { |
| 27 | |
| 28 | KlugeExtOUProcess::KlugeExtOUProcess( |
| 29 | Real rho, |
| 30 | ext::shared_ptr<ExtOUWithJumpsProcess> klugeProcess, |
| 31 | ext::shared_ptr<ExtendedOrnsteinUhlenbeckProcess> ouProcess) |
| 32 | : rho_(rho), sqrtMRho_(std::sqrt(x: 1 - rho * rho)), klugeProcess_(std::move(klugeProcess)), |
| 33 | ouProcess_(std::move(ouProcess)) { |
| 34 | QL_REQUIRE(klugeProcess_, "null Kluge process" ); |
| 35 | QL_REQUIRE(ouProcess_, "null Ornstein-Uhlenbeck process" ); |
| 36 | } |
| 37 | |
| 38 | Size KlugeExtOUProcess::size() const { |
| 39 | return klugeProcess_->size() + 1; |
| 40 | } |
| 41 | |
| 42 | Size KlugeExtOUProcess::factors() const { |
| 43 | return klugeProcess_->factors() + 1; |
| 44 | } |
| 45 | |
| 46 | Array KlugeExtOUProcess::initialValues() const { |
| 47 | Array retVal(size()); |
| 48 | const Array x0 = klugeProcess_->initialValues(); |
| 49 | std::copy(first: x0.begin(), last: x0.end(), result: retVal.begin()); |
| 50 | retVal.back() = ouProcess_->x0(); |
| 51 | |
| 52 | return retVal; |
| 53 | } |
| 54 | |
| 55 | Array KlugeExtOUProcess::drift(Time t, const Array& x) const { |
| 56 | Array retVal(size()); |
| 57 | Array mu = klugeProcess_->drift(t, x); |
| 58 | std::copy(first: mu.begin(), last: mu.end(), result: retVal.begin()); |
| 59 | retVal.back() = ouProcess_->drift(t, x: x.back()); |
| 60 | |
| 61 | return retVal; |
| 62 | } |
| 63 | |
| 64 | Matrix KlugeExtOUProcess::diffusion(Time t, const Array& x) const{ |
| 65 | Matrix retVal(size(), factors(), 0.0); |
| 66 | |
| 67 | Volatility vol = ouProcess_->diffusion(t, x: x.back()); |
| 68 | |
| 69 | retVal[0][0] = klugeProcess_->diffusion(t, x)[0][0]; |
| 70 | retVal[size()][0] = rho_*vol; |
| 71 | retVal[size()][factors()] = sqrtMRho_*vol; |
| 72 | |
| 73 | return retVal; |
| 74 | } |
| 75 | |
| 76 | Array KlugeExtOUProcess::evolve(Time t0, const Array& x0, |
| 77 | Time dt, const Array& dw) const{ |
| 78 | Array retVal(size()); |
| 79 | |
| 80 | Array ev = klugeProcess_->evolve(t0, x0, dt, dw); |
| 81 | std::copy(first: ev.begin(), last: ev.end(), result: retVal.begin()); |
| 82 | |
| 83 | const Real dz = dw.back()*sqrtMRho_ + dw.front()*rho_; |
| 84 | retVal.back() = ouProcess_->evolve(t0, x0: x0.back(), dt, dw: dz); |
| 85 | |
| 86 | return retVal; |
| 87 | } |
| 88 | |
| 89 | ext::shared_ptr<ExtOUWithJumpsProcess> |
| 90 | KlugeExtOUProcess::getKlugeProcess() const { |
| 91 | return klugeProcess_; |
| 92 | } |
| 93 | ext::shared_ptr<ExtendedOrnsteinUhlenbeckProcess> |
| 94 | KlugeExtOUProcess::getExtOUProcess() const { |
| 95 | return ouProcess_; |
| 96 | } |
| 97 | |
| 98 | Real KlugeExtOUProcess::rho() const { |
| 99 | return rho_; |
| 100 | } |
| 101 | |
| 102 | } |
| 103 | |
| 104 | |