probsys / fast-loaded-dice-roller Goto Github PK
View Code? Open in Web Editor NEWA near-optimal exact sampler for discrete probability distributions
License: Apache License 2.0
A near-optimal exact sampler for discrete probability distributions
License: Apache License 2.0
See the analysis in https://issues.apache.org/jira/browse/RNG-179
While writing a code generator based on the Fast Loaded Dice Roller, I've noticed that two equivalent sets of integer weights (where one is an integer multiple of the other) can have a different table size. For example:
1, 9, 6
, turns into a 4x5 table by the Fast Loaded Dice Roller.1000, 9000, 6000
, turns into a 14x5 table this way.This shows that it might be helpful in some cases to divide the weights by their greatest common divisor (ignoring zero weights) to achieve an equivalent weight set that minimizes table sizes.
https://github.com/probcomp/fast-loaded-dice-roller/blob/2fb32e94a04c744c86aa4cb54b3ee0259894e777/src/c/fldr.c#L55
https://github.com/probcomp/fast-loaded-dice-roller/blob/2fb32e94a04c744c86aa4cb54b3ee0259894e777/src/c/fldr.c#L97-L98
Remove unneeded additions
https://github.com/probcomp/fast-loaded-dice-roller/blob/2fb32e94a04c744c86aa4cb54b3ee0259894e777/src/c/fldr.c#L57
https://github.com/probcomp/fast-loaded-dice-roller/blob/2fb32e94a04c744c86aa4cb54b3ee0259894e777/src/c/fldr.c#L65
Change bool to int
https://github.com/probcomp/fast-loaded-dice-roller/blob/2fb32e94a04c744c86aa4cb54b3ee0259894e777/src/c/fldr.c#L56
https://github.com/probcomp/fast-loaded-dice-roller/blob/2fb32e94a04c744c86aa4cb54b3ee0259894e777/src/c/fldr.c#L64
The main bottleneck in preprocessing is bit extraction from the a[i]. It might more efficient to compute the probability matrix in reverse order (i.e., with the least significant bit in the first column) and using integer division on the running values of a[i], instead of bit shifts/masking.
Using a uniform distribution with floating-point values seems to result in only 0 or 1 values while sampling.
from fldr import fldrf_preprocess
from fldr import fldr_sample
x = fldrf_preprocess([0.1 for _ in range(10)])
print([fldr_sample(x) for _ in range(20)])
[0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1]
fast-loaded-dice-roller/src/c/flip.c
Lines 14 to 25 in 19d30ab
rand()
returns a random 31-bit integer packed into a 32-bit integer (whose sign bit is always zero). The positions of the extracted bits should then be 0, ..., 30. However, by setting k=30
, flip_pos = k
, and flip_pos --
, we only ever extract bits the bits at locations 0, ..., 29.
Also the code can be made more general to determine k
automatically from limits.h
and RAND_MAX
.
Not sure if this is expected or if it's a bug.
python -m timeit -s "from fldr import fldr_preprocess; from fldr import fldr_sample; l = range(10000); x = fldr_preprocess([1 for _ in l])" "fldr_sample(x)"
50000 loops, best of 5: 6 usec per loop
python -m timeit -s "from fldr import fldrf_preprocess; from fldr import fldr_sample; l = range(10000); x = fldrf_preprocess([1/len(l) for _ in l])" "fldr_sample(x)"
50 loops, best of 5: 5.1 msec per loop
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.