-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
sample.h
476 lines (379 loc) · 11.4 KB
/
sample.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// sample.h: Rcpp R/C++ interface class library -- sample
//
// Copyright (C) 2016 Nathan Russell
//
// This file is part of Rcpp.
//
// Rcpp 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.
//
// Rcpp 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 Rcpp. If not, see <http://www.gnu.org/licenses/>.
// The sample() in RcppArmadillo came first, but is opt-in. In case someone did
// in fact load it, we need to skip the declarations here to avoid a conflict
#ifndef RCPPARMADILLO__EXTENSIONS__SAMPLE_H
#ifndef Rcpp__sugar__sample_h
#define Rcpp__sugar__sample_h
#include <vector>
// In order to mirror the behavior of `base::sample`
// as closely as possible, this file contains adaptations
// of several functions in R/src/main/random.c:
//
// * do_sample - general logic as well as the empirical sampling routine.
//
// * FixupProb - an auxiliary function.
//
// * walker_ProbSampleReplace, ProbSampleReplace, and ProbSampleNoReplace -
// algorithms for sampling according to a supplied probability vector.
//
// For each of the sampling routines, two signatures are provided:
//
// * A version that returns an integer vector, which can be used to
// generate 0-based indices (one_based = false) or 1-based indices
// (one_based = true) -- where the latter corresponds to the
// bahavior of `base::sample.int`.
//
// * A version which takes an input Vector<> (rather than an integer 'n'),
// and samples its elements -- this corresponds to `base::sample`.
namespace Rcpp {
namespace sugar {
// Adapted from `FixupProb`
// Normalizes a probability vector 'p' S.T. sum(p) == 1
inline void Normalize(Vector<REALSXP>& p, int require_k, bool replace)
{
double sum = 0.0;
R_xlen_t npos = 0, i = 0, n = p.size();
for ( ; i < n; i++) {
if (!R_FINITE(p[i]) || (p[i] < 0)) {
stop("Probabilities must be finite and non-negative!");
}
npos += (p[i] > 0.0);
sum += p[i];
}
if ((!npos) || (!replace && (require_k > npos))) {
stop("Too few positive probabilities!");
}
for (i = 0; i < n; i++) {
p[i] /= sum;
}
}
// Adapted from `ProbSampleReplace`
// Index version
inline Vector<INTSXP> SampleReplace(Vector<REALSXP>& p, int n, int k, bool one_based)
{
Vector<INTSXP> perm = no_init(n), ans = no_init(k);
double rU = 0.0;
int i = 0, j = 0, nm1 = n - 1;
int adj = one_based ? 0 : 1;
for ( ; i < n; i++) {
perm[i] = i + 1;
}
Rf_revsort(p.begin(), perm.begin(), n);
for (i = 1; i < n; i++) {
p[i] += p[i - 1];
}
for (i = 0; i < k; i++) {
rU = unif_rand();
for (j = 0; j < nm1; j++) {
if (rU <= p[j]) {
break;
}
}
ans[i] = perm[j] - adj;
}
return ans;
}
// Element version
template <int RTYPE>
inline Vector<RTYPE> SampleReplace(Vector<REALSXP>& p, int k, const Vector<RTYPE>& ref)
{
int n = ref.size();
Vector<INTSXP> perm = no_init(n);
Vector<RTYPE> ans = no_init(k);
double rU = 0.0;
int i = 0, j = 0, nm1 = n - 1;
for ( ; i < n; i++) {
perm[i] = i + 1;
}
Rf_revsort(p.begin(), perm.begin(), n);
for (i = 1; i < n; i++) {
p[i] += p[i - 1];
}
for (i = 0; i < k; i++) {
rU = unif_rand();
for (j = 0; j < nm1; j++) {
if (rU <= p[j]) {
break;
}
}
ans[i] = ref[perm[j] - 1];
}
return ans;
}
// Adapted from `walker_ProbSampleReplace`
// Index version
inline Vector<INTSXP> WalkerSample(const Vector<REALSXP>& p, int n, int nans, bool one_based)
{
Vector<INTSXP> a = no_init(n), ans = no_init(nans);
int i, j, k;
std::vector<double> q(n);
double rU;
std::vector<int> HL(n);
std::vector<int>::iterator H, L;
int adj = one_based ? 1 : 0;
H = HL.begin() - 1; L = HL.begin() + n;
for (i = 0; i < n; i++) {
q[i] = p[i] * n;
if (q[i] < 1.0) {
*++H = i;
} else {
*--L = i;
}
}
if (H >= HL.begin() && L < HL.begin() + n) {
for (k = 0; k < n - 1; k++) {
i = HL[k];
j = *L;
a[i] = j;
q[j] += q[i] - 1;
L += (q[j] < 1.0);
if (L >= HL.begin() + n) {
break;
}
}
}
for (i = 0; i < n; i++) {
q[i] += i;
}
for (i = 0; i < nans; i++) {
rU = unif_rand() * n;
k = static_cast<int>(rU);
ans[i] = (rU < q[k]) ? k + adj : a[k] + adj;
}
return ans;
}
// Element version
template <int RTYPE>
inline Vector<RTYPE> WalkerSample(const Vector<REALSXP>& p, int nans, const Vector<RTYPE>& ref)
{
int n = ref.size();
Vector<INTSXP> a = no_init(n);
Vector<RTYPE> ans = no_init(nans);
int i, j, k;
std::vector<double> q(n);
double rU;
std::vector<int> HL(n);
std::vector<int>::iterator H, L;
H = HL.begin() - 1; L = HL.begin() + n;
for (i = 0; i < n; i++) {
q[i] = p[i] * n;
if (q[i] < 1.0) {
*++H = i;
} else {
*--L = i;
}
}
if (H >= HL.begin() && L < HL.begin() + n) {
for (k = 0; k < n - 1; k++) {
i = HL[k];
j = *L;
a[i] = j;
q[j] += q[i] - 1;
L += (q[j] < 1.0);
if (L >= HL.begin() + n) {
break;
}
}
}
for (i = 0; i < n; i++) {
q[i] += i;
}
for (i = 0; i < nans; i++) {
rU = unif_rand() * n;
k = static_cast<int>(rU);
ans[i] = (rU < q[k]) ? ref[k] : ref[a[k]];
}
return ans;
}
// Adapted from `ProbSampleNoReplace`
// Index version
inline Vector<INTSXP> SampleNoReplace(Vector<REALSXP>& p, int n, int nans, bool one_based)
{
Vector<INTSXP> perm = no_init(n), ans = no_init(nans);
double rT, mass, totalmass;
int i, j, k, n1;
int adj = one_based ? 0 : 1;
for (i = 0; i < n; i++) {
perm[i] = i + 1;
}
Rf_revsort(p.begin(), perm.begin(), n);
totalmass = 1.0;
for (i = 0, n1 = n - 1; i < nans; i++, n1--) {
rT = totalmass * unif_rand();
mass = 0.0;
for (j = 0; j < n1; j++) {
mass += p[j];
if (rT <= mass) {
break;
}
}
ans[i] = perm[j] - adj;
totalmass -= p[j];
for (k = j; k < n1; k++) {
p[k] = p[k + 1];
perm[k] = perm[k + 1];
}
}
return ans;
}
// Element version
template <int RTYPE>
inline Vector<RTYPE> SampleNoReplace(Vector<REALSXP>& p, int nans, const Vector<RTYPE>& ref)
{
int n = ref.size();
Vector<INTSXP> perm = no_init(n);
Vector<RTYPE> ans = no_init(nans);
double rT, mass, totalmass;
int i, j, k, n1;
for (i = 0; i < n; i++) {
perm[i] = i + 1;
}
Rf_revsort(p.begin(), perm.begin(), n);
totalmass = 1.0;
for (i = 0, n1 = n - 1; i < nans; i++, n1--) {
rT = totalmass * unif_rand();
mass = 0.0;
for (j = 0; j < n1; j++) {
mass += p[j];
if (rT <= mass) {
break;
}
}
ans[i] = ref[perm[j] - 1];
totalmass -= p[j];
for (k = j; k < n1; k++) {
p[k] = p[k + 1];
perm[k] = perm[k + 1];
}
}
return ans;
}
// Adapted from segment of `do_sample`
// Index version
inline Vector<INTSXP> EmpiricalSample(int n, int size, bool replace, bool one_based)
{
Vector<INTSXP> ans = no_init(size);
Vector<INTSXP>::iterator ians = ans.begin(), eans = ans.end();
int adj = one_based ? 1 : 0;
if (replace || size < 2) {
for ( ; ians != eans; ++ians) {
*ians = static_cast<int>(n * unif_rand() + adj);
}
return ans;
}
int* x = reinterpret_cast<int*>(R_alloc(n, sizeof(int)));
for (int i = 0; i < n; i++) {
x[i] = i;
}
for ( ; ians != eans; ++ians) {
int j = static_cast<int>(n * unif_rand());
*ians = x[j] + adj;
x[j] = x[--n];
}
return ans;
}
// Element version
template <int RTYPE>
inline Vector<RTYPE> EmpiricalSample(int size, bool replace, const Vector<RTYPE>& ref)
{
int n = ref.size();
Vector<RTYPE> ans = no_init(size);
typename Vector<RTYPE>::iterator ians = ans.begin(), eans = ans.end();
if (replace || size < 2) {
for ( ; ians != eans; ++ians) {
*ians = ref[static_cast<int>(n * unif_rand())];
}
return ans;
}
int* x = reinterpret_cast<int*>(R_alloc(n, sizeof(int)));
for (int i = 0; i < n; i++) {
x[i] = i;
}
for ( ; ians != eans; ++ians) {
int j = static_cast<int>(n * unif_rand());
*ians = ref[x[j]];
x[j] = x[--n];
}
return ans;
}
typedef Nullable< Vector<REALSXP> > probs_t;
} // sugar
// Adapted from `do_sample`
inline Vector<INTSXP>
sample(int n, int size, bool replace = false, sugar::probs_t probs = R_NilValue, bool one_based = true)
{
if (probs.isNotNull()) {
Vector<REALSXP> p = clone(probs.get());
if (static_cast<int>(p.size()) != n) {
stop("probs.size() != n!");
}
sugar::Normalize(p, size, replace);
if (replace) {
int i = 0, nc = 0;
for ( ; i < n; i++) {
nc += (n * p[i] > 0.1);
}
return nc > 200 ? sugar::WalkerSample(p, n, size, one_based) :
sugar::SampleReplace(p, n, size, one_based);
}
if (size > n) {
stop("Sample size must be <= n when not using replacement!");
}
return sugar::SampleNoReplace(p, n, size, one_based);
}
if (!replace && size > n) {
stop("Sample size must be <= n when not using replacement!");
}
return sugar::EmpiricalSample(n, size, replace, one_based);
}
template <int RTYPE>
inline Vector<RTYPE>
sample(const Vector<RTYPE>& x, int size, bool replace = false, sugar::probs_t probs = R_NilValue)
{
int n = x.size();
if (probs.isNotNull()) {
Vector<REALSXP> p = clone(probs.get());
if (static_cast<int>(p.size()) != n) {
stop("probs.size() != n!");
}
sugar::Normalize(p, size, replace);
if (replace) {
int i = 0, nc = 0;
for ( ; i < n; i++) {
nc += (n * p[i] > 0.1);
}
return nc > 200 ? sugar::WalkerSample(p, size, x) :
sugar::SampleReplace(p, size, x);
}
if (size > n) {
stop("Sample size must be <= n when not using replacement!");
}
return sugar::SampleNoReplace(p, size, x);
}
if (!replace && size > n) {
stop("Sample size must be <= n when not using replacement!");
}
return sugar::EmpiricalSample(size, replace, x);
}
} // Rcpp
#endif // Rcpp__sugar__sample_h
#endif // RCPPARMADILLO__EXTENSIONS__SAMPLE_H