https://github.com/dnbaker/alias_sampler
https://github.com/dnbaker/alias_sampler
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/dnbaker/alias_sampler
- Owner: dnbaker
- License: agpl-3.0
- Created: 2019-12-12T03:55:41.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-02T17:01:30.000Z (almost 6 years ago)
- Last Synced: 2025-08-01T11:01:37.236Z (11 months ago)
- Language: C++
- Size: 50.8 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# alias_sampler 🎠[](https://travis-ci.com/dnbaker/alias_sampler)
Alias sampling, for sampling from discrete probability distributions in constant time.
libstdc++ uses binary search in a cumulative probability array, which both takes logarithmic time and pollutes the cache. This doesn't.
AliasSampler uses [fastmod](https://lemire.me/blog/2019/02/08/faster-remainders-when-the-divisor-is-a-constant-beating-compilers-and-libdivide/), while MaskedAliasSampler pads to a power of two with items at probability 0 and uses a bitmask.
These samplers provide single samples (`sampler.sample()` or `sampler()`), a batch of samples (`sampler.sample(1000)`/`sampler(1000)`),
or fills a range spanned by iterators with samples (`sampler.sample(vec.begin(), vec.end(), sampling_seed)`).
Similarly, uses blaze for vector multiplications if available, but not otherwise.
In order to make sampling threadsafe, pass `-DALIAS_THREADSAFE`, which will provide a separate RNG for each thread.