-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
distributions.c
1689 lines (1494 loc) · 46 KB
/
distributions.c
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "numpy/random/distributions.h"
#include "ziggurat_constants.h"
#include "logfactorial.h"
#if defined(_MSC_VER) && defined(_WIN64)
#include <intrin.h>
#endif
#include <assert.h>
/* Inline generators for internal use */
static NPY_INLINE uint32_t next_uint32(bitgen_t *bitgen_state) {
return bitgen_state->next_uint32(bitgen_state->state);
}
static NPY_INLINE uint64_t next_uint64(bitgen_t *bitgen_state) {
return bitgen_state->next_uint64(bitgen_state->state);
}
static NPY_INLINE float next_float(bitgen_t *bitgen_state) {
return (next_uint32(bitgen_state) >> 8) * (1.0f / 16777216.0f);
}
/* Random generators for external use */
float random_standard_uniform_f(bitgen_t *bitgen_state) {
return next_float(bitgen_state);
}
double random_standard_uniform(bitgen_t *bitgen_state) {
return next_double(bitgen_state);
}
void random_standard_uniform_fill(bitgen_t *bitgen_state, npy_intp cnt, double *out) {
npy_intp i;
for (i = 0; i < cnt; i++) {
out[i] = next_double(bitgen_state);
}
}
void random_standard_uniform_fill_f(bitgen_t *bitgen_state, npy_intp cnt, float *out) {
npy_intp i;
for (i = 0; i < cnt; i++) {
out[i] = next_float(bitgen_state);
}
}
static double standard_exponential_unlikely(bitgen_t *bitgen_state,
uint8_t idx, double x) {
if (idx == 0) {
/* Switch to 1.0 - U to avoid log(0.0), see GH 13361 */
return ziggurat_exp_r - npy_log1p(-next_double(bitgen_state));
} else if ((fe_double[idx - 1] - fe_double[idx]) * next_double(bitgen_state) +
fe_double[idx] <
exp(-x)) {
return x;
} else {
return random_standard_exponential(bitgen_state);
}
}
double random_standard_exponential(bitgen_t *bitgen_state) {
uint64_t ri;
uint8_t idx;
double x;
ri = next_uint64(bitgen_state);
ri >>= 3;
idx = ri & 0xFF;
ri >>= 8;
x = ri * we_double[idx];
if (ri < ke_double[idx]) {
return x; /* 98.9% of the time we return here 1st try */
}
return standard_exponential_unlikely(bitgen_state, idx, x);
}
void random_standard_exponential_fill(bitgen_t * bitgen_state, npy_intp cnt, double * out)
{
npy_intp i;
for (i = 0; i < cnt; i++) {
out[i] = random_standard_exponential(bitgen_state);
}
}
static float standard_exponential_unlikely_f(bitgen_t *bitgen_state,
uint8_t idx, float x) {
if (idx == 0) {
/* Switch to 1.0 - U to avoid log(0.0), see GH 13361 */
return ziggurat_exp_r_f - npy_log1pf(-next_float(bitgen_state));
} else if ((fe_float[idx - 1] - fe_float[idx]) * next_float(bitgen_state) +
fe_float[idx] <
expf(-x)) {
return x;
} else {
return random_standard_exponential_f(bitgen_state);
}
}
float random_standard_exponential_f(bitgen_t *bitgen_state) {
uint32_t ri;
uint8_t idx;
float x;
ri = next_uint32(bitgen_state);
ri >>= 1;
idx = ri & 0xFF;
ri >>= 8;
x = ri * we_float[idx];
if (ri < ke_float[idx]) {
return x; /* 98.9% of the time we return here 1st try */
}
return standard_exponential_unlikely_f(bitgen_state, idx, x);
}
void random_standard_exponential_fill_f(bitgen_t * bitgen_state, npy_intp cnt, float * out)
{
npy_intp i;
for (i = 0; i < cnt; i++) {
out[i] = random_standard_exponential_f(bitgen_state);
}
}
void random_standard_exponential_inv_fill(bitgen_t * bitgen_state, npy_intp cnt, double * out)
{
npy_intp i;
for (i = 0; i < cnt; i++) {
out[i] = -npy_log1p(-next_double(bitgen_state));
}
}
void random_standard_exponential_inv_fill_f(bitgen_t * bitgen_state, npy_intp cnt, float * out)
{
npy_intp i;
for (i = 0; i < cnt; i++) {
out[i] = -npy_log1p(-next_float(bitgen_state));
}
}
double random_standard_normal(bitgen_t *bitgen_state) {
uint64_t r;
int sign;
uint64_t rabs;
int idx;
double x, xx, yy;
for (;;) {
/* r = e3n52sb8 */
r = next_uint64(bitgen_state);
idx = r & 0xff;
r >>= 8;
sign = r & 0x1;
rabs = (r >> 1) & 0x000fffffffffffff;
x = rabs * wi_double[idx];
if (sign & 0x1)
x = -x;
if (rabs < ki_double[idx])
return x; /* 99.3% of the time return here */
if (idx == 0) {
for (;;) {
/* Switch to 1.0 - U to avoid log(0.0), see GH 13361 */
xx = -ziggurat_nor_inv_r * npy_log1p(-next_double(bitgen_state));
yy = -npy_log1p(-next_double(bitgen_state));
if (yy + yy > xx * xx)
return ((rabs >> 8) & 0x1) ? -(ziggurat_nor_r + xx)
: ziggurat_nor_r + xx;
}
} else {
if (((fi_double[idx - 1] - fi_double[idx]) * next_double(bitgen_state) +
fi_double[idx]) < exp(-0.5 * x * x))
return x;
}
}
}
void random_standard_normal_fill(bitgen_t *bitgen_state, npy_intp cnt, double *out) {
npy_intp i;
for (i = 0; i < cnt; i++) {
out[i] = random_standard_normal(bitgen_state);
}
}
float random_standard_normal_f(bitgen_t *bitgen_state) {
uint32_t r;
int sign;
uint32_t rabs;
int idx;
float x, xx, yy;
for (;;) {
/* r = n23sb8 */
r = next_uint32(bitgen_state);
idx = r & 0xff;
sign = (r >> 8) & 0x1;
rabs = (r >> 9) & 0x0007fffff;
x = rabs * wi_float[idx];
if (sign & 0x1)
x = -x;
if (rabs < ki_float[idx])
return x; /* # 99.3% of the time return here */
if (idx == 0) {
for (;;) {
/* Switch to 1.0 - U to avoid log(0.0), see GH 13361 */
xx = -ziggurat_nor_inv_r_f * npy_log1pf(-next_float(bitgen_state));
yy = -npy_log1pf(-next_float(bitgen_state));
if (yy + yy > xx * xx)
return ((rabs >> 8) & 0x1) ? -(ziggurat_nor_r_f + xx)
: ziggurat_nor_r_f + xx;
}
} else {
if (((fi_float[idx - 1] - fi_float[idx]) * next_float(bitgen_state) +
fi_float[idx]) < exp(-0.5 * x * x))
return x;
}
}
}
void random_standard_normal_fill_f(bitgen_t *bitgen_state, npy_intp cnt, float *out) {
npy_intp i;
for (i = 0; i < cnt; i++) {
out[i] = random_standard_normal_f(bitgen_state);
}
}
double random_standard_gamma(bitgen_t *bitgen_state,
double shape) {
double b, c;
double U, V, X, Y;
if (shape == 1.0) {
return random_standard_exponential(bitgen_state);
} else if (shape == 0.0) {
return 0.0;
} else if (shape < 1.0) {
for (;;) {
U = next_double(bitgen_state);
V = random_standard_exponential(bitgen_state);
if (U <= 1.0 - shape) {
X = pow(U, 1. / shape);
if (X <= V) {
return X;
}
} else {
Y = -log((1 - U) / shape);
X = pow(1.0 - shape + shape * Y, 1. / shape);
if (X <= (V + Y)) {
return X;
}
}
}
} else {
b = shape - 1. / 3.;
c = 1. / sqrt(9 * b);
for (;;) {
do {
X = random_standard_normal(bitgen_state);
V = 1.0 + c * X;
} while (V <= 0.0);
V = V * V * V;
U = next_double(bitgen_state);
if (U < 1.0 - 0.0331 * (X * X) * (X * X))
return (b * V);
/* log(0.0) ok here */
if (log(U) < 0.5 * X * X + b * (1. - V + log(V)))
return (b * V);
}
}
}
float random_standard_gamma_f(bitgen_t *bitgen_state,
float shape) {
float b, c;
float U, V, X, Y;
if (shape == 1.0f) {
return random_standard_exponential_f(bitgen_state);
} else if (shape == 0.0) {
return 0.0;
} else if (shape < 1.0f) {
for (;;) {
U = next_float(bitgen_state);
V = random_standard_exponential_f(bitgen_state);
if (U <= 1.0f - shape) {
X = powf(U, 1.0f / shape);
if (X <= V) {
return X;
}
} else {
Y = -logf((1.0f - U) / shape);
X = powf(1.0f - shape + shape * Y, 1.0f / shape);
if (X <= (V + Y)) {
return X;
}
}
}
} else {
b = shape - 1.0f / 3.0f;
c = 1.0f / sqrtf(9.0f * b);
for (;;) {
do {
X = random_standard_normal_f(bitgen_state);
V = 1.0f + c * X;
} while (V <= 0.0f);
V = V * V * V;
U = next_float(bitgen_state);
if (U < 1.0f - 0.0331f * (X * X) * (X * X))
return (b * V);
/* logf(0.0) ok here */
if (logf(U) < 0.5f * X * X + b * (1.0f - V + logf(V)))
return (b * V);
}
}
}
int64_t random_positive_int64(bitgen_t *bitgen_state) {
return next_uint64(bitgen_state) >> 1;
}
int32_t random_positive_int32(bitgen_t *bitgen_state) {
return next_uint32(bitgen_state) >> 1;
}
int64_t random_positive_int(bitgen_t *bitgen_state) {
#if ULONG_MAX <= 0xffffffffUL
return (int64_t)(next_uint32(bitgen_state) >> 1);
#else
return (int64_t)(next_uint64(bitgen_state) >> 1);
#endif
}
uint64_t random_uint(bitgen_t *bitgen_state) {
#if ULONG_MAX <= 0xffffffffUL
return next_uint32(bitgen_state);
#else
return next_uint64(bitgen_state);
#endif
}
/*
* log-gamma function to support some of these distributions. The
* algorithm comes from SPECFUN by Shanjie Zhang and Jianming Jin and their
* book "Computation of Special Functions", 1996, John Wiley & Sons, Inc.
*
* If random_loggam(k+1) is being used to compute log(k!) for an integer k, consider
* using logfactorial(k) instead.
*/
double random_loggam(double x) {
double x0, x2, lg2pi, gl, gl0;
RAND_INT_TYPE k, n;
static double a[10] = {8.333333333333333e-02, -2.777777777777778e-03,
7.936507936507937e-04, -5.952380952380952e-04,
8.417508417508418e-04, -1.917526917526918e-03,
6.410256410256410e-03, -2.955065359477124e-02,
1.796443723688307e-01, -1.39243221690590e+00};
if ((x == 1.0) || (x == 2.0)) {
return 0.0;
} else if (x < 7.0) {
n = (RAND_INT_TYPE)(7 - x);
} else {
n = 0;
}
x0 = x + n;
x2 = (1.0 / x0) * (1.0 / x0);
/* log(2 * M_PI) */
lg2pi = 1.8378770664093453e+00;
gl0 = a[9];
for (k = 8; k >= 0; k--) {
gl0 *= x2;
gl0 += a[k];
}
gl = gl0 / x0 + 0.5 * lg2pi + (x0 - 0.5) * log(x0) - x0;
if (x < 7.0) {
for (k = 1; k <= n; k++) {
gl -= log(x0 - 1.0);
x0 -= 1.0;
}
}
return gl;
}
/*
double random_normal(bitgen_t *bitgen_state, double loc, double scale) {
return loc + scale * random_gauss(bitgen_state);
}
*/
double random_normal(bitgen_t *bitgen_state, double loc, double scale) {
return loc + scale * random_standard_normal(bitgen_state);
}
double random_exponential(bitgen_t *bitgen_state, double scale) {
return scale * random_standard_exponential(bitgen_state);
}
double random_uniform(bitgen_t *bitgen_state, double lower, double range) {
return lower + range * next_double(bitgen_state);
}
double random_gamma(bitgen_t *bitgen_state, double shape, double scale) {
return scale * random_standard_gamma(bitgen_state, shape);
}
float random_gamma_f(bitgen_t *bitgen_state, float shape, float scale) {
return scale * random_standard_gamma_f(bitgen_state, shape);
}
double random_beta(bitgen_t *bitgen_state, double a, double b) {
double Ga, Gb;
if ((a <= 1.0) && (b <= 1.0)) {
double U, V, X, Y, XpY;
/* Use Johnk's algorithm */
while (1) {
U = next_double(bitgen_state);
V = next_double(bitgen_state);
X = pow(U, 1.0 / a);
Y = pow(V, 1.0 / b);
XpY = X + Y;
/* Reject if both U and V are 0.0, which is approx 1 in 10^106 */
if ((XpY <= 1.0) && (XpY > 0.0)) {
if (X + Y > 0) {
return X / XpY;
} else {
double logX = log(U) / a;
double logY = log(V) / b;
double logM = logX > logY ? logX : logY;
logX -= logM;
logY -= logM;
return exp(logX - log(exp(logX) + exp(logY)));
}
}
}
} else {
Ga = random_standard_gamma(bitgen_state, a);
Gb = random_standard_gamma(bitgen_state, b);
return Ga / (Ga + Gb);
}
}
double random_chisquare(bitgen_t *bitgen_state, double df) {
return 2.0 * random_standard_gamma(bitgen_state, df / 2.0);
}
double random_f(bitgen_t *bitgen_state, double dfnum, double dfden) {
return ((random_chisquare(bitgen_state, dfnum) * dfden) /
(random_chisquare(bitgen_state, dfden) * dfnum));
}
double random_standard_cauchy(bitgen_t *bitgen_state) {
return random_standard_normal(bitgen_state) / random_standard_normal(bitgen_state);
}
double random_pareto(bitgen_t *bitgen_state, double a) {
return expm1(random_standard_exponential(bitgen_state) / a);
}
double random_weibull(bitgen_t *bitgen_state, double a) {
if (a == 0.0) {
return 0.0;
}
return pow(random_standard_exponential(bitgen_state), 1. / a);
}
double random_power(bitgen_t *bitgen_state, double a) {
return pow(-expm1(-random_standard_exponential(bitgen_state)), 1. / a);
}
double random_laplace(bitgen_t *bitgen_state, double loc, double scale) {
double U;
U = next_double(bitgen_state);
if (U >= 0.5) {
U = loc - scale * log(2.0 - U - U);
} else if (U > 0.0) {
U = loc + scale * log(U + U);
} else {
/* Reject U == 0.0 and call again to get next value */
U = random_laplace(bitgen_state, loc, scale);
}
return U;
}
double random_gumbel(bitgen_t *bitgen_state, double loc, double scale) {
double U;
U = 1.0 - next_double(bitgen_state);
if (U < 1.0) {
return loc - scale * log(-log(U));
}
/* Reject U == 1.0 and call again to get next value */
return random_gumbel(bitgen_state, loc, scale);
}
double random_logistic(bitgen_t *bitgen_state, double loc, double scale) {
double U;
U = next_double(bitgen_state);
if (U > 0.0) {
return loc + scale * log(U / (1.0 - U));
}
/* Reject U == 0.0 and call again to get next value */
return random_logistic(bitgen_state, loc, scale);
}
double random_lognormal(bitgen_t *bitgen_state, double mean, double sigma) {
return exp(random_normal(bitgen_state, mean, sigma));
}
double random_rayleigh(bitgen_t *bitgen_state, double mode) {
return mode * sqrt(2.0 * random_standard_exponential(bitgen_state));
}
double random_standard_t(bitgen_t *bitgen_state, double df) {
double num, denom;
num = random_standard_normal(bitgen_state);
denom = random_standard_gamma(bitgen_state, df / 2);
return sqrt(df / 2) * num / sqrt(denom);
}
static RAND_INT_TYPE random_poisson_mult(bitgen_t *bitgen_state, double lam) {
RAND_INT_TYPE X;
double prod, U, enlam;
enlam = exp(-lam);
X = 0;
prod = 1.0;
while (1) {
U = next_double(bitgen_state);
prod *= U;
if (prod > enlam) {
X += 1;
} else {
return X;
}
}
}
/*
* The transformed rejection method for generating Poisson random variables
* W. Hoermann
* Insurance: Mathematics and Economics 12, 39-45 (1993)
*/
#define LS2PI 0.91893853320467267
#define TWELFTH 0.083333333333333333333333
static RAND_INT_TYPE random_poisson_ptrs(bitgen_t *bitgen_state, double lam) {
RAND_INT_TYPE k;
double U, V, slam, loglam, a, b, invalpha, vr, us;
slam = sqrt(lam);
loglam = log(lam);
b = 0.931 + 2.53 * slam;
a = -0.059 + 0.02483 * b;
invalpha = 1.1239 + 1.1328 / (b - 3.4);
vr = 0.9277 - 3.6224 / (b - 2);
while (1) {
U = next_double(bitgen_state) - 0.5;
V = next_double(bitgen_state);
us = 0.5 - fabs(U);
k = (RAND_INT_TYPE)floor((2 * a / us + b) * U + lam + 0.43);
if ((us >= 0.07) && (V <= vr)) {
return k;
}
if ((k < 0) || ((us < 0.013) && (V > us))) {
continue;
}
/* log(V) == log(0.0) ok here */
/* if U==0.0 so that us==0.0, log is ok since always returns */
if ((log(V) + log(invalpha) - log(a / (us * us) + b)) <=
(-lam + k * loglam - random_loggam(k + 1))) {
return k;
}
}
}
RAND_INT_TYPE random_poisson(bitgen_t *bitgen_state, double lam) {
if (lam >= 10) {
return random_poisson_ptrs(bitgen_state, lam);
} else if (lam == 0) {
return 0;
} else {
return random_poisson_mult(bitgen_state, lam);
}
}
RAND_INT_TYPE random_negative_binomial(bitgen_t *bitgen_state, double n,
double p) {
double Y = random_gamma(bitgen_state, n, (1 - p) / p);
return random_poisson(bitgen_state, Y);
}
RAND_INT_TYPE random_binomial_btpe(bitgen_t *bitgen_state, RAND_INT_TYPE n,
double p, binomial_t *binomial) {
double r, q, fm, p1, xm, xl, xr, c, laml, lamr, p2, p3, p4;
double a, u, v, s, F, rho, t, A, nrq, x1, x2, f1, f2, z, z2, w, w2, x;
RAND_INT_TYPE m, y, k, i;
if (!(binomial->has_binomial) || (binomial->nsave != n) ||
(binomial->psave != p)) {
/* initialize */
binomial->nsave = n;
binomial->psave = p;
binomial->has_binomial = 1;
binomial->r = r = MIN(p, 1.0 - p);
binomial->q = q = 1.0 - r;
binomial->fm = fm = n * r + r;
binomial->m = m = (RAND_INT_TYPE)floor(binomial->fm);
binomial->p1 = p1 = floor(2.195 * sqrt(n * r * q) - 4.6 * q) + 0.5;
binomial->xm = xm = m + 0.5;
binomial->xl = xl = xm - p1;
binomial->xr = xr = xm + p1;
binomial->c = c = 0.134 + 20.5 / (15.3 + m);
a = (fm - xl) / (fm - xl * r);
binomial->laml = laml = a * (1.0 + a / 2.0);
a = (xr - fm) / (xr * q);
binomial->lamr = lamr = a * (1.0 + a / 2.0);
binomial->p2 = p2 = p1 * (1.0 + 2.0 * c);
binomial->p3 = p3 = p2 + c / laml;
binomial->p4 = p4 = p3 + c / lamr;
} else {
r = binomial->r;
q = binomial->q;
fm = binomial->fm;
m = binomial->m;
p1 = binomial->p1;
xm = binomial->xm;
xl = binomial->xl;
xr = binomial->xr;
c = binomial->c;
laml = binomial->laml;
lamr = binomial->lamr;
p2 = binomial->p2;
p3 = binomial->p3;
p4 = binomial->p4;
}
/* sigh ... */
Step10:
nrq = n * r * q;
u = next_double(bitgen_state) * p4;
v = next_double(bitgen_state);
if (u > p1)
goto Step20;
y = (RAND_INT_TYPE)floor(xm - p1 * v + u);
goto Step60;
Step20:
if (u > p2)
goto Step30;
x = xl + (u - p1) / c;
v = v * c + 1.0 - fabs(m - x + 0.5) / p1;
if (v > 1.0)
goto Step10;
y = (RAND_INT_TYPE)floor(x);
goto Step50;
Step30:
if (u > p3)
goto Step40;
y = (RAND_INT_TYPE)floor(xl + log(v) / laml);
/* Reject if v==0.0 since previous cast is undefined */
if ((y < 0) || (v == 0.0))
goto Step10;
v = v * (u - p2) * laml;
goto Step50;
Step40:
y = (RAND_INT_TYPE)floor(xr - log(v) / lamr);
/* Reject if v==0.0 since previous cast is undefined */
if ((y > n) || (v == 0.0))
goto Step10;
v = v * (u - p3) * lamr;
Step50:
k = llabs(y - m);
if ((k > 20) && (k < ((nrq) / 2.0 - 1)))
goto Step52;
s = r / q;
a = s * (n + 1);
F = 1.0;
if (m < y) {
for (i = m + 1; i <= y; i++) {
F *= (a / i - s);
}
} else if (m > y) {
for (i = y + 1; i <= m; i++) {
F /= (a / i - s);
}
}
if (v > F)
goto Step10;
goto Step60;
Step52:
rho =
(k / (nrq)) * ((k * (k / 3.0 + 0.625) + 0.16666666666666666) / nrq + 0.5);
t = -k * k / (2 * nrq);
/* log(0.0) ok here */
A = log(v);
if (A < (t - rho))
goto Step60;
if (A > (t + rho))
goto Step10;
x1 = y + 1;
f1 = m + 1;
z = n + 1 - m;
w = n - y + 1;
x2 = x1 * x1;
f2 = f1 * f1;
z2 = z * z;
w2 = w * w;
if (A > (xm * log(f1 / x1) + (n - m + 0.5) * log(z / w) +
(y - m) * log(w * r / (x1 * q)) +
(13680. - (462. - (132. - (99. - 140. / f2) / f2) / f2) / f2) / f1 /
166320. +
(13680. - (462. - (132. - (99. - 140. / z2) / z2) / z2) / z2) / z /
166320. +
(13680. - (462. - (132. - (99. - 140. / x2) / x2) / x2) / x2) / x1 /
166320. +
(13680. - (462. - (132. - (99. - 140. / w2) / w2) / w2) / w2) / w /
166320.)) {
goto Step10;
}
Step60:
if (p > 0.5) {
y = n - y;
}
return y;
}
RAND_INT_TYPE random_binomial_inversion(bitgen_t *bitgen_state, RAND_INT_TYPE n,
double p, binomial_t *binomial) {
double q, qn, np, px, U;
RAND_INT_TYPE X, bound;
if (!(binomial->has_binomial) || (binomial->nsave != n) ||
(binomial->psave != p)) {
binomial->nsave = n;
binomial->psave = p;
binomial->has_binomial = 1;
binomial->q = q = 1.0 - p;
binomial->r = qn = exp(n * log(q));
binomial->c = np = n * p;
binomial->m = bound = (RAND_INT_TYPE)MIN(n, np + 10.0 * sqrt(np * q + 1));
} else {
q = binomial->q;
qn = binomial->r;
np = binomial->c;
bound = binomial->m;
}
X = 0;
px = qn;
U = next_double(bitgen_state);
while (U > px) {
X++;
if (X > bound) {
X = 0;
px = qn;
U = next_double(bitgen_state);
} else {
U -= px;
px = ((n - X + 1) * p * px) / (X * q);
}
}
return X;
}
int64_t random_binomial(bitgen_t *bitgen_state, double p, int64_t n,
binomial_t *binomial) {
double q;
if ((n == 0LL) || (p == 0.0f))
return 0;
if (p <= 0.5) {
if (p * n <= 30.0) {
return random_binomial_inversion(bitgen_state, n, p, binomial);
} else {
return random_binomial_btpe(bitgen_state, n, p, binomial);
}
} else {
q = 1.0 - p;
if (q * n <= 30.0) {
return n - random_binomial_inversion(bitgen_state, n, q, binomial);
} else {
return n - random_binomial_btpe(bitgen_state, n, q, binomial);
}
}
}
double random_noncentral_chisquare(bitgen_t *bitgen_state, double df,
double nonc) {
if (npy_isnan(nonc)) {
return NPY_NAN;
}
if (nonc == 0) {
return random_chisquare(bitgen_state, df);
}
if (1 < df) {
const double Chi2 = random_chisquare(bitgen_state, df - 1);
const double n = random_standard_normal(bitgen_state) + sqrt(nonc);
return Chi2 + n * n;
} else {
const RAND_INT_TYPE i = random_poisson(bitgen_state, nonc / 2.0);
return random_chisquare(bitgen_state, df + 2 * i);
}
}
double random_noncentral_f(bitgen_t *bitgen_state, double dfnum, double dfden,
double nonc) {
double t = random_noncentral_chisquare(bitgen_state, dfnum, nonc) * dfden;
return t / (random_chisquare(bitgen_state, dfden) * dfnum);
}
double random_wald(bitgen_t *bitgen_state, double mean, double scale) {
double U, X, Y;
double mu_2l;
mu_2l = mean / (2 * scale);
Y = random_standard_normal(bitgen_state);
Y = mean * Y * Y;
X = mean + mu_2l * (Y - sqrt(4 * scale * Y + Y * Y));
U = next_double(bitgen_state);
if (U <= mean / (mean + X)) {
return X;
} else {
return mean * mean / X;
}
}
double random_vonmises(bitgen_t *bitgen_state, double mu, double kappa) {
double s;
double U, V, W, Y, Z;
double result, mod;
int neg;
if (npy_isnan(kappa)) {
return NPY_NAN;
}
if (kappa < 1e-8) {
/* Use a uniform for very small values of kappa */
return M_PI * (2 * next_double(bitgen_state) - 1);
} else {
/* with double precision rho is zero until 1.4e-8 */
if (kappa < 1e-5) {
/*
* second order taylor expansion around kappa = 0
* precise until relatively large kappas as second order is 0
*/
s = (1. / kappa + kappa);
} else {
if (kappa <= 1e6) {
/* Path for 1e-5 <= kappa <= 1e6 */
double r = 1 + sqrt(1 + 4 * kappa * kappa);
double rho = (r - sqrt(2 * r)) / (2 * kappa);
s = (1 + rho * rho) / (2 * rho);
} else {
/* Fallback to wrapped normal distribution for kappa > 1e6 */
result = mu + sqrt(1. / kappa) * random_standard_normal(bitgen_state);
/* Ensure result is within bounds */
if (result < -M_PI) {
result += 2*M_PI;
}
if (result > M_PI) {
result -= 2*M_PI;
}
return result;
}
}
while (1) {
U = next_double(bitgen_state);
Z = cos(M_PI * U);
W = (1 + s * Z) / (s + Z);
Y = kappa * (s - W);
V = next_double(bitgen_state);
/*
* V==0.0 is ok here since Y >= 0 always leads
* to accept, while Y < 0 always rejects
*/
if ((Y * (2 - Y) - V >= 0) || (log(Y / V) + 1 - Y >= 0)) {
break;
}
}
U = next_double(bitgen_state);
result = acos(W);
if (U < 0.5) {
result = -result;
}
result += mu;
neg = (result < 0);
mod = fabs(result);
mod = (fmod(mod + M_PI, 2 * M_PI) - M_PI);
if (neg) {
mod *= -1;
}
return mod;
}
}
int64_t random_logseries(bitgen_t *bitgen_state, double p) {
double q, r, U, V;
int64_t result;
r = npy_log1p(-p);
while (1) {
V = next_double(bitgen_state);
if (V >= p) {
return 1;
}
U = next_double(bitgen_state);
q = -expm1(r * U);
if (V <= q * q) {
result = (int64_t)floor(1 + log(V) / log(q));
if ((result < 1) || (V == 0.0)) {
continue;
} else {
return result;
}
}
if (V >= q) {
return 1;
}
return 2;
}
}
/*
* RAND_INT_TYPE is used to share integer generators with RandomState which
* used long in place of int64_t. If changing a distribution that uses
* RAND_INT_TYPE, then the original unmodified copy must be retained for
* use in RandomState by copying to the legacy distributions source file.
*/
/* Still used but both generator and mtrand via legacy_random_geometric */
RAND_INT_TYPE random_geometric_search(bitgen_t *bitgen_state, double p) {
double U;
RAND_INT_TYPE X;
double sum, prod, q;
X = 1;
sum = prod = p;
q = 1.0 - p;
U = next_double(bitgen_state);
while (U > sum) {
prod *= q;
sum += prod;
X++;
}
return X;
}
int64_t random_geometric_inversion(bitgen_t *bitgen_state, double p) {
return (int64_t)ceil(-random_standard_exponential(bitgen_state) / npy_log1p(-p));
}
int64_t random_geometric(bitgen_t *bitgen_state, double p) {
if (p >= 0.333333333333333333333333) {
return random_geometric_search(bitgen_state, p);
} else {
return random_geometric_inversion(bitgen_state, p);
}
}
RAND_INT_TYPE random_zipf(bitgen_t *bitgen_state, double a) {
double am1, b;
am1 = a - 1.0;
b = pow(2.0, am1);
while (1) {
double T, U, V, X;
U = 1.0 - next_double(bitgen_state);
V = next_double(bitgen_state);
X = floor(pow(U, -1.0 / am1));
/*
* The real result may be above what can be represented in a signed
* long. Since this is a straightforward rejection algorithm, we can
* just reject this value. This function then models a Zipf
* distribution truncated to sys.maxint.
*/
if (X > (double)RAND_INT_MAX || X < 1.0) {
continue;
}
T = pow(1.0 + 1.0 / X, am1);
if (V * X * (T - 1.0) / (b - 1.0) <= T / b) {
return (RAND_INT_TYPE)X;
}
}
}