Expand description
simsam — sample from custom discrete and continuous distributions.
Build a distribution from a PDF or CDF (closures, histograms, location-scale transforms,
or simsym symbolic expressions), then draw samples via inverse
transform sampling — similar to SciPy rv_continuous.
§Features
pdf,cdf,ppf,sf,isf,logpdf,logcdf,logsfmean,var,std,median,entropy,expect,interval- Fast sampling via
HermitePpfTable(BuildOptions::with_hermite) or TDR (BuildOptions::with_tdr) LocScale,Truncated,HistogramPdf
§Limitations
- Continuous distributions require finite support
[lo, hi](useTruncatedon a wide interval). - Inverse transform assumes a unimodal CDF on that interval.
§Example
use simsam::{from_pdf_fn, Interval};
let support = Interval::new(0.0, 1.0).unwrap();
let sampler = from_pdf_fn(|x| 3.0 * x * x, support).unwrap();
let x = sampler.sample().unwrap();
assert!((0.0..=1.0).contains(&x));
assert!((sampler.mean().unwrap() - 0.75).abs() < 1e-2);Structs§
- Affine
Cdf - Normalized CDF built from a user-provided
Cdf, affine-scaled to[0, 1]on support. - Build
Options - Options for building a continuous sampler from PDF or CDF.
- CdfFn
- Closure-backed CDF with explicit support.
- CdfMc
Estimator - Monte-Carlo estimator for normalization and CDF of an N-D PDF on a bounded support.
- CdfMc
Options - Conditional
Factor Sampler - Continuous
Sampler - Continuous distribution sampler.
- Discrete
Sampler - Finite discrete distribution sampler via inverse transform.
- DpdfFn
- Closure-backed dPDF with explicit support.
- Gaussian
Copula - Gaussian copula sampler.
- Gibbs
Options - Gibbs
Sampler Nd - Hermite
PpfTable - Precomputed inverse-CDF table with monotone cubic Hermite interpolation (fast
ppf). - Histogram
Pdf - Piecewise-constant PDF from a histogram (SciPy
rv_histogram). - HmcOptions
- HmcSampler
Nd - Hyper
Rect - Axis-aligned bounded support in R^n:
lo[i] <= x[i] <= hi[i]. - Integrated
Pdf - CDF obtained by integrating and normalizing a PDF on support.
- Interval
- Finite support interval
[lo, hi]withlo < hi. - Invert
Options - LocScale
- Location-scale transform:
Y = loc + scale * Xfor base PDF onbase_support. - Metropolis
Hastings Nd - Random-walk Metropolis–Hastings sampler on a bounded hyper-rectangle.
- MhOptions
- PdfFn
- Closure-backed PDF with explicit support.
- PdfNdFn
- Closure-backed ND PDF with explicit support.
- Rejection
Options - Rejection
Sampler Nd - Generic N-D rejection sampler on a bounded hyper-rectangle.
- TdrBuild
Config - TDR configuration used when
SampleMethod::Tdris selected. - TdrHat
- Built TDR hat function; immutable after construction.
- TdrOptions
- TdrSampler
- 1D Transformed Density Rejection sampler.
- Truncated
- Truncate a base PDF to
[lo, hi]and renormalize.
Enums§
- Build
Error - Error while constructing a distribution or sampler.
- PpfMethod
- How to invert the CDF when computing
ppf(inverse transform sampling path). - Sample
Error - Error during sampling (e.g. invalid quantile).
- Sample
Method - How to draw samples from a continuous distribution.
- TdrTransform
- Transformation parameter for TDR.
Traits§
- Cdf
- Cumulative distribution function, monotone on support with
cdf(lo) ≈ 0,cdf(hi) ≈ 1. - CdfDiscrete
- Discrete cumulative distribution at support index
k(inclusive). - CdfSource
- Internal CDF source for inverse-transform sampling.
- Conditional
Factorization - Conditional factorization
x0 ~ f0(),x1 ~ f1(x0), …,x_{n-1} ~ f_{n-1}(x0..x_{n-2}). - Conditional
Sampler - Trait for sampling coordinate
iconditional on other coordinates. - Dpdf
- Derivative of a probability density function
dpdf = d/dx pdf(x). - HasSupport
- Distribution support.
- HasSupport
Nd - LogPdf
Nd - Log-density (up to additive constant). Prefer this for MCMC.
- Probability density function on a finite support.
- PdfNd
- Joint probability density function on a bounded hyper-rectangle support.
- Pmf
- Probability mass function on a finite support.
Functions§
- default_
quad_ tol - from_
cdf_ fn - Build a sampler from a CDF closure on
[lo, hi]. - from_
histogram - Build from a histogram (
HistogramPdf). - from_
pdf_ dpdf_ fn - Build a sampler from PDF and explicit dPDF closures.
- from_
pdf_ fn - Build a sampler from a PDF closure on
[lo, hi]with default options. - from_
pdf_ fn_ with_ options - Build a sampler from a PDF closure with custom
BuildOptions. - from_
pdf_ loc_ scale - Build from a base PDF with
y = loc + scale * x. - tdr_
from_ fns - Convenience constructor from closures.