sampling-tree

Crates.iosampling-tree
lib.rssampling-tree
version0.1.0
sourcesrc
created_at2024-11-08 15:21:46.173009
updated_at2024-11-08 15:21:46.173009
descriptionA simple sampling tree implementation for sampling discrete distributions with sparse dynamic updates. This allows us to sample efficiently from a distribution given the relative importance of each datapoint. Construction time is O(n), updating is O(log(n)), and sampling is O(log(n)). The memory footprint is no more than twice the size of `n*std::mem::size_of::()` where `T` is weight datatype.
homepagehttps://github.com/BenJourdan/sampling-tree
repositoryhttps://github.com/BenJourdan/sampling-tree
max_upload_size
id1441279
size25,699
Ben Jourdan (BenJourdan)

documentation

README

sampling-tree

A simple sampling tree implementation for sampling discrete distributions with sparse dynamic updates. This allows us to sample efficiently from a distribution given the relative importance of each datapoint. Construction time is O(n), updating is O(log(n)), and sampling is O(log(n)). The memory footprint is no more than twice the size of n*std::mem::size_of::<T>() where T is weight datatype.

Basic usage:

    let mut rng = rand::thread_rng();
    let n = 100;
    let range = 10000u32;
    let data = (0..n).map(|_|{
        rng.gen_range(0..range)
    });
    let mut sampling_tree: SimpleSamplingTree<_> = SimpleSamplingTree::from_iterable(data).unwrap();
    println!("{:?}",sampling_tree);
    let sample_idx = sampling_tree.sample(&mut rng).unwrap();
    println!("{:?}, {:?}",sample_idx, sampling_tree.contribution(sample_idx).unwrap());
    sampling_tree.update(sample_idx,0).unwrap();
    println!("{:?}",sampling_tree);

Supports most numeric types for the type of each datapoint's weight.

Commit count: 12

cargo fmt