Expand description
§tpt-sci-ppl
Probabilistic programming for the tpt-science pillar: a small model/DSL
layer and a from-scratch NUTS (No-U-Turn Sampler) Hamiltonian Monte Carlo
backend.
The original spec.txt planned to wrap nuts-rs; per the project’s
“build our own internals” direction this crate implements NUTS directly on
top of the tpt-math substrate — reverse-mode gradients come from
tpt_math_autodiff_rev and randomness from tpt-math-prob — so there is
no external sampler dependency to audit or license-check.
Priors are expressed as reverse-mode differentiable closures (so the NUTS
gradient is exact). They can be written directly with the parameter
autodiff variable, or derived from a typed distribution in
tpt-math-prob-bayes (Gaussian, Beta, Gamma) via
ModelBuilder::gaussian_prior, ModelBuilder::beta_prior, and
ModelBuilder::gamma_prior. The likelihood is a second differentiable
closure over the parameters and the observed data.
§Example
use tpt_sci_ppl::ModelBuilder;
use tpt_math_prob_core::SplitMix64;
// mu ~ N(0, 5), data | mu ~ N(mu, 1)
let data = [2.0, 3.0, 2.5, 3.5, 3.0];
let mut m = ModelBuilder::new();
m.gaussian_parameter(0.0, 5.0);
m.set_data(data.to_vec());
m.likelihood(|t, v, d| {
let mut s = t.constant(0.0);
for &x in d.iter() {
let z = (v[0] - x) / 1.0;
s = s + (-0.5 * z * z);
}
s
});
let model = m.build().unwrap();
let mut rng = SplitMix64::seed_from_u64(1);
let trace = model.fit(&mut rng, 500).unwrap();
let samples = trace.combined_samples();
let mean: f64 = samples.iter().map(|s| s[0]).sum::<f64>() / samples.len() as f64;
assert!((mean - 2.8).abs() < 0.6);The same prior written from a typed Gaussian:
use tpt_sci_ppl::{ModelBuilder, Gaussian};
use tpt_math_prob_core::SplitMix64;
let data = [2.0, 3.0, 2.5, 3.5, 3.0];
let mut m = ModelBuilder::new();
m.gaussian_prior(Gaussian::new(0.0, 5.0));
m.set_data(data.to_vec());
m.likelihood(|t, v, d| {
let mut s = t.constant(0.0);
for &x in d.iter() {
let z = (v[0] - x) / 1.0;
s = s + (-0.5 * z * z);
}
s
});
let model = m.build().unwrap();
let mut rng = SplitMix64::seed_from_u64(1);
let _ = model.fit(&mut rng, 50).unwrap();Re-exports§
pub use error::PplError;pub use model::Model;pub use model::ModelBuilder;pub use nuts::NutsOptions;pub use nuts::Target;pub use nuts::nuts;pub use nuts::nuts_with_options;pub use trace::Trace;
Modules§
- error
- Errors raised while building or running a probabilistic-programming model.
- gradient
- Sampling from log-densities whose gradients come from outside the tape.
- model
- A small probabilistic-programming model/DSL built on top of the from-scratch
crate::nuts()sampler. - nuts
- NUTS (No-U-Turn Sampler) — a Hamiltonian Monte Carlo variant with an
adaptive trajectory length, built from scratch on top of
tpt_math_autodiff_rev(reverse-mode gradients) and thetpt-math-probRNG traits. - trace
- Convergence diagnostics and the return type of
crate::Model::fit.
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(μ, σ²). - Gradient
Tape - A tape that records every operation performed on its
Variables so that all partial derivatives can be recovered with a single backward pass. - Variable
- A scalar bound to a
GradientTape.
Traits§
- Rng
- A source of pseudo-random bits.