Crates.io | vose-alias |
lib.rs | vose-alias |
version | 1.0.0 |
source | src |
created_at | 2020-10-27 14:35:37.804573 |
updated_at | 2020-10-27 14:35:37.804573 |
description | An implementation of the Vose Alias method to sample an element out of a list, given a discrete probablity distribution |
homepage | https://github.com/jrenoux/vose-alias |
repository | https://github.com/jrenoux/vose-alias |
max_upload_size | |
id | 306009 |
size | 52,743 |
A Rust implementation of the Vose-Alias Algorithm. This algorithm allows to sample an element from a list given a discrete probability distribution.
For a vecotr of n
elements, the initialization time is O(n)
, the sampling time is O(1)
, and the memory usage is O(n)
.
Add this to your Cargo.toml
[dependencies]
vose-alias = "1.0.0"
use vose_alias::VoseAlias
let va = VoseAlias::new(vec!["orange", "yellow", "green", "turquoise", "grey", "blue", "pink"], vec![0.125, 0.2, 0.1, 0.25, 0.1, 0.1, 0.125]);
let element = va.sample();
This crate provides a VoseAlias
structure, that stores the list of elements given by the user of the library, as well as the probability and alias tables. The probability and alias tables are created by the new
function.
The crate also provides a sample
function, that allows to sample an element from the element vector in constant time. The function returns the element directly.
For a description of the method implemented as well as the algorithm (in pseudo-code), see [[https://www.keithschwarz.com/darts-dice-coins/]]