Visitar URL original
Replace BG with faster BINV that uses only one call to random() by rhettinger · Pull Request #94886 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20250607232237/https://github.com/python/cpython/pull/94886
Skip to content

Replace BG with faster BINV that uses only one call to random() #94886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

rhettinger
Copy link
Contributor

@rhettinger rhettinger commented Jul 15, 2022 •

The BINV algorithm is faster and uses only one call to random(), but I am concerned about numerical issues with the initial q**n calculation, round-off errors in r *= (a / k) - s and the accumulation of errors in sum_r += r.

For example, with n=322580 and p=0.000031, the cumulative sum hits a maximum of 0.9999999999989545 at k=46. The random value u can be larger than that, leaving BINV trapped in the loop until k=322580.

Using only one call to random() is nice but is that enough information to be able to subdivide the inverse distribution with a small p and large n?

The version in the paper (with u -= r instead of sum_r and without the bounds test) gets stuck in an infinite loop with Binv(n=100, p=0.01) and u >= 0.9999999999999993 (0x1.ffffffffffffap-1) the sixth largest possible value of random().

This technique, used in the original paper, likely has better
numerical characteristics on the long tail of the distribution.
This substracts to small values rather subtracting a small from
a large.
@rhettinger
Copy link
Contributor Author

rhettinger commented Jul 16, 2022 •

Performance

The BINV method only uses one call to random(). This is much better than the BG method where the expected number of calls is n * p, occasionally going up to double that number.

With the new guards for correctness, the timings come out only slightly in favor of BINV.

Bg Geometric Method

./python.exe -m timeit -r 11 -s 'from random import binomialvariate as B' 'B(190, 0.01)'
1000000 loops, best of 11: 343 nsec per loop

% ./python.exe -m timeit -r 11 -s 'from random import binomialvariate as B' 'B(190, 0.1)'
500000 loops, best of 11: 829 nsec per loop

Binv Inverse Transform Method

./python.exe -m timeit -r 11 -s 'from random import binomialvariate as B' 'B(190, 0.01)'
1000000 loops, best of 11: 325 nsec per loop

% ./python.exe -m timeit -r 11 -s 'from random import binomialvariate as B' 'B(190, 0.1)'
500000 loops, best of 11: 824 nsec per loop

Correctness

The BINV distributions are all as expected. There may be an issue with a rare extreme value spilling to one higher than expected — the cause likely being that the no progress test should return k - 1 instead of k where the maximum value was first encountered rather that when it was detected.


n=0	p=0.5	times=131072
[131072] actual
[131072] expected

n=1	p=0.5	times=131072
[65753, 65319] actual
[65536, 65536] expected

n=1	p=0.1	times=131072
[118020, 13052] actual
[117965, 13107] expected

n=1	p=0.9	times=131072
[13190, 117882] actual
[13107, 117965] expected

n=1	p=0.0	times=131072
[131072] actual
[131072] expected

n=1	p=1.0	times=131072
[0, 131072] actual
[0, 131072] expected

n=2	p=0.5	times=131072
[33054, 65332, 32686] actual
[32768, 65536, 32768] expected

n=3	p=0.5	times=131072
[16352, 49135, 49215, 16370] actual
[16384, 49152, 49152, 16384] expected

n=4	p=0.5	times=131072
[8243, 32682, 49034, 32932, 8181] actual
[8192, 32768, 49152, 32768, 8192] expected

n=2	p=0.25	times=131072
[73620, 49278, 8174] actual
[73728, 49152, 8192] expected

n=3	p=0.25	times=131072
[54982, 55502, 18518, 2070] actual
[55296, 55296, 18432, 2048] expected

n=4	p=0.25	times=131072
[41104, 55344, 27908, 6165, 551] actual
[41472, 55296, 27648, 6144, 512] expected

n=2	p=0.75	times=131072
[8243, 48840, 73989] actual
[8192, 49152, 73728] expected

n=3	p=0.75	times=131072
[2028, 18278, 55269, 55497] actual
[2048, 18432, 55296, 55296] expected

n=4	p=0.75	times=131072
[511, 6232, 27625, 55273, 41431] actual
[512, 6144, 27648, 55296, 41472] expected

