forked from lballabio/QuantLib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathg2process.cpp
236 lines (198 loc) · 7.74 KB
/
g2process.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2006 Banca Profilo S.p.A.
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ql/processes/g2process.hpp>
#include <ql/processes/eulerdiscretization.hpp>
namespace QuantLib {
G2Process::G2Process(Real a, Real sigma, Real b, Real eta, Real rho)
: x0_(0.0), y0_(0.0), a_(a), sigma_(sigma), b_(b), eta_(eta), rho_(rho),
xProcess_(new QuantLib::OrnsteinUhlenbeckProcess(a, sigma, 0.0)),
yProcess_(new QuantLib::OrnsteinUhlenbeckProcess(b, eta, 0.0)) {}
Size G2Process::size() const {
return 2;
}
Disposable<Array> G2Process::initialValues() const {
Array tmp(2);
tmp[0] = x0_;
tmp[1] = y0_;
return tmp;
}
Disposable<Array> G2Process::drift(Time t, const Array& x) const {
Array tmp(2);
tmp[0] = xProcess_->drift(t, x[0]);
tmp[1] = yProcess_->drift(t, x[1]);
return tmp;
}
Disposable<Matrix> G2Process::diffusion(Time, const Array&) const {
/* the correlation matrix is
| 1 rho |
| rho 1 |
whose square root (which is used here) is
| 1 0 |
| rho sqrt(1-rho^2) |
*/
Matrix tmp(2,2);
Real sigma1 = sigma_;
Real sigma2 = eta_;
tmp[0][0] = sigma1; tmp[0][1] = 0.0;
tmp[1][0] = rho_*sigma1; tmp[1][1] = std::sqrt(1.0-rho_*rho_)*sigma2;
return tmp;
}
Disposable<Array> G2Process::expectation(Time t0, const Array& x0,
Time dt) const {
Array tmp(2);
tmp[0] = xProcess_->expectation(t0, x0[0], dt);
tmp[1] = yProcess_->expectation(t0, x0[1], dt);
return tmp;
}
Disposable<Matrix> G2Process::stdDeviation(Time t0, const Array& x0,
Time dt) const {
/* the correlation matrix is
| 1 rho |
| rho 1 |
whose square root (which is used here) is
| 1 0 |
| rho sqrt(1-rho^2) |
*/
Matrix tmp(2,2);
Real sigma1 = xProcess_->stdDeviation(t0, x0[0], dt);
Real sigma2 = yProcess_->stdDeviation(t0, x0[1], dt);
Real expa = std::exp(-a_*dt), expb = std::exp(-b_*dt);
Real H = (rho_*sigma_*eta_)/(a_+b_)*(1-expa*expb);
Real den =
(0.5*sigma_*eta_)*std::sqrt((1-expa*expa)*(1-expb*expb)/(a_*b_));
Real newRho = H/den;
tmp[0][0] = sigma1;
tmp[0][1] = 0.0;
tmp[1][0] = newRho*sigma2;
tmp[1][1] = std::sqrt(1.0-newRho*newRho)*sigma2;
return tmp;
}
Disposable<Matrix> G2Process::covariance(Time t0, const Array& x0,
Time dt) const {
Matrix sigma = stdDeviation(t0, x0, dt);
Matrix result = sigma*transpose(sigma);
return result;
}
Real G2Process::x0() const {
return x0_;
}
Real G2Process::y0() const {
return y0_;
}
Real G2Process::a() const {
return a_;
}
Real G2Process::sigma() const {
return sigma_;
}
Real G2Process::b() const {
return b_;
}
Real G2Process::eta() const {
return eta_;
}
Real G2Process::rho() const {
return rho_;
}
G2ForwardProcess::G2ForwardProcess(Real a, Real sigma, Real b,
Real eta, Real rho)
: x0_(0.0), y0_(0.0), a_(a), sigma_(sigma), b_(b), eta_(eta), rho_(rho),
xProcess_(new QuantLib::OrnsteinUhlenbeckProcess(a, sigma, 0.0)),
yProcess_(new QuantLib::OrnsteinUhlenbeckProcess(b, eta, 0.0)) {}
Size G2ForwardProcess::size() const {
return 2;
}
Disposable<Array> G2ForwardProcess::initialValues() const {
Array tmp(2);
tmp[0] = x0_;
tmp[1] = y0_;
return tmp;
}
Disposable<Array> G2ForwardProcess::drift(Time t, const Array& x) const {
Array tmp(2);
tmp[0] = xProcess_->drift(t, x[0]) + xForwardDrift(t, T_);
tmp[1] = yProcess_->drift(t, x[1]) + yForwardDrift(t, T_);
return tmp;
}
Disposable<Matrix> G2ForwardProcess::diffusion(Time, const Array&) const {
Matrix tmp(2,2);
Real sigma1 = sigma_;
Real sigma2 = eta_;
tmp[0][0] = sigma1; tmp[0][1] = 0.0;
tmp[1][0] = rho_*sigma1; tmp[1][1] = std::sqrt(1.0-rho_*rho_)*sigma2;
return tmp;
}
Disposable<Array> G2ForwardProcess::expectation(Time t0, const Array& x0,
Time dt) const {
Array tmp(2);
tmp[0] = xProcess_->expectation(t0, x0[0], dt) - Mx_T(t0, t0+dt, T_);
tmp[1] = yProcess_->expectation(t0, x0[1], dt) - My_T(t0, t0+dt, T_);
return tmp;
}
Disposable<Matrix> G2ForwardProcess::stdDeviation(Time t0, const Array& x0,
Time dt) const {
Matrix tmp(2,2);
Real sigma1 = xProcess_->stdDeviation(t0, x0[0], dt);
Real sigma2 = yProcess_->stdDeviation(t0, x0[1], dt);
Real expa = std::exp(-a_*dt), expb = std::exp(-b_*dt);
Real H = (rho_*sigma_*eta_)/(a_+b_)*(1-expa*expb);
Real den =
(0.5*sigma_*eta_)*std::sqrt((1-expa*expa)*(1-expb*expb)/(a_*b_));
Real newRho = H/den;
tmp[0][0] = sigma1;
tmp[0][1] = 0.0;
tmp[1][0] = newRho*sigma2;
tmp[1][1] = std::sqrt(1.0-newRho*newRho)*sigma2;
return tmp;
}
Disposable<Matrix> G2ForwardProcess::covariance(Time t0, const Array& x0,
Time dt) const {
Matrix sigma = stdDeviation(t0, x0, dt);
Matrix result = sigma*transpose(sigma);
return result;
}
Real G2ForwardProcess::xForwardDrift(Time t, Time T) const {
Real expatT = std::exp(-a_*(T-t));
Real expbtT = std::exp(-b_*(T-t));
return -(sigma_*sigma_/a_) * (1-expatT)
- (rho_*sigma_*eta_/b_) * (1-expbtT);
}
Real G2ForwardProcess::yForwardDrift(Time t, Time T) const {
Real expatT = std::exp(-a_*(T-t));
Real expbtT = std::exp(-b_*(T-t));
return -(eta_*eta_/b_) * (1-expbtT)
- (rho_*sigma_*eta_/a_) * (1-expatT);
}
Real G2ForwardProcess::Mx_T(Real s, Real t, Real T) const {
Real M;
M = ( (sigma_*sigma_)/(a_*a_) + (rho_*sigma_*eta_)/(a_*b_) )
* (1-std::exp(-a_*(t-s)));
M += -(sigma_*sigma_)/(2*a_*a_) *
(std::exp(-a_*(T-t))-std::exp(-a_*(T+t-2*s)));
M += -(rho_*sigma_*eta_)/(b_*(a_+b_))
* (std::exp(-b_*(T-t)) -std::exp(-b_*T-a_*t+(a_+b_)*s));
return M;
}
Real G2ForwardProcess::My_T(Real s, Real t, Real T) const {
Real M;
M = ( (eta_*eta_)/(b_*b_) + (rho_*sigma_*eta_)/(a_*b_) )
* (1-std::exp(-b_*(t-s)));
M += -(eta_*eta_)/(2*b_*b_) *
(std::exp(-b_*(T-t))-std::exp(-b_*(T+t-2*s)));
M += -(rho_*sigma_*eta_)/(a_*(a_+b_))
* (std::exp(-a_*(T-t))-std::exp(-a_*T-b_*t+(a_+b_)*s));
return M;
}
}