Skip to main content

Crate tpt_math_prob_sampler

Crate tpt_math_prob_sampler 

Source
Expand description

Sampling strategies for the tpt-math-prob-* family.

This crate consolidates reusable sampling strategies on top of the shared tpt_math_prob_core traits (Distribution, Rng, Sampler). It is no_std-compatible; the bulk of the API is allocation-free and works with any slice-backed storage, while the convenience constructors that own a alloc::vec::Vec are gated behind the std (default) or alloc features.

The crate provides:

§Examples

use tpt_math_prob_sampler::{categorical, SystematicResampler, ResampleScheme};
use tpt_math_prob_core::{Distribution, Rng, Standard, SplitMix64};

let mut rng = SplitMix64::seed_from_u64(0);
let cat = categorical(&[1.0, 2.0, 3.0]).unwrap();
let x = cat.sample(&mut rng);
assert!(x < 3);

let resampler = SystematicResampler::new(cat.cdf().to_vec()).unwrap();
let mut out = [0usize; 5];
resampler.sample_indices(&mut rng, ResampleScheme::Systematic, &mut out);
assert_eq!(out.len(), 5);

Modules§

prelude
Convenient re-exports for sampling work.

Structs§

Bernoulli
Bernoulli (p success) distribution returning bool.
DensityFn
Adapter turning a closure Fn(f64) -> f64 into a Density.
InverseCdfSampler
Inverse-transform sampler for a categorical (discrete) distribution.
RejectionSampler
Standard rejection sampler.
ReservoirSampler
Streaming reservoir sampler: keep a uniformly random sample of size cap from a stream of k items of unknown length.
SplitMix64
SplitMix64: a tiny, fast, deterministic 64-bit PRNG.
Standard
The standard uniform distribution over the type’s natural range.
SystematicResampler
Resamples n particle indices from a weighted categorical distribution.
Uniform
Continuous uniform distribution over [lo, hi).

Enums§

ResampleScheme
The resampling scheme used by SystematicResampler::sample_indices.
SamplerError
Errors produced while constructing or validating a sampler.

Traits§

Density
A probability density (evaluated on the real line) for rejection sampling.
Distribution
A distribution that can produce samples of type T.
Rng
A source of pseudo-random bits.
Sampler
Bulk sampling for any Distribution.

Functions§

categorical
Build a WeightedIndex (owned, alloc-backed inverse-CDF sampler) from unnormalised weights.
sample_categorical
Draw a categorical index from weights using allocation-free linear scan (inverse-transform without a normalised CDF table).
shuffle
In-place Fisher–Yates shuffle of slice.
uniform_index
Draw a uniform usize in 0..n (unbiased, rejection-based).

Type Aliases§

WeightedIndex
Convenience alias for the owned, alloc backed inverse-CDF sampler.