/
bridges.h
312 lines (230 loc) · 7.33 KB
/
bridges.h
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// Dale Roberts <dale.o.roberts@gmail.com>
//
#ifndef volmodels_bridges_h
#define volmodels_bridges_h
#include "utils.h"
using namespace std;
vector<double> stockPricePath(const vector<double>& times, const vector<double>& drifts, const vector<double>& vols, const vector<double>& qs)
{
int tdim = times.size();
vector<double> s(tdim, 0);
s[0] = exp(drifts[0]);
for (int i = 1; i < tdim; ++i) {
s[i] = exp(drifts[i] + sqrt(vols[i])*qnorm(qs[i-1]));
#ifdef DEBUG
qs.at(i-1);
#endif
}
return s;
}
vector<double> stockPriceBridge(const vector<double>& times, const vector<double>& drifts, const vector<double>& vols, const vector<double>& qs)
{
int tdim = times.size()-1;
int m = log2(tdim);
vector<double> s(times.size(), 0);
vector<double> Z(tdim, 0);
for (int i = 0; i < Z.size(); ++i) {
Z[i] = qnorm(qs[i]);
#ifdef DEBUG
qs.at(i);
#endif
}
int h = 1 << m; // 2^m
int jmax = 1;
s[h] = exp(drifts[h] + sqrt(vols[h])*Z[0]);
s[0] = exp(drifts[0]);
int qindex = 1;
for (int k = 1; k <= m; ++k) {
int l = 0, r = h, imin = h/2, i = imin;
double a, b;
for (int j = 1; j <= jmax; ++j) {
a = drifts[i]-drifts[l]+log(s[l])+(vols[i]-vols[l])/(vols[r]-vols[l])*(log(s[r])-log(s[l])+drifts[l]-drifts[r]);
b = vols[i]-vols[l]-((vols[i]-vols[l])*(vols[i]-vols[l]))/(vols[r]-vols[l]);
s[i] = exp(a + sqrt(b) * Z[qindex]);
i = i + h;
l = l + h;
r = r + h;
qindex++;
}
jmax = 2 * jmax;
h = imin;
}
return s;
}
vector<double> squareRootPath(const vector<double>& times, double kappa, double theta, double sigma, double v0, const vector<double>& qs)
{
int m = log2(times.size()-1);
int h = 1 << m; // 2^m
// the modified dates
vector<double> s(h+1, 0.0);
for (int i = 1; i < h+1; ++i) {
s[i] = sigma*sigma/(4*kappa) * (exp(kappa*times[i])-1);
}
// the dimension of the square-root process
double delta = 4*kappa*theta/(sigma*sigma);
// contains the squared Bessel bridge, which is finally modified to yield the square-root process
vector<double> x(h+1, v0);
for (int i = 1; i < h+1; ++i) {
x[i] = (s[i]-s[i-1])*qchisq(qs[i-1],delta,x[i-1]/(s[i]-s[i-1]));
#ifdef DEBUG
qs.at(i-1);
#endif
}
vector<double> v(times.size(), v0);
for (int i = 1; i < times.size(); ++i) {
v[i] = x[i] * exp(-kappa * times[i]);
}
return v;
}
vector<double> squareRootBridge(const vector<double>& times, double kappa, double theta, double sigma, double v0, const vector<double>& qs)
{
int m = log2(times.size()-1);
int h = 1 << m; // 2^m
// the modified dates
vector<double> s(h+1, 0.0);
for (int i = 1; i < h+1; ++i) {
s[i] = sigma*sigma/(4*kappa) * (exp(kappa*times[i])-1);
}
// the dimension of the square-root process
double delta = 4*kappa*theta/(sigma*sigma);
int jmax = 1;
int iq = 0; // index of quantiles qs
// contains the squared Bessel bridge, which is finally modified to yield the square-root process
vector<double> x(h+1, 0);
x[0] = v0;
x[h] = s[h] * qchisq(qs[iq], delta, x[0]/s[h]);
#ifdef DEBUG
qs.at(iq);
#endif
iq++;
for (int k = 1; k <= m; ++k) {
int imin = h/2, i = imin, l = 0, r = h;
for (int j = 1; j <= jmax; ++j) {
double Z, P, lambda, a, b, G;
lambda=(1/(2*(s[r]-s[l])))*( (s[r]-s[i])*x[l]/(s[i]-s[l]) +(s[i]-s[l])*x[r]/(s[r]-s[i]));
P = qpois(qs[iq], lambda);
// vector<double> vals = {qs[iq],lambda};
// PIO(vals);
// PO(P);
#ifdef DEBUG
qs.at(iq);
#endif
iq++;
// PO(iq);
Z = qbessel(qs[iq], delta/2-1, sqrt(x[l]*x[r])/(s[r]-s[l]));
// PO(Z);
#ifdef DEBUG
qs.at(iq);
#endif
iq++;
a = P + 2*Z + delta/2;
b = (s[r]-s[l])/(2*(s[i]-s[l])*(s[r]-s[i]));
G = qgamma(qs[iq], a, 1/b);
#ifdef DEBUG
qs.at(iq);
#endif
iq++;
// PO(a);
// PO(b);
// PO(G);
x[i] = G;
// PIO(x);
i = i+h;
l = l+h;
r = r+h;
}
jmax = jmax*2;
h = imin;
}
vector<double> v(times.size(), v0);
for (int i = 1; i < times.size(); ++i) {
v[i] = x[i] * exp(-kappa * times[i]);
}
return v;
}
vector<double> poissonPathBridge(const vector<double>& times, double lambda, const vector<double>& qs)
{
int tdim = times.size()-1;
int m = log2(tdim);
int h = 1 << m; // 2^m
vector<double> N(tdim+1, 0.0);
N[h] = qpois(qs[0], lambda*times[tdim]);
#ifdef DEBUG
qs.at(0);
#endif
int qindex = 1;
int jmax = 1;
for (int k = 1; k <= m; ++k)
{
int l = 0, r = h, imin = h/2, i = imin;
int a;
double b, B;
for (int j = 1; j <= jmax; ++j)
{
a = N[r] - N[l];
b = (times[i]-times[l])/(times[r]-times[l]);
B = qbinom(qs[qindex],a,b);
N[i] = N[l] + B;
#ifdef DEBUG
qs.at(qindex);
#endif
i = i + h;
l = l + h;
r = r + h;
qindex++;
}
jmax = 2 * jmax;
h = imin;
}
return N;
}
vector<double> sumPoissonPathBridge(const vector<double>& poissonPath, double mu_s, double sigma, const vector<double> qs)
{
int tdim = poissonPath.size();
vector<double> mus(tdim, 0.0);
vector<double> sigmasqs(tdim, 0.0);
for (int i = 1; i < tdim; ++i)
{
mus[i] = poissonPath[i] * mu_s;
sigmasqs[i] = poissonPath[i] * sigma * sigma;
}
int m = log2(tdim-1);
int h = 1 << m; // 2^m
vector<double> S(tdim, 0.0);
S[h] = mus[h] + sqrt(sigmasqs[h]) * qnorm(qs[0]);
#ifdef DEBUG
qs.at(0);
#endif
int qindex = 1;
int jmax = 1;
for (int k = 1; k <= m; ++k)
{
int l = 0, r = h, imin = h/2, i = imin;
double a, b;
for (int j = 1; j <= jmax; ++j)
{
if (poissonPath[l] == poissonPath[r])
{
S[i] = S[l];
}
else
{
a = S[l]+mus[i]-mus[l] + (sigmasqs[i]-sigmasqs[l])*(S[r]-S[l]+mus[l]-mus[r])/(sigmasqs[r]-sigmasqs[l]);
b = sigmasqs[i]-sigmasqs[l]-((sigmasqs[i]-sigmasqs[l])*(sigmasqs[i]-sigmasqs[l]))/(sigmasqs[r]-sigmasqs[l]);
S[i] = a+sqrt(b)*qnorm(qs[qindex]);
#ifdef DEBUG
qs.at(qindex);
#endif
}
i = i + h;
l = l + h;
r = r + h;
qindex++;
}
jmax = 2 * jmax;
h = imin;
}
return S;
}
#endif