Skip to content

randint_constrained

([API reference][max_div.sampling.con.randint_constrained])

Command:

uv tool install max-div
max-div benchmark --markdown randint_constrained
or
uv run max-div benchmark --markdown randint_constrained

We compare the following situations: - use randint, i.e. we simply sample without taking constraints into account (this is max_div's numba-accelerated implementation) - use randint_constrained, i.e. we sample taking constraints into account

We test both speed (absolute time of a single call) and accuracy (% of cases the generated samples satisfied the constraints).

Scenario A

Varying n & k with 10 non-overlapping constraints spanning equal portions of the n-range

Uniform sampling.

Timing Results

k n m randint randint_constrained
(eager=False)
randint_constrained
(eager=True)
2 10 10 485.9 nsec ± 0.7% 1.133 μsec ± 0.6% 1.162 μsec ± 1.9%
4 10 10 483.7 nsec ± 0.5% 1.533 μsec ± 0.3% 1.542 μsec ± 0.3%
8 10 10 519.0 nsec ± 1.2% 2.495 μsec ± 0.4% 2.508 μsec ± 0.2%
2 100 10 522.0 nsec ± 0.3% 1.651 μsec ± 0.4% 1.652 μsec ± 0.8%
4 100 10 529.6 nsec ± 0.2% 2.522 μsec ± 0.6% 2.489 μsec ± 0.3%
8 100 10 538.2 nsec ± 0.5% 4.414 μsec ± 0.2% 4.384 μsec ± 0.1%
16 100 10 550.2 nsec ± 0.6% 8.351 μsec ± 0.2% 8.401 μsec ± 0.2%
32 100 10 587.7 nsec ± 0.4% 16.12 μsec ± 0.4% 16.22 μsec ± 0.3%
64 100 10 662.4 nsec ± 0.3% 32.06 μsec ± 0.5% 31.91 μsec ± 0.2%
2 1000 10 537.8 nsec ± 2.1% 4.779 μsec ± 0.6% 4.763 μsec ± 0.2%
4 1000 10 558.0 nsec ± 2.9% 8.830 μsec ± 0.3% 8.762 μsec ± 0.2%
8 1000 10 546.2 nsec ± 2.1% 17.50 μsec ± 0.3% 17.51 μsec ± 0.2%
16 1000 10 589.6 nsec ± 1.8% 34.91 μsec ± 0.1% 35.12 μsec ± 0.3%
32 1000 10 608.7 nsec ± 2.2% 69.33 μsec ± 0.8% 69.15 μsec ± 0.8%
64 1000 10 645.0 nsec ± 2.2% 142.9 μsec ± 0.7% 142.4 μsec ± 0.1%
128 1000 10 732.8 nsec ± 1.8% 288.9 μsec ± 0.8% 291.0 μsec ± 0.3%
256 1000 10 911.3 nsec ± 1.5% 595.5 μsec ± 0.3% 598.9 μsec ± 0.6%
Geomean: 581.2 nsec ± 1.3% 13.43 μsec ± 0.4% 13.45 μsec ± 0.4%

Accuracy Results

k n m randint randint_constrained
(eager=False)
randint_constrained
(eager=True)
2 10 10 100.0% 100.0% 100.0%
4 10 10 100.0% 100.0% 100.0%
8 10 10 100.0% 100.0% 100.0%
2 100 10 90.0% 100.0% 100.0%
4 100 10 54.0% 100.0% 100.0%
8 100 10 2.0% 100.0% 100.0%
16 100 10 1.0% 100.0% 100.0%
32 100 10 4.5% 100.0% 100.0%
64 100 10 13.5% 100.0% 100.0%
2 1000 10 90.5% 100.0% 100.0%
4 1000 10 45.5% 100.0% 100.0%
8 1000 10 2.0% 100.0% 100.0%
16 1000 10 1.0% 100.0% 100.0%
32 1000 10 1.0% 100.0% 100.0%
64 1000 10 0.5% 100.0% 100.0%
128 1000 10 0.5% 100.0% 100.0%
256 1000 10 0.5% 100.0% 100.0%
Mean: 35.68% 100.00% 100.00%

Non-uniform sampling (custom p).

Timing Results

k n m randint randint_constrained
(eager=False)
randint_constrained
(eager=True)
2 10 10 601.0 nsec ± 1.6% 1.132 μsec ± 1.2% 1.141 μsec ± 0.9%
4 10 10 612.3 nsec ± 0.4% 1.545 μsec ± 0.6% 1.531 μsec ± 0.2%
8 10 10 588.0 nsec ± 0.5% 2.405 μsec ± 0.6% 2.399 μsec ± 0.2%
2 100 10 875.7 nsec ± 0.6% 1.694 μsec ± 0.3% 1.690 μsec ± 0.1%
4 100 10 1.047 μsec ± 4.3% 2.545 μsec ± 0.1% 2.562 μsec ± 0.1%
8 100 10 1.346 μsec ± 2.0% 4.354 μsec ± 0.7% 4.526 μsec ± 0.1%
16 100 10 2.082 μsec ± 0.3% 8.351 μsec ± 2.1% 8.172 μsec ± 0.1%
32 100 10 2.192 μsec ± 0.3% 15.82 μsec ± 0.3% 16.06 μsec ± 2.7%
64 100 10 2.220 μsec ± 0.5% 33.49 μsec ± 1.8% 31.47 μsec ± 0.2%
2 1000 10 2.357 μsec ± 0.5% 5.458 μsec ± 3.1% 5.454 μsec ± 0.9%
4 1000 10 2.776 μsec ± 0.8% 9.345 μsec ± 0.4% 9.365 μsec ± 0.2%
8 1000 10 3.488 μsec ± 2.3% 19.02 μsec ± 1.4% 19.08 μsec ± 0.8%
16 1000 10 5.050 μsec ± 0.6% 37.32 μsec ± 0.7% 37.51 μsec ± 5.7%
32 1000 10 7.399 μsec ± 0.3% 70.06 μsec ± 0.2% 69.68 μsec ± 0.2%
64 1000 10 9.065 μsec ± 5.7% 140.9 μsec ± 0.3% 141.1 μsec ± 0.2%
128 1000 10 9.421 μsec ± 2.7% 287.3 μsec ± 0.2% 286.4 μsec ± 0.2%
256 1000 10 10.20 μsec ± 0.4% 580.7 μsec ± 0.2% 581.8 μsec ± 0.2%
Geomean: 2.348 μsec ± 1.4% 13.69 μsec ± 0.8% 13.67 μsec ± 0.8%

Accuracy Results

k n m randint randint_constrained
(eager=False)
randint_constrained
(eager=True)
2 10 10 100.0% 100.0% 100.0%
4 10 10 100.0% 100.0% 100.0%
8 10 10 100.0% 100.0% 100.0%
2 100 10 87.5% 100.0% 100.0%
4 100 10 42.0% 100.0% 100.0%
8 100 10 0.5% 100.0% 100.0%
16 100 10 0.5% 100.0% 100.0%
32 100 10 0.0% 100.0% 100.0%
64 100 10 0.0% 100.0% 100.0%
2 1000 10 84.5% 100.0% 100.0%
4 1000 10 43.0% 100.0% 100.0%
8 1000 10 0.5% 100.0% 100.0%
16 1000 10 0.0% 100.0% 100.0%
32 1000 10 0.0% 100.0% 100.0%
64 1000 10 0.0% 100.0% 100.0%
128 1000 10 0.0% 100.0% 100.0%
256 1000 10 0.0% 100.0% 100.0%
Mean: 32.85% 100.00% 100.00%

Scenario B

Fixed n=1000 & k=100 with varying number of constraints spanning random 1% portions of the n-range

Uniform sampling.

Timing Results

k n m randint randint_constrained
(eager=False)
randint_constrained
(eager=True)
100 1000 2 717.3 nsec ± 1.6% 160.9 μsec ± 0.7% 160.1 μsec ± 0.2%
100 1000 4 719.9 nsec ± 2.0% 162.6 μsec ± 0.2% 162.7 μsec ± 0.2%
100 1000 8 699.3 nsec ± 1.9% 166.4 μsec ± 0.4% 166.0 μsec ± 0.2%
100 1000 16 706.0 nsec ± 1.7% 174.7 μsec ± 0.6% 171.8 μsec ± 0.1%
100 1000 32 686.4 nsec ± 2.2% 193.9 μsec ± 0.2% 183.8 μsec ± 0.2%
100 1000 64 687.1 nsec ± 1.5% 236.6 μsec ± 0.3% 207.8 μsec ± 0.2%
100 1000 128 696.4 nsec ± 1.8% 311.4 μsec ± 0.3% 259.6 μsec ± 0.5%
100 1000 256 706.4 nsec ± 1.3% 423.3 μsec ± 0.5% 362.5 μsec ± 0.4%
100 1000 384 709.6 nsec ± 1.6% 548.4 μsec ± 0.4% 477.1 μsec ± 0.5%
100 1000 512 692.5 nsec ± 1.9% 690.6 μsec ± 0.8% 608.6 μsec ± 0.5%
100 1000 768 717.0 nsec ± 4.1% 970.6 μsec ± 0.3% 899.2 μsec ± 0.3%
100 1000 1024 708.2 nsec ± 2.0% 1.394 msec ± 0.4% 1.357 msec ± 0.3%
Geomean: 703.8 nsec ± 2.0% 339.6 μsec ± 0.4% 314.8 μsec ± 0.3%

Accuracy Results

k n m randint randint_constrained
(eager=False)
randint_constrained
(eager=True)
100 1000 2 0.0% 100.0% 100.0%
100 1000 4 0.0% 100.0% 100.0%
100 1000 8 0.0% 100.0% 100.0%
100 1000 16 0.0% 100.0% 100.0%
100 1000 32 0.0% 100.0% 100.0%
100 1000 64 0.0% 100.0% 100.0%
100 1000 128 0.0% 100.0% 100.0%
100 1000 256 0.0% 98.0% 100.0%
100 1000 384 0.0% 0.0% 100.0%
100 1000 512 0.0% 0.0% 50.0%
100 1000 768 0.0% 0.0% 0.0%
100 1000 1024 0.0% 0.0% 0.0%
Mean: 0.00% 66.50% 79.17%

Non-uniform sampling (custom p).

Timing Results

k n m randint_numba randint_constrained
(eager=False)
randint_constrained
(eager=True)
100 1000 2 9.460 μsec ± 5.2% 161.4 μsec ± 0.2% 161.5 μsec ± 0.2%
100 1000 4 9.262 μsec ± 0.2% 163.6 μsec ± 0.3% 164.2 μsec ± 0.2%
100 1000 8 9.234 μsec ± 0.3% 167.6 μsec ± 0.6% 167.2 μsec ± 0.3%
100 1000 16 9.251 μsec ± 5.5% 175.4 μsec ± 0.3% 172.2 μsec ± 0.1%
100 1000 32 9.289 μsec ± 5.2% 194.5 μsec ± 0.1% 184.4 μsec ± 0.5%
100 1000 64 9.211 μsec ± 0.3% 236.7 μsec ± 0.3% 210.7 μsec ± 0.1%
100 1000 128 9.188 μsec ± 0.2% 310.7 μsec ± 0.2% 260.2 μsec ± 0.3%
100 1000 256 10.24 μsec ± 4.9% 421.6 μsec ± 0.2% 363.7 μsec ± 0.1%
100 1000 384 9.248 μsec ± 0.5% 550.0 μsec ± 0.2% 476.9 μsec ± 0.1%
100 1000 512 9.217 μsec ± 0.5% 690.1 μsec ± 0.2% 605.5 μsec ± 0.2%
100 1000 768 9.471 μsec ± 1.3% 971.2 μsec ± 0.4% 900.7 μsec ± 0.5%
100 1000 1024 9.181 μsec ± 0.6% 1.402 msec ± 0.3% 1.350 msec ± 0.4%
Geomean: 9.350 μsec ± 2.0% 340.4 μsec ± 0.3% 315.9 μsec ± 0.3%

Accuracy Results

k n m randint_numba randint_constrained
(eager=False)
randint_constrained
(eager=True)
100 1000 2 0.0% 100.0% 100.0%
100 1000 4 0.0% 100.0% 100.0%
100 1000 8 0.0% 100.0% 100.0%
100 1000 16 0.0% 100.0% 100.0%
100 1000 32 0.0% 100.0% 100.0%
100 1000 64 0.0% 100.0% 100.0%
100 1000 128 0.0% 100.0% 100.0%
100 1000 256 0.0% 98.5% 100.0%
100 1000 384 0.0% 0.0% 100.0%
100 1000 512 0.0% 0.0% 44.0%
100 1000 768 0.0% 0.0% 0.0%
100 1000 1024 0.0% 0.0% 0.0%
Mean: 0.00% 66.54% 78.67%