-
Notifications
You must be signed in to change notification settings - Fork 4
/
ziggurat.cpp
363 lines (293 loc) · 8.79 KB
/
ziggurat.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
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// ziggurat.cpp: R wrapper for various Ziggurat implementations in RcppZiggurat
//
// Copyright (C) 2013 Dirk Eddelbuettel
//
// This file is part of RcppZiggurat.
//
// RcppZiggurat is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// RcppZiggurat 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
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RcppZiggurat. If not, see <http://www.gnu.org/licenses/>.
// trying a simple Ziggurat class based (initially) on John Burkardt's code
// Burkhardt implements the original paper http://www.jstatsoft.org/v05/i08/paper
// but (initially) NOT the comment paper http://www.jstatsoft.org/v12/i07/paper
// following email with Burkhard has modified
#include <Rcpp.h>
#include <ZigguratMT.h>
#include <ZigguratLZLLV.h>
#include <ZigguratV1.h>
#include <ZigguratV1b.h>
#include <Ziggurat.h>
#include <ZigguratGSL.h>
#include <ZigguratQL.h>
#include <ZigguratGretl.h>
#include <ZigguratR.h>
// Version 1 -- Derived from Marsaglia and Tsang, JSS, 2000
static Ziggurat::MT::ZigguratMT ziggmt;
// Marsaglia and Tsang (JSS,2000)
// [[Rcpp::export]]
Rcpp::NumericVector zrnormMT(int n) {
Rcpp::NumericVector x(n);
for (int i=0; i<n; i++) {
x[i] = ziggmt.norm();
}
return x;
}
// [[Rcpp::export]]
void zsetseedMT(int s) {
ziggmt.setSeed(s);
}
// Version 2 -- Derived from Leong et al, JSS, 2005
static Ziggurat::LZLLV::ZigguratLZLLV zigglzllv;
// Marsaglia and Tsang (JSS,2000)
// [[Rcpp::export]]
Rcpp::NumericVector zrnormLZLLV(int n) {
Rcpp::NumericVector x(n);
for (int i=0; i<n; i++) {
x[i] = zigglzllv.norm();
}
return x;
}
// [[Rcpp::export]]
void zsetseedLZLLV(int s) {
zigglzllv.setSeed(s);
}
// Version 3 -- Derived from John Burkardt's older implementation
static Ziggurat::V1::ZigguratV1 ziggv1;
// [[Rcpp::export]]
Rcpp::NumericVector zrnormV1(int n) {
Rcpp::NumericVector x(n);
for (int i=0; i<n; i++) {
x[i] = ziggv1.norm();
}
return x;
}
// [[Rcpp::export]]
Rcpp::NumericVector zrnormVecV1(Rcpp::NumericVector x) {
int n = x.size();
for (int i=0; i<n; i++) {
x[i] = ziggv1.norm();
}
return x;
}
// [[Rcpp::export]]
std::vector<double> zrnormStlV1(int n) {
std::vector<double> x(n);
for (int i=0; i<n; i++) {
x[i] = ziggv1.norm();
}
return x;
}
// [[Rcpp::export]]
void zsetseedV1(unsigned long int s) {
ziggv1.setSeed(s);
return;
}
// [[Rcpp::export]]
unsigned long int zgetseedV1() {
return ziggv1.getSeed();
}
// Version 4 -- Derived from John Burkardt's new implementation of our mods
static Ziggurat::Ziggurat::Ziggurat zigg;
// [[Rcpp::export]]
Rcpp::NumericVector zrnorm(int n) {
Rcpp::NumericVector x(n);
for (int i=0; i<n; i++) {
x[i] = zigg.norm();
}
return x;
}
// [[Rcpp::export]]
Rcpp::NumericVector zrnormVec(Rcpp::NumericVector x) {
int n = x.size();
for (int i=0; i<n; i++) {
x[i] = zigg.norm();
}
return x;
}
// [[Rcpp::export]]
std::vector<double> zrnormStl(int n) {
std::vector<double> x(n);
for (int i=0; i<n; i++) {
x[i] = zigg.norm();
}
return x;
}
// [[Rcpp::export]]
void zsetseed(unsigned long int s) {
zigg.setSeed(s);
return;
}
// [[Rcpp::export]]
unsigned long int zgetseed() {
return zigg.getSeed();
}
// Version 5 -- Wrapping the GSL version
static Ziggurat::GSL::ZigguratGSL gsl;
// [[Rcpp::export]]
void zsetseedGSL(const uint32_t s) {
gsl.setSeed(s);
return;
}
// [[Rcpp::export]]
Rcpp::NumericVector zrnormGSL(int n) {
Rcpp::NumericVector x(n);
for (int i=0; i<n; i++) {
x[i] = gsl.norm();
}
return x;
}
// Version 6 -- Modified V1
static Ziggurat::V1::ZigguratV1b ziggv1b;
// [[Rcpp::export]]
Rcpp::NumericVector zrnormV1b(int n) {
Rcpp::NumericVector x(n);
for (int i=0; i<n; i++) {
x[i] = ziggv1b.norm();
}
return x;
}
// Version 7 -- QuantLib
static Ziggurat::QL::ZigguratQL ziggql;
// [[Rcpp::export]]
Rcpp::NumericVector zrnormQL(int n) {
Rcpp::NumericVector x(n);
for (int i=0; i<n; i++) {
x[i] = ziggql.norm();
}
return x;
}
// [[Rcpp::export]]
void zsetseedQL(unsigned long int s) {
ziggql.setSeed(s);
return;
}
// Version 8 -- Gretl
static Ziggurat::Gretl::ZigguratGretl zigggl;
// [[Rcpp::export]]
Rcpp::NumericVector zrnormGl(int n) {
Rcpp::NumericVector x(n);
for (int i=0; i<n; i++) {
x[i] = zigggl.norm();
}
return x;
}
// [[Rcpp::export]]
void zsetseedGl(unsigned long int s) {
zigggl.setSeed(s);
return;
}
// Version 9 -- Ziggurat with R's unif. generator
static Ziggurat::R::ZigguratR ziggr;
// [[Rcpp::export]]
Rcpp::NumericVector zrnormR(int n) {
Rcpp::NumericVector x(n);
for (int i=0; i<n; i++) {
x[i] = ziggr.norm();
}
return x;
}
// no zsetseedR() as we use set.seed() to seed R's uniform
// see help(RNGind) and help(Random.user) in R
static double x_;
extern "C" double *user_norm_rand() {
x_ = ziggr.norm();
return &x_;
}
Ziggurat::Zigg* getZiggurat(const std::string generator, const int seed) {
if (generator=="MT") {
return new Ziggurat::MT::ZigguratMT(seed);
} else if (generator=="LZLLV") {
return new Ziggurat::LZLLV::ZigguratLZLLV(seed);
} else if (generator=="V1") {
return new Ziggurat::V1::ZigguratV1(seed);
} else if (generator=="Ziggurat") {
return new Ziggurat::Ziggurat::Ziggurat(seed);
} else if (generator=="GSL") {
return new Ziggurat::GSL::ZigguratGSL(seed);
} else if (generator=="V1b") {
return new Ziggurat::V1::ZigguratV1b(seed);
} else if (generator=="QL") {
return new Ziggurat::QL::ZigguratQL(seed);
} else if (generator=="Gretl") {
return new Ziggurat::Gretl::ZigguratGretl(seed);
}
Rcpp::Rcout << "Unrecognised generator: " << generator << "\n";
return NULL;
}
// [[Rcpp::export]]
Rcpp::NumericMatrix ziggbin(int nbins, double ndraws,
const std::string generator = "Ziggurat",
const int seed=42, int edge = 7, int res = 40) {
Rcpp::NumericMatrix v(res, nbins);
const double grmin = -edge;
const double grmax = edge;
const double d = (grmax - grmin)/static_cast<double>(nbins);
Ziggurat::Zigg *zigg = getZiggurat(generator, seed);
double seglen = static_cast<double>(ndraws)/static_cast<double>(res);
// Yes, it crazy to write the inner loop with doubles. But the CRAN maintainers
// still say that we cannot use C99 or C++0x -- so no long long int for us.
// And an unsigned long makes it only to 4.2e9 which is not enough for our use.
for (int row=0; row<res; row++) {
double i = 0.0;
while (i < seglen) {
double val = zigg->norm(); // N(0,1) draw
int can = floor((val - grmin)/d); // find the 'can' to bin it in
can = (can < 0) ? 0 : can; // protect can from being below 0
can = (can > nbins-1) ? nbins-1 : can; // or past the last can
v(row,can) = v(row,can) + 1.0; // increment counter
i = i + 1.0;
}
if (row < res-1) {
v(row+1,Rcpp::_) = v(row,Rcpp::_);
}
}
delete zigg;
return v;
}
// [[Rcpp::export]]
Rcpp::NumericVector ziggsum(int nbins, double ndraws,
const std::string generator = "Ziggurat",
const int seed=42) {
Rcpp::NumericVector vec(nbins);
Ziggurat::Zigg *zigg = getZiggurat(generator, seed);
for (int i=0; i<nbins; i++) {
double sum = 0.0;
double j = 0.0;
while (j < ndraws) {
sum = sum + zigg->norm();
j = j + 1.0;
}
vec[i] = sum;
}
delete zigg;
return vec;
}
// [[Rcpp::export]]
Rcpp::NumericVector ziggtest(int nbins, double ndraws,
const std::string generator = "Ziggurat",
const int seed=42) {
Rcpp::NumericVector vec(nbins);
Ziggurat::Zigg *zigg = getZiggurat(generator, seed);
for (int i=0; i<nbins; i++) {
double sum = 0.0;
double j = 0.0;
while (j < ndraws) {
sum = sum + R::pnorm(zigg->norm(),0.0,1.0,0,0);
j = j + 1.0;
}
vec[i] = sum;
}
delete zigg;
return vec;
}