n=99	p=0.1	times=8388608
[251, 2684, 14913, 53345, 142043, 299459, 522857, 771481, 984668, 1105292, 1105806, 995771, 809901, 601729, 410683, 259068, 151066, 81636, 41430, 19807, 8796, 3670, 1400, 542, 208, 65, 27, 8, 1, 1] actual
[248, 2723, 14827, 53266, 142043, 299870, 521995, 770564, 984610, 1106167, 1106167, 994433, 810278, 602515, 411240, 258929, 151042, 81938, 41475, 19646, 8732, 3650, 1438, 535, 188, 63, 20, 6, 2] expected

n=990	p=0.01	times=8388608
[448, 4034, 19919, 66902, 166172, 329997, 547210, 778925, 968106, 1064295, 1054544, 946867, 781791, 593603, 417874, 275543, 168693, 97693, 53440, 27854, 13530, 6337, 2830, 1250, 476, 168, 67, 30, 7, 2, 0, 1] actual
[400, 4004, 20002, 66537, 165839, 330337, 547782, 777803, 965379, 1063975, 1054303, 948776, 781862, 594142, 418814, 275261, 169432, 98055, 53539, 27666, 13568, 6330, 2816, 1197, 487, 190, 71, 26, 9, 3, 1] expected

n=9900	p=0.001	times=8388608
[412, 4146, 20350, 67373, 168478, 334165, 549283, 777086, 962247, 1061147, 1049756, 944154, 779876, 594730, 418140, 276670, 171300, 99921, 54859, 28596, 14173, 6643, 2892, 1366, 519, 200, 83, 29, 10, 3, 0, 1] actual
[419, 4150, 20563, 67912, 168201, 333236, 550112, 778322, 963456, 1060005, 1049501, 944541, 779160, 593234, 419370, 276669, 171101, 99580, 54729, 28494, 14091, 6636, 2983, 1282, 528, 209, 79, 29, 10, 3, 1] expected

n=99000	p=0.0001	times=8388608
[444, 4091, 20663, 67760, 168640, 333438, 549555, 780782, 963859, 1060933, 1047509, 943804, 777435, 593349, 418988, 276580, 171226, 100082, 54939, 28576, 14078, 6719, 2993, 1285, 533, 214, 73, 34, 17, 8, 1] actual
[421, 4165, 20620, 68050, 168437, 333524, 550342, 778372, 963264, 1059610, 1049024, 944120, 778891, 593143, 419423, 276808, 171266, 99731, 54848, 28576, 14144, 6667, 3000, 1291, 533, 211, 80, 29, 10, 4, 1] expected

n=990000	p=1e-05	times=8388608
[466, 4247, 20758, 68231, 168163, 333059, 550431, 778202, 963711, 1060485, 1049867, 943207, 777982, 593856, 419776, 276616, 171469, 99326, 54168, 28625, 14163, 6609, 3037, 1250, 552, 229, 84, 27, 8, 3, 1] actual
[421, 4167, 20625, 68064, 168460, 333553, 550365, 778377, 963244, 1059571, 1048976, 944078, 778864, 593134, 419429, 276822, 171283, 99746, 54860, 28585, 14149, 6670, 3002, 1292, 533, 211, 80, 29, 10, 4, 1] expected

@rhettinger rhettinger closed this Jul 16, 2022
@rhettinger rhettinger reopened this Jul 23, 2022
@rhettinger rhettinger requested review from tim-one and mdickinson July 23, 2022 22:28
@rhettinger
Copy link
Contributor Author

rhettinger commented Jul 23, 2022 •

When I get a chance, I'll look at Numpy, Julia, and Tensorflow to see what they are doing here.

The BINV algorithm is a straightforward idea, recommended by the referenced paper, and nicely only uses one call to random. But from a numerical point of view BG method seems less prone to numerical issues, particularly with regard to loss of precision in q ** n, to pms computation with a recurrence relation, to the deaccumulation of u, and to the progress monitoring (which still needs work).

Ideally, we want this to generate the same random selections across different builds when given the same seed. In that regard, BINV mostly uses basic operations with 1/2 ulp guarantees, but the q ** n step relies on the math library's pow() function that can vary in quality. IIRC, the log() call used by BG also doesn't have guarantees but none-the-less tends to be more accurate and consistent across builds than pow(). See this note for GCC.

Once log() was replaced with log2() in BG, it now wins on speed. It already was winning on quality – it has none of the cumulative numeric issues and it doesn't hit a point where no progress can be made deaccumulating u while k is still far short of n.

The only winning points for BINV are using only one call to random() and having the possibility that the r values could be precomputed for generating many binomial variates at the same time (something the current API doesn't support).

@rhettinger rhettinger closed this Jul 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants