-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
_sampling.py
1314 lines (1103 loc) · 45.3 KB
/
_sampling.py
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
import math
import numbers
import numpy as np
from scipy import stats
from scipy import special as sc
from ._qmc import (check_random_state as check_random_state_qmc,
Halton, QMCEngine)
from ._unuran.unuran_wrapper import NumericalInversePolynomial
from scipy._lib._util import check_random_state
__all__ = ['FastGeneratorInversion', 'RatioUniforms']
# define pdfs and other helper functions to create the generators
def argus_pdf(x, chi):
# approach follows Baumgarten/Hoermann: Generating ARGUS random variates
# for chi > 5, use relationship of the ARGUS distribution to Gamma(1.5)
if chi <= 5:
y = 1 - x * x
return x * math.sqrt(y) * math.exp(-0.5 * chi**2 * y)
return math.sqrt(x) * math.exp(-x)
def argus_gamma_trf(x, chi):
if chi <= 5:
return x
return np.sqrt(1.0 - 2 * x / chi**2)
def argus_gamma_inv_trf(x, chi):
if chi <= 5:
return x
return 0.5 * chi**2 * (1 - x**2)
def betaprime_pdf(x, a, b):
if x > 0:
logf = (a - 1) * math.log(x) - (a + b) * math.log1p(x) - sc.betaln(a, b)
return math.exp(logf)
else:
# return pdf at x == 0 separately to avoid runtime warnings
if a > 1:
return 0
elif a < 1:
return np.inf
else:
return 1 / sc.beta(a, b)
def beta_valid_params(a, b):
return (min(a, b) >= 0.1) and (max(a, b) <= 700)
def gamma_pdf(x, a):
if x > 0:
return math.exp(-math.lgamma(a) + (a - 1.0) * math.log(x) - x)
else:
return 0 if a >= 1 else np.inf
def invgamma_pdf(x, a):
if x > 0:
return math.exp(-(a + 1.0) * math.log(x) - math.lgamma(a) - 1 / x)
else:
return 0 if a >= 1 else np.inf
def burr_pdf(x, cc, dd):
# note: we use np.exp instead of math.exp, otherwise an overflow
# error can occur in the setup, e.g., for parameters
# 1.89128135, 0.30195177, see test test_burr_overflow
if x > 0:
lx = math.log(x)
return np.exp(-(cc + 1) * lx - (dd + 1) * math.log1p(np.exp(-cc * lx)))
else:
return 0
def burr12_pdf(x, cc, dd):
if x > 0:
lx = math.log(x)
logterm = math.log1p(math.exp(cc * lx))
return math.exp((cc - 1) * lx - (dd + 1) * logterm + math.log(cc * dd))
else:
return 0
def chi_pdf(x, a):
if x > 0:
return math.exp(
(a - 1) * math.log(x)
- 0.5 * (x * x)
- (a / 2 - 1) * math.log(2)
- math.lgamma(0.5 * a)
)
else:
return 0 if a >= 1 else np.inf
def chi2_pdf(x, df):
if x > 0:
return math.exp(
(df / 2 - 1) * math.log(x)
- 0.5 * x
- (df / 2) * math.log(2)
- math.lgamma(0.5 * df)
)
else:
return 0 if df >= 1 else np.inf
def alpha_pdf(x, a):
if x > 0:
return math.exp(-2.0 * math.log(x) - 0.5 * (a - 1.0 / x) ** 2)
return 0.0
def bradford_pdf(x, c):
if 0 <= x <= 1:
return 1.0 / (1.0 + c * x)
return 0.0
def crystalball_pdf(x, b, m):
if x > -b:
return math.exp(-0.5 * x * x)
return math.exp(m * math.log(m / b) - 0.5 * b * b - m * math.log(m / b - b - x))
def weibull_min_pdf(x, c):
if x > 0:
return c * math.exp((c - 1) * math.log(x) - x**c)
return 0.0
def weibull_max_pdf(x, c):
if x < 0:
return c * math.exp((c - 1) * math.log(-x) - ((-x) ** c))
return 0.0
def invweibull_pdf(x, c):
if x > 0:
return c * math.exp(-(c + 1) * math.log(x) - x ** (-c))
return 0.0
def wald_pdf(x):
if x > 0:
return math.exp(-((x - 1) ** 2) / (2 * x)) / math.sqrt(x**3)
return 0.0
def geninvgauss_mode(p, b):
if p > 1: # equivalent mode formulas numerical more stable versions
return (math.sqrt((1 - p) ** 2 + b**2) - (1 - p)) / b
return b / (math.sqrt((1 - p) ** 2 + b**2) + (1 - p))
def geninvgauss_pdf(x, p, b):
m = geninvgauss_mode(p, b)
lfm = (p - 1) * math.log(m) - 0.5 * b * (m + 1 / m)
if x > 0:
return math.exp((p - 1) * math.log(x) - 0.5 * b * (x + 1 / x) - lfm)
return 0.0
def invgauss_mode(mu):
return 1.0 / (math.sqrt(1.5 * 1.5 + 1 / (mu * mu)) + 1.5)
def invgauss_pdf(x, mu):
m = invgauss_mode(mu)
lfm = -1.5 * math.log(m) - (m - mu) ** 2 / (2 * m * mu**2)
if x > 0:
return math.exp(-1.5 * math.log(x) - (x - mu) ** 2 / (2 * x * mu**2) - lfm)
return 0.0
def powerlaw_pdf(x, a):
if x > 0:
return x ** (a - 1)
return 0.0
# Define a dictionary: for a given distribution (keys), another dictionary
# (values) specifies the parameters for NumericalInversePolynomial (PINV).
# The keys of the latter dictionary are:
# - pdf: the pdf of the distribution (callable). The signature of the pdf
# is float -> float (i.e., the function does not have to be vectorized).
# If possible, functions like log or exp from the module math should be
# preferred over functions from numpy since the PINV setup will be faster
# in that case.
# - check_pinv_params: callable f that returns true if the shape parameters
# (args) are recommended parameters for PINV (i.e., the u-error does
# not exceed the default tolerance)
# - center: scalar if the center does not depend on args, otherwise
# callable that returns the center as a function of the shape parameters
# - rvs_transform: a callable that can be used to transform the rvs that
# are distributed according to the pdf to the target distribution
# (as an example, see the entry for the beta distribution)
# - rvs_transform_inv: the inverse of rvs_transform (it is required
# for the transformed ppf)
# - mirror_uniform: boolean or a callable that returns true or false
# depending on the shape parameters. If True, the ppf is applied
# to 1-u instead of u to generate rvs, where u is a uniform rv.
# While both u and 1-u are uniform, it can be required to use 1-u
# to compute the u-error correctly. This is only relevant for the argus
# distribution.
# The only required keys are "pdf" and "check_pinv_params".
# All other keys are optional.
PINV_CONFIG = {
"alpha": {
"pdf": alpha_pdf,
"check_pinv_params": lambda a: 1.0e-11 <= a < 2.1e5,
"center": lambda a: 0.25 * (math.sqrt(a * a + 8.0) - a),
},
"anglit": {
"pdf": lambda x: math.cos(2 * x) + 1.0e-13,
# +1.e-13 is necessary, otherwise PINV has strange problems as
# f(upper border) is very close to 0
"center": 0,
},
"argus": {
"pdf": argus_pdf,
"center": lambda chi: 0.7 if chi <= 5 else 0.5,
"check_pinv_params": lambda chi: 1e-20 < chi < 901,
"rvs_transform": argus_gamma_trf,
"rvs_transform_inv": argus_gamma_inv_trf,
"mirror_uniform": lambda chi: chi > 5,
},
"beta": {
"pdf": betaprime_pdf,
"center": lambda a, b: max(0.1, (a - 1) / (b + 1)),
"check_pinv_params": beta_valid_params,
"rvs_transform": lambda x, *args: x / (1 + x),
"rvs_transform_inv": lambda x, *args: x / (1 - x) if x < 1 else np.inf,
},
"betaprime": {
"pdf": betaprime_pdf,
"center": lambda a, b: max(0.1, (a - 1) / (b + 1)),
"check_pinv_params": beta_valid_params,
},
"bradford": {
"pdf": bradford_pdf,
"check_pinv_params": lambda a: 1.0e-6 <= a <= 1e9,
"center": 0.5,
},
"burr": {
"pdf": burr_pdf,
"center": lambda a, b: (2 ** (1 / b) - 1) ** (-1 / a),
"check_pinv_params": lambda a, b: (min(a, b) >= 0.3) and (max(a, b) <= 50),
},
"burr12": {
"pdf": burr12_pdf,
"center": lambda a, b: (2 ** (1 / b) - 1) ** (1 / a),
"check_pinv_params": lambda a, b: (min(a, b) >= 0.2) and (max(a, b) <= 50),
},
"cauchy": {
"pdf": lambda x: 1 / (1 + (x * x)),
"center": 0,
},
"chi": {
"pdf": chi_pdf,
"check_pinv_params": lambda df: 0.05 <= df <= 1.0e6,
"center": lambda a: math.sqrt(a),
},
"chi2": {
"pdf": chi2_pdf,
"check_pinv_params": lambda df: 0.07 <= df <= 1e6,
"center": lambda a: a,
},
"cosine": {
"pdf": lambda x: 1 + math.cos(x),
"center": 0,
},
"crystalball": {
"pdf": crystalball_pdf,
"check_pinv_params": lambda b, m: (0.01 <= b <= 5.5)
and (1.1 <= m <= 75.1),
"center": 0.0,
},
"expon": {
"pdf": lambda x: math.exp(-x),
"center": 1.0,
},
"gamma": {
"pdf": gamma_pdf,
"check_pinv_params": lambda a: 0.04 <= a <= 1e6,
"center": lambda a: a,
},
"gennorm": {
"pdf": lambda x, b: math.exp(-abs(x) ** b),
"check_pinv_params": lambda b: 0.081 <= b <= 45.0,
"center": 0.0,
},
"geninvgauss": {
"pdf": geninvgauss_pdf,
"check_pinv_params": lambda p, b: (abs(p) <= 1200.0)
and (1.0e-10 <= b <= 1200.0),
"center": geninvgauss_mode,
},
"gumbel_l": {
"pdf": lambda x: math.exp(x - math.exp(x)),
"center": -0.6,
},
"gumbel_r": {
"pdf": lambda x: math.exp(-x - math.exp(-x)),
"center": 0.6,
},
"hypsecant": {
"pdf": lambda x: 1.0 / (math.exp(x) + math.exp(-x)),
"center": 0.0,
},
"invgamma": {
"pdf": invgamma_pdf,
"check_pinv_params": lambda a: 0.04 <= a <= 1e6,
"center": lambda a: 1 / a,
},
"invgauss": {
"pdf": invgauss_pdf,
"check_pinv_params": lambda mu: 1.0e-10 <= mu <= 1.0e9,
"center": invgauss_mode,
},
"invweibull": {
"pdf": invweibull_pdf,
"check_pinv_params": lambda a: 0.12 <= a <= 512,
"center": 1.0,
},
"laplace": {
"pdf": lambda x: math.exp(-abs(x)),
"center": 0.0,
},
"logistic": {
"pdf": lambda x: math.exp(-x) / (1 + math.exp(-x)) ** 2,
"center": 0.0,
},
"maxwell": {
"pdf": lambda x: x * x * math.exp(-0.5 * x * x),
"center": 1.41421,
},
"moyal": {
"pdf": lambda x: math.exp(-(x + math.exp(-x)) / 2),
"center": 1.2,
},
"norm": {
"pdf": lambda x: math.exp(-x * x / 2),
"center": 0.0,
},
"pareto": {
"pdf": lambda x, b: x ** -(b + 1),
"center": lambda b: b / (b - 1) if b > 2 else 1.5,
"check_pinv_params": lambda b: 0.08 <= b <= 400000,
},
"powerlaw": {
"pdf": powerlaw_pdf,
"center": 1.0,
"check_pinv_params": lambda a: 0.06 <= a <= 1.0e5,
},
"t": {
"pdf": lambda x, df: (1 + x * x / df) ** (-0.5 * (df + 1)),
"check_pinv_params": lambda a: 0.07 <= a <= 1e6,
"center": 0.0,
},
"rayleigh": {
"pdf": lambda x: x * math.exp(-0.5 * (x * x)),
"center": 1.0,
},
"semicircular": {
"pdf": lambda x: math.sqrt(1.0 - (x * x)),
"center": 0,
},
"wald": {
"pdf": wald_pdf,
"center": 1.0,
},
"weibull_max": {
"pdf": weibull_max_pdf,
"check_pinv_params": lambda a: 0.25 <= a <= 512,
"center": -1.0,
},
"weibull_min": {
"pdf": weibull_min_pdf,
"check_pinv_params": lambda a: 0.25 <= a <= 512,
"center": 1.0,
},
}
def _validate_qmc_input(qmc_engine, d, seed):
# Input validation for `qmc_engine` and `d`
# Error messages for invalid `d` are raised by QMCEngine
# we could probably use a stats.qmc.check_qrandom_state
if isinstance(qmc_engine, QMCEngine):
if d is not None and qmc_engine.d != d:
message = "`d` must be consistent with dimension of `qmc_engine`."
raise ValueError(message)
d = qmc_engine.d if d is None else d
elif qmc_engine is None:
d = 1 if d is None else d
qmc_engine = Halton(d, seed=seed)
else:
message = (
"`qmc_engine` must be an instance of "
"`scipy.stats.qmc.QMCEngine` or `None`."
)
raise ValueError(message)
return qmc_engine, d
class CustomDistPINV:
def __init__(self, pdf, args):
self._pdf = lambda x: pdf(x, *args)
def pdf(self, x):
return self._pdf(x)
class FastGeneratorInversion:
"""
Fast sampling by numerical inversion of the CDF for a large class of
continuous distributions in `scipy.stats`.
Parameters
----------
dist : rv_frozen object
Frozen distribution object from `scipy.stats`. The list of supported
distributions can be found in the Notes section. The shape parameters,
`loc` and `scale` used to create the distributions must be scalars.
For example, for the Gamma distribution with shape parameter `p`,
`p` has to be a float, and for the beta distribution with shape
parameters (a, b), both a and b have to be floats.
domain : tuple of floats, optional
If one wishes to sample from a truncated/conditional distribution,
the domain has to be specified.
The default is None. In that case, the random variates are not
truncated, and the domain is inferred from the support of the
distribution.
ignore_shape_range : boolean, optional.
If False, shape parameters that are outside of the valid range
of values to ensure that the numerical accuracy (see Notes) is
high, raise a ValueError. If True, any shape parameters that are valid
for the distribution are accepted. This can be useful for testing.
The default is False.
random_state : {None, int, `numpy.random.Generator`,
`numpy.random.RandomState`}, optional
A NumPy random number generator or seed for the underlying NumPy
random number generator used to generate the stream of uniform
random numbers.
If `random_state` is None, it uses ``self.random_state``.
If `random_state` is an int,
``np.random.default_rng(random_state)`` is used.
If `random_state` is already a ``Generator`` or ``RandomState``
instance then that instance is used.
Attributes
----------
loc : float
The location parameter.
random_state : {`numpy.random.Generator`, `numpy.random.RandomState`}
The random state used in relevant methods like `rvs` (unless
another `random_state` is passed as an argument to these methods).
scale : float
The scale parameter.
Methods
-------
cdf
evaluate_error
ppf
qrvs
rvs
support
Notes
-----
The class creates an object for continuous distributions specified
by `dist`. The method `rvs` uses a generator from
`scipy.stats.sampling` that is created when the object is instantiated.
In addition, the methods `qrvs` and `ppf` are added.
`qrvs` generate samples based on quasi-random numbers from
`scipy.stats.qmc`. `ppf` is the PPF based on the
numerical inversion method in [1]_ (`NumericalInversePolynomial`) that is
used to generate random variates.
Supported distributions (`distname`) are:
``alpha``, ``anglit``, ``argus``, ``beta``, ``betaprime``, ``bradford``,
``burr``, ``burr12``, ``cauchy``, ``chi``, ``chi2``, ``cosine``,
``crystalball``, ``expon``, ``gamma``, ``gennorm``, ``geninvgauss``,
``gumbel_l``, ``gumbel_r``, ``hypsecant``, ``invgamma``, ``invgauss``,
``invweibull``, ``laplace``, ``logistic``, ``maxwell``, ``moyal``,
``norm``, ``pareto``, ``powerlaw``, ``t``, ``rayleigh``, ``semicircular``,
``wald``, ``weibull_max``, ``weibull_min``.
`rvs` relies on the accuracy of the numerical inversion. If very extreme
shape parameters are used, the numerical inversion might not work. However,
for all implemented distributions, the admissible shape parameters have
been tested, and an error will be raised if the user supplies values
outside of the allowed range. The u-error should not exceed 1e-10 for all
valid parameters. Note that warnings might be raised even if parameters
are within the valid range when the object is instantiated.
To check numerical accuracy, the method `evaluate_error` can be used.
Note that all implemented distributions are also part of `scipy.stats`, and
the object created by `FastGeneratorInversion` relies on methods like
`ppf`, `cdf` and `pdf` from `rv_frozen`. The main benefit of using this
class can be summarized as follows: Once the generator to sample random
variates is created in the setup step, sampling and evaluation of
the PPF using `ppf` are very fast,
and performance is essentially independent of the distribution. Therefore,
a substantial speed-up can be achieved for many distributions if large
numbers of random variates are required. It is important to know that this
fast sampling is achieved by inversion of the CDF. Thus, one uniform
random variate is transformed into a non-uniform variate, which is an
advantage for several simulation methods, e.g., when
the variance reduction methods of common random variates or
antithetic variates are be used ([2]_).
In addition, inversion makes it possible to
- to use a QMC generator from `scipy.stats.qmc` (method `qrvs`),
- to generate random variates truncated to an interval. For example, if
one aims to sample standard normal random variates from
the interval (2, 4), this can be easily achieved by using the parameter
`domain`.
The location and scale that are initially defined by `dist`
can be reset without having to rerun the setup
step to create the generator that is used for sampling. The relation
of the distribution `Y` with `loc` and `scale` to the standard
distribution `X` (i.e., ``loc=0`` and ``scale=1``) is given by
``Y = loc + scale * X``.
References
----------
.. [1] Derflinger, Gerhard, Wolfgang Hörmann, and Josef Leydold.
"Random variate generation by numerical inversion when only the
density is known." ACM Transactions on Modeling and Computer
Simulation (TOMACS) 20.4 (2010): 1-25.
.. [2] Hörmann, Wolfgang, Josef Leydold and Gerhard Derflinger.
"Automatic nonuniform random number generation."
Springer, 2004.
Examples
--------
>>> import numpy as np
>>> from scipy import stats
>>> from scipy.stats.sampling import FastGeneratorInversion
Let's start with a simple example to illustrate the main features:
>>> gamma_frozen = stats.gamma(1.5)
>>> gamma_dist = FastGeneratorInversion(gamma_frozen)
>>> r = gamma_dist.rvs(size=1000)
The mean should be approximately equal to the shape parameter 1.5:
>>> r.mean()
1.52423591130436 # may vary
Similarly, we can draw a sample based on quasi-random numbers:
>>> r = gamma_dist.qrvs(size=1000)
>>> r.mean()
1.4996639255942914 # may vary
Compare the PPF against approximation `ppf`.
>>> q = [0.001, 0.2, 0.5, 0.8, 0.999]
>>> np.max(np.abs(gamma_frozen.ppf(q) - gamma_dist.ppf(q)))
4.313394796895409e-08
To confirm that the numerical inversion is accurate, we evaluate the
approximation error (u-error), which should be below 1e-10 (for more
details, refer to the documentation of `evaluate_error`):
>>> gamma_dist.evaluate_error()
(7.446320551265581e-11, nan) # may vary
Note that the location and scale can be changed without instantiating a
new generator:
>>> gamma_dist.loc = 2
>>> gamma_dist.scale = 3
>>> r = gamma_dist.rvs(size=1000)
The mean should be approximately 2 + 3*1.5 = 6.5.
>>> r.mean()
6.399549295242894 # may vary
Let us also illustrate how truncation can be applied:
>>> trunc_norm = FastGeneratorInversion(stats.norm(), domain=(3, 4))
>>> r = trunc_norm.rvs(size=1000)
>>> 3 < r.min() < r.max() < 4
True
Check the mean:
>>> r.mean()
3.250433367078603 # may vary
>>> stats.norm.expect(lb=3, ub=4, conditional=True)
3.260454285589997
In this particular, case, `scipy.stats.truncnorm` could also be used to
generate truncated normal random variates.
"""
def __init__(
self,
dist,
*,
domain=None,
ignore_shape_range=False,
random_state=None,
):
if isinstance(dist, stats.distributions.rv_frozen):
distname = dist.dist.name
if distname not in PINV_CONFIG.keys():
raise ValueError(
f"Distribution '{distname}' is not supported."
f"It must be one of {list(PINV_CONFIG.keys())}"
)
else:
raise ValueError("`dist` must be a frozen distribution object")
loc = dist.kwds.get("loc", 0)
scale = dist.kwds.get("scale", 1)
args = dist.args
if not np.isscalar(loc):
raise ValueError("loc must be scalar.")
if not np.isscalar(scale):
raise ValueError("scale must be scalar.")
self._frozendist = getattr(stats, distname)(
*args,
loc=loc,
scale=scale,
)
self._distname = distname
nargs = np.broadcast_arrays(args)[0].size
nargs_expected = self._frozendist.dist.numargs
if nargs != nargs_expected:
raise ValueError(
f"Each of the {nargs_expected} shape parameters must be a "
f"scalar, but {nargs} values are provided."
)
self.random_state = random_state
if domain is None:
self._domain = self._frozendist.support()
self._p_lower = 0.0
self._p_domain = 1.0
else:
self._domain = domain
self._p_lower = self._frozendist.cdf(self._domain[0])
_p_domain = self._frozendist.cdf(self._domain[1]) - self._p_lower
self._p_domain = _p_domain
self._set_domain_adj()
self._ignore_shape_range = ignore_shape_range
# the domain to be passed to NumericalInversePolynomial
# define a separate variable since in case of a transformation,
# domain_pinv will not be the same as self._domain
self._domain_pinv = self._domain
# get information about the distribution from the config to set up
# the generator
dist = self._process_config(distname, args)
if self._rvs_transform_inv is not None:
d0 = self._rvs_transform_inv(self._domain[0], *args)
d1 = self._rvs_transform_inv(self._domain[1], *args)
if d0 > d1:
# swap values if transformation if decreasing
d0, d1 = d1, d0
# only update _domain_pinv and not _domain
# _domain refers to the original distribution, _domain_pinv
# to the transformed distribution
self._domain_pinv = d0, d1
# self._center has been set by the call self._process_config
# check if self._center is inside the transformed domain
# _domain_pinv, otherwise move it to the endpoint that is closer
if self._center is not None:
if self._center < self._domain_pinv[0]:
self._center = self._domain_pinv[0]
elif self._center > self._domain_pinv[1]:
self._center = self._domain_pinv[1]
self._rng = NumericalInversePolynomial(
dist,
random_state=self.random_state,
domain=self._domain_pinv,
center=self._center,
)
@property
def random_state(self):
return self._random_state
@random_state.setter
def random_state(self, random_state):
self._random_state = check_random_state_qmc(random_state)
@property
def loc(self):
return self._frozendist.kwds.get("loc", 0)
@loc.setter
def loc(self, loc):
if not np.isscalar(loc):
raise ValueError("loc must be scalar.")
self._frozendist.kwds["loc"] = loc
# update the adjusted domain that depends on loc and scale
self._set_domain_adj()
@property
def scale(self):
return self._frozendist.kwds.get("scale", 0)
@scale.setter
def scale(self, scale):
if not np.isscalar(scale):
raise ValueError("scale must be scalar.")
self._frozendist.kwds["scale"] = scale
# update the adjusted domain that depends on loc and scale
self._set_domain_adj()
def _set_domain_adj(self):
""" Adjust the domain based on loc and scale. """
loc = self.loc
scale = self.scale
lb = self._domain[0] * scale + loc
ub = self._domain[1] * scale + loc
self._domain_adj = (lb, ub)
def _process_config(self, distname, args):
cfg = PINV_CONFIG[distname]
if "check_pinv_params" in cfg:
if not self._ignore_shape_range:
if not cfg["check_pinv_params"](*args):
msg = ("No generator is defined for the shape parameters "
f"{args}. Use ignore_shape_range to proceed "
"with the selected values.")
raise ValueError(msg)
if "center" in cfg.keys():
if not np.isscalar(cfg["center"]):
self._center = cfg["center"](*args)
else:
self._center = cfg["center"]
else:
self._center = None
self._rvs_transform = cfg.get("rvs_transform", None)
self._rvs_transform_inv = cfg.get("rvs_transform_inv", None)
_mirror_uniform = cfg.get("mirror_uniform", None)
if _mirror_uniform is None:
self._mirror_uniform = False
else:
self._mirror_uniform = _mirror_uniform(*args)
return CustomDistPINV(cfg["pdf"], args)
def rvs(self, size=None):
"""
Sample from the distribution by inversion.
Parameters
----------
size : int or tuple, optional
The shape of samples. Default is ``None`` in which case a scalar
sample is returned.
Returns
-------
rvs : array_like
A NumPy array of random variates.
Notes
-----
Random variates are generated by numerical inversion of the CDF, i.e.,
`ppf` computed by `NumericalInversePolynomial` when the class
is instantiated. Note that the
default ``rvs`` method of the rv_continuous class is
overwritten. Hence, a different stream of random numbers is generated
even if the same seed is used.
"""
# note: we cannot use self._rng.rvs directly in case
# self._mirror_uniform is true
u = self.random_state.uniform(size=size)
if self._mirror_uniform:
u = 1 - u
r = self._rng.ppf(u)
if self._rvs_transform is not None:
r = self._rvs_transform(r, *self._frozendist.args)
return self.loc + self.scale * r
def ppf(self, q):
"""
Very fast PPF (inverse CDF) of the distribution which
is a very close approximation of the exact PPF values.
Parameters
----------
u : array_like
Array with probabilities.
Returns
-------
ppf : array_like
Quantiles corresponding to the values in `u`.
Notes
-----
The evaluation of the PPF is very fast but it may have a large
relative error in the far tails. The numerical precision of the PPF
is controlled by the u-error, that is,
``max |u - CDF(PPF(u))|`` where the max is taken over points in
the interval [0,1], see `evaluate_error`.
Note that this PPF is designed to generate random samples.
"""
q = np.asarray(q)
if self._mirror_uniform:
x = self._rng.ppf(1 - q)
else:
x = self._rng.ppf(q)
if self._rvs_transform is not None:
x = self._rvs_transform(x, *self._frozendist.args)
return self.scale * x + self.loc
def qrvs(self, size=None, d=None, qmc_engine=None):
"""
Quasi-random variates of the given distribution.
The `qmc_engine` is used to draw uniform quasi-random variates, and
these are converted to quasi-random variates of the given distribution
using inverse transform sampling.
Parameters
----------
size : int, tuple of ints, or None; optional
Defines shape of random variates array. Default is ``None``.
d : int or None, optional
Defines dimension of uniform quasi-random variates to be
transformed. Default is ``None``.
qmc_engine : scipy.stats.qmc.QMCEngine(d=1), optional
Defines the object to use for drawing
quasi-random variates. Default is ``None``, which uses
`scipy.stats.qmc.Halton(1)`.
Returns
-------
rvs : ndarray or scalar
Quasi-random variates. See Notes for shape information.
Notes
-----
The shape of the output array depends on `size`, `d`, and `qmc_engine`.
The intent is for the interface to be natural, but the detailed rules
to achieve this are complicated.
- If `qmc_engine` is ``None``, a `scipy.stats.qmc.Halton` instance is
created with dimension `d`. If `d` is not provided, ``d=1``.
- If `qmc_engine` is not ``None`` and `d` is ``None``, `d` is
determined from the dimension of the `qmc_engine`.
- If `qmc_engine` is not ``None`` and `d` is not ``None`` but the
dimensions are inconsistent, a ``ValueError`` is raised.
- After `d` is determined according to the rules above, the output
shape is ``tuple_shape + d_shape``, where:
- ``tuple_shape = tuple()`` if `size` is ``None``,
- ``tuple_shape = (size,)`` if `size` is an ``int``,
- ``tuple_shape = size`` if `size` is a sequence,
- ``d_shape = tuple()`` if `d` is ``None`` or `d` is 1, and
- ``d_shape = (d,)`` if `d` is greater than 1.
The elements of the returned array are part of a low-discrepancy
sequence. If `d` is 1, this means that none of the samples are truly
independent. If `d` > 1, each slice ``rvs[..., i]`` will be of a
quasi-independent sequence; see `scipy.stats.qmc.QMCEngine` for
details. Note that when `d` > 1, the samples returned are still those
of the provided univariate distribution, not a multivariate
generalization of that distribution.
"""
qmc_engine, d = _validate_qmc_input(qmc_engine, d, self.random_state)
# mainly copied from unuran_wrapper.pyx.templ
# `rvs` is flexible about whether `size` is an int or tuple, so this
# should be, too.
try:
if size is None:
tuple_size = (1,)
else:
tuple_size = tuple(size)
except TypeError:
tuple_size = (size,)
# we do not use rng.qrvs directly since we need to be
# able to apply the ppf to 1 - u
N = 1 if size is None else np.prod(size)
u = qmc_engine.random(N)
if self._mirror_uniform:
u = 1 - u
qrvs = self._ppf(u)
if self._rvs_transform is not None:
qrvs = self._rvs_transform(qrvs, *self._frozendist.args)
if size is None:
qrvs = qrvs.squeeze()[()]
else:
if d == 1:
qrvs = qrvs.reshape(tuple_size)
else:
qrvs = qrvs.reshape(tuple_size + (d,))
return self.loc + self.scale * qrvs
def evaluate_error(self, size=100000, random_state=None, x_error=False):
"""
Evaluate the numerical accuracy of the inversion (u- and x-error).
Parameters
----------
size : int, optional
The number of random points over which the error is estimated.
Default is ``100000``.
random_state : {None, int, `numpy.random.Generator`,
`numpy.random.RandomState`}, optional
A NumPy random number generator or seed for the underlying NumPy
random number generator used to generate the stream of uniform
random numbers.
If `random_state` is None, use ``self.random_state``.
If `random_state` is an int,
``np.random.default_rng(random_state)`` is used.
If `random_state` is already a ``Generator`` or ``RandomState``
instance then that instance is used.
Returns
-------
u_error, x_error : tuple of floats
A NumPy array of random variates.
Notes
-----
The numerical precision of the inverse CDF `ppf` is controlled by
the u-error. It is computed as follows:
``max |u - CDF(PPF(u))|`` where the max is taken `size` random
points in the interval [0,1]. `random_state` determines the random
sample. Note that if `ppf` was exact, the u-error would be zero.
The x-error measures the direct distance between the exact PPF
and `ppf`. If ``x_error`` is set to ``True`, it is
computed as the maximum of the minimum of the relative and absolute
x-error:
``max(min(x_error_abs[i], x_error_rel[i]))`` where
``x_error_abs[i] = |PPF(u[i]) - PPF_fast(u[i])|``,
``x_error_rel[i] = max |(PPF(u[i]) - PPF_fast(u[i])) / PPF(u[i])|``.
Note that it is important to consider the relative x-error in the case
that ``PPF(u)`` is close to zero or very large.
By default, only the u-error is evaluated and the x-error is set to
``np.nan``. Note that the evaluation of the x-error will be very slow
if the implementation of the PPF is slow.
Further information about these error measures can be found in [1]_.
References
----------
.. [1] Derflinger, Gerhard, Wolfgang Hörmann, and Josef Leydold.
"Random variate generation by numerical inversion when only the
density is known." ACM Transactions on Modeling and Computer
Simulation (TOMACS) 20.4 (2010): 1-25.
Examples
--------
>>> import numpy as np
>>> from scipy import stats
>>> from scipy.stats.sampling import FastGeneratorInversion
Create an object for the normal distribution:
>>> d_norm_frozen = stats.norm()
>>> d_norm = FastGeneratorInversion(d_norm_frozen)
To confirm that the numerical inversion is accurate, we evaluate the
approximation error (u-error and x-error).
>>> u_error, x_error = d_norm.evaluate_error(x_error=True)