Skip to main content

Crate tpt_math_prob_bayes

Crate tpt_math_prob_bayes 

Source
Expand description

Bayesian inference primitives.

Small, dependency-free building blocks for exact conjugate updates and for approximate inference when conjugacy runs out:

  • Beta — conjugate prior for a Bernoulli/Binomial success rate.
  • Gaussian (aliased Normal) — density, sampling, and the Normal–Normal conjugate update for an unknown mean with known variance.
  • Gamma — conjugate prior for a Poisson rate, and the engine behind Beta sampling.
  • Metropolis — a random-walk Metropolis(–Hastings) sampler for any unnormalised scalar log-target.
  • special — the log-gamma/log-beta/normal-CDF machinery underneath.
  • Free log-likelihood helpers such as bernoulli_log_likelihood, normal_log_likelihood, and log_sum_exp.

Every distribution samples through the Rng and Distribution traits from tpt-math-prob-core, so a deterministic generator such as SplitMix64 makes any inference run exactly reproducible.

§Conjugate updates

use tpt_math_prob_bayes::Beta;

// Uniform prior over a coin's bias, then 7 heads and 3 tails.
let posterior = Beta::uniform().update(7, 3);
assert_eq!((posterior.alpha(), posterior.beta()), (8.0, 4.0));
assert!((posterior.mean() - 2.0 / 3.0).abs() < 1e-12);

// Posterior belief with 95% probability mass.
let (lo, hi) = posterior.credible_interval(0.95);
assert!(lo < posterior.mean() && posterior.mean() < hi);

§Approximate inference

use tpt_math_prob_bayes::{Beta, Metropolis};
use tpt_math_prob_core::SplitMix64;

let prior = Beta::new(2.0, 2.0);
let (successes, failures) = (30, 70);

// Sample the same posterior with MCMC instead of the conjugate formula.
let target = move |p: f64| prior.log_unnormalized_posterior(p, successes, failures);
let mut sampler = Metropolis::with_gaussian_proposal(target, 0.1);
let mut rng = SplitMix64::seed_from_u64(2024);
let trace = sampler.run_with_burn_in(&mut rng, 0.5, 2_000, 40_000);

let mcmc_mean = trace.iter().sum::<f64>() / trace.len() as f64;
let exact_mean = prior.update(successes, failures).mean();
assert!((mcmc_mean - exact_mean).abs() < 0.01);

Modules§

adaptive
Adaptive random-walk Metropolis (Haario, Saksman and Tamminen, 2001).
diagnostics
Convergence diagnostics for MCMC output and self-normalised importance sampling.
hmc
Hamiltonian Monte Carlo with a leapfrog integrator and a unit mass matrix.
special
Special functions backing the conjugate-prior distributions.

Structs§

Beta
The Beta distribution Beta(α, β) on the closed unit interval.
Gamma
The Gamma distribution Gamma(k, β) in shape/rate parameterisation.
Gaussian
The Gaussian distribution N(μ, σ²).
Metropolis
A random-walk Metropolis sampler over a scalar state.

Enums§

ParameterError
An invalid distribution parameter.

Traits§

Distribution
The randomness traits this crate is built on, re-exported for convenience. A distribution that can produce samples of type T.
Rng
The randomness traits this crate is built on, re-exported for convenience. A source of pseudo-random bits.
Sampler
The randomness traits this crate is built on, re-exported for convenience. Bulk sampling for any Distribution.

Functions§

bernoulli_log_likelihood
Log-likelihood of successes and failures Bernoulli trials at rate p.
binomial_log_likelihood
Log-likelihood of observing successes out of trials at rate p.
exponential_log_likelihood
Log-likelihood of exponential data at rate rate.
log_sum_exp
Numerically stable ln Σ exp(xᵢ).
normal_log_likelihood
Log-likelihood of independent Gaussian data with mean mean and standard deviation std.
poisson_log_likelihood
Log-likelihood of Poisson counts at rate rate.

Type Aliases§

Normal
Alias for Gaussian, for callers who prefer the name Normal.