Crates.io | sampling-tree |
lib.rs | sampling-tree |
version | 0.1.0 |
source | src |
created_at | 2024-11-08 15:21:46.173009 |
updated_at | 2024-11-08 15:21:46.173009 |
description | 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:: |
homepage | https://github.com/BenJourdan/sampling-tree |
repository | https://github.com/BenJourdan/sampling-tree |
max_upload_size | |
id | 1441279 |
size | 25,699 |
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.