QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
extendedornsteinuhlenbeckprocess.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2010 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/math/integrals/gausslobattointegral.hpp>
22#include <ql/processes/ornsteinuhlenbeckprocess.hpp>
23#include <utility>
24
25namespace QuantLib {
26
27 namespace {
28
29 class integrand {
30 ext::function<Real (Real)> b;
31 Real speed;
32 public:
33 integrand(ext::function<Real(Real)> b, Real speed) : b(std::move(b)), speed(speed) {}
34 Real operator()(Real x) const {
35 return b(x) * std::exp(speed*x);
36 }
37 };
38
39 }
40
42 Real speed,
43 Volatility vol,
44 Real x0,
45 ext::function<Real(Real)> b,
47 Real intEps)
48 : speed_(speed), vol_(vol), b_(std::move(b)), intEps_(intEps),
49 ouProcess_(new OrnsteinUhlenbeckProcess(speed, vol, x0)), discretization_(discretization) {
50 QL_REQUIRE(speed_ >= 0.0, "negative a given");
51 QL_REQUIRE(vol_ >= 0.0, "negative volatility given");
52 }
53
55 return ouProcess_->x0();
56 }
57
59 return ouProcess_->drift(t, x) + speed_*b_(t);
60 }
61
63 return ouProcess_->diffusion(t, x);
64 }
65
67 Time t0, Real x0, Time dt) const{
68 return ouProcess_->stdDeviation(t0, x0, dt);
69 }
70
72 Time t0, Real x0, Time dt) const{
73 return ouProcess_->variance(t0, x0, dt);
74 }
75
77 return speed_;
78 }
79
81 return vol_;
82 }
83
85 Time t0, Real x0, Time dt) const {
86 switch (discretization_) {
87 case MidPoint:
88 return ouProcess_->expectation(t0, x0, dt)
89 + b_(t0+0.5*dt)*(1.0 - std::exp(-speed_*dt));
90 break;
91 case Trapezodial:
92 {
93 const Time t = t0+dt;
94 const Time u = t0;
95 const Real bt = b_(t);
96 const Real bu = b_(u);
97 const Real ex = std::exp(-speed_*dt);
98
99 return ouProcess_->expectation(t0, x0, dt)
100 + bt-ex*bu - (bt-bu)/(speed_*dt)*(1-ex);
101 }
102 break;
103 case GaussLobatto:
104 return ouProcess_->expectation(t0, x0, dt)
105 + speed_*std::exp(-speed_*(t0+dt))
106 * GaussLobattoIntegral(100000, intEps_)(integrand(b_, speed_),
107 t0, t0+dt);
108 break;
109 default:
110 QL_FAIL("unknown discretization scheme");
111 }
112 }
113}
114
ExtendedOrnsteinUhlenbeckProcess(Real speed, Volatility sigma, Real x0, ext::function< Real(Real)> b, Discretization discretization=MidPoint, Real intEps=1e-4)
Real diffusion(Time t, Real x) const override
returns the diffusion part of the equation, i.e.
Real stdDeviation(Time t0, Real x0, Time dt) const override
const ext::shared_ptr< OrnsteinUhlenbeckProcess > ouProcess_
Real drift(Time t, Real x) const override
returns the drift part of the equation, i.e.
Real expectation(Time t0, Real x0, Time dt) const override
Real x0() const override
returns the initial value of the state variable
Real variance(Time t0, Real x0, Time dt) const override
Integral of a one-dimensional function.
Ornstein-Uhlenbeck process class.
discretization of a stochastic process over a given time interval
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
Definition: any.hpp:35
STL namespace.