Code Monkey home page Code Monkey logo

fast-loaded-dice-roller's People

Contributors

fsaad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fast-loaded-dice-roller's Issues

Consider dividing integer weights by their GCD

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:

  • One weight set, 1, 9, 6, turns into a 4x5 table by the Fast Loaded Dice Roller.
  • However, an equivalent weight set, 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.

Implement various minor optimizations in fldr_preprocess

  1. Remove initialization of H to -1; instead, use calloc for initalizatio nto 0 and index the samples by 1, ..., n+1; also make sure to update the sampler to check if the sample is <= n, and return one minus

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

  1. 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

  2. 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.

Sampling issues for uniform distributions with floating-point values

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]

Skipping final bit in flip.c

static int k = 30;
static int flip_word = 0;
static int flip_pos = 0;
int flip(void){
if (flip_pos == 0) {
flip_word = rand();
flip_pos = k;
}
--flip_pos;
return (flip_word >> flip_pos) & 1;
}

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.

Sampling with floating-point distributions seems to be significantly slower

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.