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:
InverseCdfSampler— inverse-transform (alias) sampling over a weighted categorical distribution, generic over any CDF storage.SystematicResampler(with multinomial, systematic and stratified resampling schemes) — resamplenparticles from weighted possibilities, writing into a caller-provided index buffer.ReservoirSampler— streamingn-from-k(withkunknown) uniform reservoir sampling.RejectionSampler— standard rejection sampling that wraps a target density and a proposalDistribution+Densitywith a bounding constantm.- Free helpers:
categorical,sample_categorical,uniform_index,shuffle, and the smallUniform/Bernoullibuilding blocks.
§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 (
psuccess) distribution returningbool. - Density
Fn - Adapter turning a closure
Fn(f64) -> f64into aDensity. - Inverse
CdfSampler - Inverse-transform sampler for a categorical (discrete) distribution.
- Rejection
Sampler - Standard rejection sampler.
- Reservoir
Sampler - Streaming reservoir sampler: keep a uniformly random sample of size
capfrom a stream ofkitems of unknown length. - Split
Mix64 SplitMix64: a tiny, fast, deterministic 64-bit PRNG.- Standard
- The standard uniform distribution over the type’s natural range.
- Systematic
Resampler - Resamples
nparticle indices from a weighted categorical distribution. - Uniform
- Continuous uniform distribution over
[lo, hi).
Enums§
- Resample
Scheme - The resampling scheme used by
SystematicResampler::sample_indices. - Sampler
Error - 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 unnormalisedweights. - sample_
categorical - Draw a categorical index from
weightsusing allocation-free linear scan (inverse-transform without a normalised CDF table). - shuffle
- In-place Fisher–Yates shuffle of
slice. - uniform_
index - Draw a uniform
usizein0..n(unbiased, rejection-based).
Type Aliases§
- Weighted
Index - Convenience alias for the owned,
allocbacked inverse-CDF sampler.