forked from lballabio/QuantLib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgsrprocesscore.cpp
367 lines (321 loc) · 13 KB
/
gsrprocesscore.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2015 Peter Caspers
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/gsrprocesscore.hpp>
#include <cmath>
using std::exp;
using std::pow;
namespace QuantLib {
namespace detail {
GsrProcessCore::GsrProcessCore(const Array ×, const Array &vols,
const Array &reversions, const Real T)
: times_(times), vols_(vols), reversions_(reversions),
T_(T), revZero_(reversions.size(), false) {
QL_REQUIRE(times.size() == vols.size() - 1,
"number of volatilities ("
<< vols.size() << ") compared to number of times ("
<< times_.size() << " must be bigger by one");
QL_REQUIRE(times.size() == reversions.size() - 1 || reversions.size() == 1,
"number of reversions ("
<< vols.size() << ") compared to number of times ("
<< times_.size() << " must be bigger by one, or exactly "
"1 reversion must be given");
for (int i = 0; i < ((int)times.size()) - 1; i++)
QL_REQUIRE(times[i] < times[i + 1], "times must be increasing ("
<< times[i] << "@" << i << " , "
<< times[i + 1] << "@" << i + 1
<< ")");
flushCache();
}
void GsrProcessCore::flushCache() const {
for (int i = 0; i < (int)reversions_.size(); i++)
// small reversions cause numerical problems, so we keep them
// away from zero
if (std::fabs(reversions_[i]) < 1E-4)
revZero_[i] = true;
else
revZero_[i] = false;
cache1_.clear();
cache2a_.clear();
cache2b_.clear();
cache3_.clear();
cache4_.clear();
cache5_.clear();
}
Real GsrProcessCore::expectation_x0dep_part(const Time w, const Real xw,
const Time dt) const {
Real t = w + dt;
std::pair<Real, Real> key;
key = std::make_pair(w, t);
std::map<std::pair<Real, Real>, Real>::const_iterator k = cache1_.find(key);
if (k != cache1_.end())
return xw * (k->second);
// A(w,t)x(w)
Real res2 = 1.0;
for (int i = lowerIndex(w); i <= upperIndex(t) - 1; i++) {
res2 *= exp(-rev(i) * (cappedTime(i + 1, t) - flooredTime(i, w)));
}
cache1_.insert(std::make_pair(key, res2));
return res2 * xw;
}
Real GsrProcessCore::expectation_rn_part(const Time w,
const Time dt) const {
Real t = w + dt;
std::pair<Real, Real> key;
key = std::make_pair(w, t);
std::map<std::pair<Real, Real>, Real>::const_iterator k =
cache2a_.find(key);
if (k != cache2a_.end())
return k->second;
Real res = 0.0;
// \int A(s,t)y(s)
for (int k = lowerIndex(w); k <= upperIndex(t) - 1; k++) {
// l<k
for (int l = 0; l <= k - 1; l++) {
Real res2 = 1.0;
// alpha_l
res2 *= revZero(l) ? vol(l) * vol(l) * (time2(l + 1) - time2(l))
: vol(l) * vol(l) / (2.0 * rev(l)) *
(1.0 - exp(-2.0 * rev(l) *
(time2(l + 1) - time2(l))));
// zeta_i (i>k)
for (int i = k + 1; i <= upperIndex(t) - 1; i++)
res2 *= exp(-rev(i) * (cappedTime(i + 1, t) - time2(i)));
// beta_j (j<k)
for (int j = l + 1; j <= k - 1; j++)
res2 *= exp(-2.0 * rev(j) * (time2(j + 1) - time2(j)));
// zeta_k beta_k
res2 *=
revZero(k)
? 2.0 * time2(k) - flooredTime(k, w) -
cappedTime(k + 1, t) -
2.0 * (time2(k) - cappedTime(k + 1, t))
: (exp(rev(k) * (2.0 * time2(k) - flooredTime(k, w) -
cappedTime(k + 1, t))) -
exp(2.0 * rev(k) * (time2(k) - cappedTime(k + 1, t)))) /
rev(k);
// add to sum
res += res2;
}
// l=k
Real res2 = 1.0;
// alpha_k zeta_k
res2 *=
revZero(k)
? vol(k) * vol(k) / 4.0 *
(4.0 * pow(cappedTime(k + 1, t) - time2(k), 2.0) -
(pow(flooredTime(k, w) - 2.0 * time2(k) +
cappedTime(k + 1, t),
2.0) +
pow(cappedTime(k + 1, t) - flooredTime(k, w), 2.0)))
: vol(k) * vol(k) / (2.0 * rev(k) * rev(k)) *
(exp(-2.0 * rev(k) * (cappedTime(k + 1, t) - time2(k))) +
1.0 -
(exp(-rev(k) * (flooredTime(k, w) - 2.0 * time2(k) +
cappedTime(k + 1, t))) +
exp(-rev(k) *
(cappedTime(k + 1, t) - flooredTime(k, w)))));
// zeta_i (i>k)
for (int i = k + 1; i <= upperIndex(t) - 1; i++)
res2 *= exp(-rev(i) * (cappedTime(i + 1, t) - time2(i)));
// no beta_j in this case ...
res += res2;
}
cache2a_.insert(std::make_pair(key, res));
return res;
} // expectation_rn_part
Real GsrProcessCore::expectation_tf_part(const Time w,
const Time dt) const {
Real t = w + dt;
std::pair<Real, Real> key;
key = std::make_pair(w, t);
std::map<std::pair<Real, Real>, Real>::const_iterator k =
cache2b_.find(key);
if (k != cache2b_.end())
return k->second;
Real res = 0.0;
// int -A(s,t) \sigma^2 G(s,T)
for (int k = lowerIndex(w); k <= upperIndex(t) - 1; k++) {
Real res2 = 0.0;
// l>k
for (int l = k + 1; l <= upperIndex(T_) - 1; l++) {
Real res3 = 1.0;
// eta_l
res3 *= revZero(l)
? cappedTime(l + 1, T_) - time2(l)
: (1.0 -
exp(-rev(l) * (cappedTime(l + 1, T_) - time2(l)))) /
rev(l);
// zeta_i (i>k)
for (int i = k + 1; i <= upperIndex(t) - 1; i++)
res3 *= exp(-rev(i) * (cappedTime(i + 1, t) - time2(i)));
// gamma_j (j>k)
for (int j = k + 1; j <= l - 1; j++)
res3 *= exp(-rev(j) * (time2(j + 1) - time2(j)));
// zeta_k gamma_k
res3 *=
revZero(k)
? (cappedTime(k + 1, t) - time2(k + 1) -
(2.0 * flooredTime(k, w) - cappedTime(k + 1, t) -
time2(k + 1))) /
2.0
: (exp(rev(k) * (cappedTime(k + 1, t) - time2(k + 1))) -
exp(rev(k) * (2.0 * flooredTime(k, w) -
cappedTime(k + 1, t) - time2(k + 1)))) /
(2.0 * rev(k));
// add to sum
res2 += res3;
}
// l=k
Real res3 = 1.0;
// eta_k zeta_k
res3 *=
revZero(k)
? (-pow(cappedTime(k + 1, t) - cappedTime(k + 1, T_), 2.0) -
2.0 * pow(cappedTime(k + 1, t) - flooredTime(k, w), 2.0) +
pow(2.0 * flooredTime(k, w) - cappedTime(k + 1, T_) -
cappedTime(k + 1, t),
2.0)) /
4.0
: (2.0 - exp(rev(k) *
(cappedTime(k + 1, t) - cappedTime(k + 1, T_))) -
(2.0 * exp(-rev(k) *
(cappedTime(k + 1, t) - flooredTime(k, w))) -
exp(rev(k) *
(2.0 * flooredTime(k, w) - cappedTime(k + 1, T_) -
cappedTime(k + 1, t))))) /
(2.0 * rev(k) * rev(k));
// zeta_i (i>k)
for (int i = k + 1; i <= upperIndex(t) - 1; i++)
res3 *= exp(-rev(i) * (cappedTime(i + 1, t) - time2(i)));
// no gamma_j in this case ...
res2 += res3;
// add to main accumulator
res += -vol(k) * vol(k) * res2;
}
cache2b_.insert(std::make_pair(key, res));
return res;
} // expectation_tf_part
Real GsrProcessCore::variance(const Time w, const Time dt) const {
Real t = w + dt;
std::pair<Real, Real> key;
key = std::make_pair(w, t);
std::map<std::pair<Real, Real>, Real>::const_iterator k = cache3_.find(key);
if (k != cache3_.end())
return k->second;
Real res = 0.0;
for (int k = lowerIndex(w); k <= upperIndex(t) - 1; k++) {
Real res2 = vol(k) * vol(k);
// zeta_k^2
res2 *= revZero(k)
? -(flooredTime(k, w) - cappedTime(k + 1, t))
: (1.0 - exp(2.0 * rev(k) *
(flooredTime(k, w) - cappedTime(k + 1, t)))) /
(2.0 * rev(k));
// zeta_i (i>k)
for (int i = k + 1; i <= upperIndex(t) - 1; i++) {
res2 *= exp(-2.0 * rev(i) * (cappedTime(i + 1, t) - time2(i)));
}
res += res2;
}
cache3_.insert(std::make_pair(key, res));
return res;
}
Real GsrProcessCore::y(const Time t) const {
Real key;
key = t;
std::map<Real, Real>::const_iterator k = cache4_.find(key);
if (k != cache4_.end())
return k->second;
Real res = 0.0;
for (int i = 0; i <= upperIndex(t) - 1; i++) {
Real res2 = 1.0;
for (int j = i + 1; j <= upperIndex(t) - 1; j++) {
res2 *= exp(-2.0 * rev(j) * (cappedTime(j + 1, t) - time2(j)));
}
res2 *= revZero(i) ? vol(i) * vol(i) * (cappedTime(i + 1, t) - time2(i))
: (vol(i) * vol(i) / (2.0 * rev(i)) *
(1.0 - exp(-2.0 * rev(i) *
(cappedTime(i + 1, t) - time2(i)))));
res += res2;
}
cache4_.insert(std::make_pair(key, res));
return res;
}
Real GsrProcessCore::G(const Time t, const Time w) const {
std::pair<Real, Real> key;
key = std::make_pair(w, t);
std::map<std::pair<Real, Real>, Real>::const_iterator k = cache5_.find(key);
if (k != cache5_.end())
return k->second;
Real res = 0.0;
for (int i = lowerIndex(t); i <= upperIndex(w) - 1; i++) {
Real res2 = 1.0;
for (int j = lowerIndex(t); j <= i - 1; j++) {
res2 *= exp(-rev(j) * (time2(j + 1) - flooredTime(j, t)));
}
res2 *= revZero(i) ? cappedTime(i + 1, w) - flooredTime(i, t)
: (1.0 - exp(-rev(i) * (cappedTime(i + 1, w) -
flooredTime(i, t)))) /
rev(i);
res += res2;
}
cache5_.insert(std::make_pair(key, res));
return res;
}
int GsrProcessCore::lowerIndex(const Time t) const {
return static_cast<int>(std::upper_bound(times_.begin(), times_.end(), t) -
times_.begin());
}
int GsrProcessCore::upperIndex(const Time t) const {
if (t < QL_MIN_POSITIVE_REAL)
return 0;
return static_cast<int>(
std::upper_bound(times_.begin(), times_.end(), t - QL_EPSILON) -
times_.begin()) +
1;
}
Real GsrProcessCore::cappedTime(const Size index, const Real cap) const {
return cap != Null<Real>() ? std::min(cap, time2(index)) : time2(index);
}
Real GsrProcessCore::flooredTime(const Size index,
const Real floor) const {
return floor != Null<Real>() ? std::max(floor, time2(index)) : time2(index);
}
Real GsrProcessCore::time2(const Size index) const {
if (index == 0)
return 0.0;
if (index > times_.size())
return T_; // FIXME how to ensure that forward
// measure time is geq all times
// given
return times_[index - 1];
}
Real GsrProcessCore::vol(const Size index) const {
if (index >= vols_.size())
return vols_.back();
return vols_[index];
}
Real GsrProcessCore::rev(const Size index) const {
if (index >= reversions_.size())
return reversions_.back();
return reversions_[index];
}
bool GsrProcessCore::revZero(const Size index) const {
if (index >= revZero_.size())
return revZero_.back();
return revZero_[index];
}
} // namespace detail
} // namesapce QuantLib