-
Notifications
You must be signed in to change notification settings - Fork 62
/
mrg32k3a.h
395 lines (341 loc) · 8.03 KB
/
mrg32k3a.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
Written by Antoine Savine in 2018
This code is the strict IP of Antoine Savine
License to use and alter this code for personal and commercial applications
is freely granted to any person or company who purchased a copy of the book
Modern Computational Finance: AAD and Parallel Simulations
Antoine Savine
Wiley, 2018
As long as this comment is preserved at the top of the file
*/
#pragma once
// Implementation of the mrg32k3a RNG,
// See chapters 5 and 6
#include "mcBase.h"
#include "gaussians.h"
class mrg32k3a : public RNG
{
// Seed
const double myA, myB;
// Dimension
size_t myDim;
// State
double myXn, myXn1, myXn2, myYn, myYn1, myYn2;
// Antithetic
bool myAnti;
// false: generate new, true: negate cached
vector<double> myCachedUniforms;
vector<double> myCachedGaussians;
// Constants
static constexpr double m1 = 4294967087;
static constexpr double m2 = 4294944443;
static constexpr double a12 = 1403580;
static constexpr double a13 = 810728;
static constexpr double a21 = 527612;
static constexpr double a23 = 1370589;
// We divide the final uniform
// by m1 + 1 so we never hit 1
static constexpr double m1p1 = 4294967088;
// Produce next number and update state
double nextNumber()
{
// Update X
// Recursion
double x = a12 * myXn1 - a13 * myXn2;
// Modulus
x -= long(x / m1) * m1;
if (x < 0) x += m1;
// Update
myXn2 = myXn1;
myXn1 = myXn;
myXn = x;
// Same for Y
double y = a21 * myYn - a23 * myYn2;
y -= long(y / m2) * m2;
if (y < 0) y += m2;
myYn2 = myYn1;
myYn1 = myYn;
myYn = y;
// Uniform
const double u = x > y
? (x - y) / m1p1
: (x - y + m1) / m1p1;
return u;
}
public:
// Constructor with seed
mrg32k3a(const unsigned a = 12345, const unsigned b = 12346) :
myA(a), myB(b)
{
reset();
}
// Reset state to 0 (seed)
void reset()
{
// Reset state
myXn = myXn1 = myXn2 = myA;
myYn = myYn1 = myYn2 = myB;
// Anti = false: generate next
myAnti = false;
}
// Virtual copy constructor
unique_ptr<RNG> clone() const override
{
return make_unique<mrg32k3a>(*this);
}
// Initializer
void init(const size_t simDim) override
{
myDim = simDim;
myCachedUniforms.resize(myDim);
myCachedGaussians.resize(myDim);
}
void nextU(vector<double>& uVec) override
{
if (myAnti)
{
// Do not generate, negate cached
transform(
myCachedUniforms.begin(),
myCachedUniforms.end(),
uVec.begin(),
[](const double d) { return 1.0 - d; });
// Generate next
myAnti = false;
}
else
{
// Generate and cache
generate(
myCachedUniforms.begin(),
myCachedUniforms.end(),
[this]() { return nextNumber(); });
// Copy
copy(
myCachedUniforms.begin(),
myCachedUniforms.end(),
uVec.begin());
// Do not generate next
myAnti = true;
}
}
void nextG(vector<double>& gaussVec) override
{
if (myAnti)
{
// Do not generate, negate cached
// Note: we reuse the Gaussian numbers,
// we save not only generation, but also
// Gaussian transaformation
transform(
myCachedGaussians.begin(),
myCachedGaussians.end(),
gaussVec.begin(),
[](const double n) { return -n; });
// Generate next
myAnti = false;
}
else
{
// Generate and cache
generate(
myCachedGaussians.begin(),
myCachedGaussians.end(),
[this]() { return invNormalCdf(nextNumber()); });
// Copy
copy(
myCachedGaussians.begin(),
myCachedGaussians.end(),
gaussVec.begin());
// Do not generate next
myAnti = true;
}
}
// Skip ahead logic
// See chapter 7
// To avoid overflow, we nest mods in innermost results
// and use 64bit unsigned long long for storage
// Skip ahead
void skipTo(const unsigned b) override
{
// First reset to 0
reset();
// How many numbers to skip
unsigned skipnums = b * myDim;
bool odd = false;
// Antithetic: skip only half
if (skipnums & 1)
{
// Odd
odd = true;
skipnums = (skipnums - 1) / 2;
}
else
{
// Even
skipnums /= 2;
}
// Skip state
skipNumbers(skipnums);
// If odd, pre-generate for antithetic
if (odd)
{
myAnti = true;
// Uniforms
generate(
myCachedUniforms.begin(),
myCachedUniforms.end(),
[this]() { return nextNumber(); });
// Gaussians
generate(
myCachedGaussians.begin(),
myCachedGaussians.end(),
[this]() { return invNormalCdf(nextNumber()); });
}
else
{
myAnti = false;
}
}
private:
// Matrix product with modulus
static void mPrd(
const unsigned long long lhs[3][3],
const unsigned long long rhs[3][3],
const unsigned long long mod,
unsigned long long result[3][3])
{
// Result go to temp, in case result points to lhs or rhs
unsigned long long temp[3][3];
for (size_t j = 0; j<3; j++)
{
for (size_t k = 0; k<3; k++)
{
unsigned long long s = 0;
for (size_t l = 0; l<3; l++)
{
// Apply modulus to innermost product
unsigned long long tmpNum = lhs[j][l] * rhs[l][k];
// Apply mod
tmpNum %= mod;
// Result
s += tmpNum;
// Reapply mod
s %= mod;
}
// Store result in temp
temp[j][k] = s;
}
}
// Now product is done, copy temp to result
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
result[j][k] = temp[j][k];
}
}
}
// Matrix by vector, exact same logic
// Except we don't implement temp,
// we never point result to lhs or rhs
static void vPrd(
const unsigned long long lhs[3][3],
const unsigned long long rhs[3],
const unsigned long long mod,
unsigned long long result[3])
{
for (size_t j = 0; j<3; j++)
{
unsigned long long s = 0;
for (size_t l = 0; l<3; l++)
{
unsigned long long tmpNum = lhs[j][l] * rhs[l];
tmpNum %= mod;
s += tmpNum;
s %= mod;
}
result[j] = s;
}
}
void skipNumbers(const unsigned b)
{
if ( b <= 0) return;
unsigned skip = b;
static constexpr unsigned long long
m1l = unsigned long long(m1);
static constexpr unsigned long long
m2l = unsigned long long(m2);
unsigned long long Ab[3][3] = {
{ 1, 0 ,0 },
{ 0, 1, 0 },
{ 0, 0, 1 }
},
Bb[3][3] = {
{ 1, 0 ,0 },
{ 0, 1, 0 },
{ 0, 0, 1 }
},
Ai[3][3] = { // A0 = A
{
0,
unsigned long long (a12) ,
unsigned long long (m1 - a13)
// m1 - a13 instead of -a13
// so results are always positive
// and we can use unsigned long longs
// after modulus, we get the same results
},
{ 1, 0, 0 },
{ 0, 1, 0 }
},
Bi[3][3] = { // B0 = B
{
unsigned long long (a21),
0 ,
unsigned long long (m2 - a23)
// same logic: m2 - a32
},
{ 1, 0, 0 },
{ 0, 1, 0 }
};
while (skip>0)
{
if (skip & 1) // i.e. ai == 1
{
// accumulate Ab and Bb
mPrd(Ab, Ai, m1l, Ab);
mPrd(Bb, Bi, m2l, Bb);
}
// Recursion on Ai and Bi
mPrd(Ai, Ai, m1l, Ai);
mPrd(Bi, Bi, m2l, Bi);
skip >>= 1;
}
// Final result
unsigned long long X0[3] =
{
unsigned long long (myXn),
unsigned long long (myXn1),
unsigned long long (myXn2)
},
Y0[3] =
{
unsigned long long (myYn),
unsigned long long (myYn1),
unsigned long long (myYn2)
},
temp[3];
// From initial to final state
vPrd(Ab, X0, m1l, temp);
// Convert back to doubles
myXn = double(temp[0]);
myXn1 = double(temp[1]);
myXn2 = double(temp[2]);
// Same for Y
vPrd(Bb, Y0, m2l, temp);
myYn = double(temp[0]);
myYn1 = double(temp[1]);
myYn2 = double(temp[2]);
}
};