github ↗
docs menu

norn

scientific computing · like NumPyro / Stan

Probabilistic inference for OCaml, built on Nx, Rune and Jera. Norn turns a log density over a structure of your own type into draws and diagnostics. A model written as one generative function gives that density; samplers run many chains at once; draws and their diagnostics come back as values of your structure, so (Norn.Diag.rhat params draws).loc is the R-hat of loc.

install
opam install norn
built on
nx · rune · jera

Quick start

ocaml
module D = Norn.Dist
module M = Norn_model

(* The unknowns, as a record of your own. *)
type 'a params = { loc : 'a; scale : 'a }

module Params = struct
  type ('a, _) t = 'a params

  let walk c { loc; scale } =
    let open Nx.Ptree.Walk in
    let loc = field c "loc" leaf loc in
    let scale = field c "scale" leaf scale in
    { loc; scale }
end

let params : (Nx.host Nx.float64_t params, Nx.host) Nx.Ptree.t =
  Nx.Ptree.instantiate (module Params)

let s x = Nx.scalar Nx.float64 x
let y = Nx.create Nx.float64 [| 6 |] [| 4.1; 5.3; 3.8; 6.0; 4.9; 5.5 |]

(* The model draws every random variable and returns them. *)
let model =
  M.v Nx.float64 params Nx.Ptree.tensor @@ fun () ->
  let loc = M.sample (D.normal ~loc:(s 0.) ~scale:(s 10.)) in
  let scale = M.sample (D.half_normal ~scale:(s 5.)) in
  let y = M.sample (D.iid [| 6 |] (D.normal ~loc ~scale)) in
  ({ loc; scale }, y)

let () =
  let u = M.coords model and lp = M.log_density model y in
  let key = Nx.Rng.key 0 in
  let state = Norn.Nuts.init u lp (M.init model y ~chains:4 key) in
  let state = Norn.Nuts.warmup u lp key ~steps:500 state in
  let _, coords, stats = Norn.Nuts.sample u lp key ~draws:500 state in
  let draws = Norn.Draws.map u params (M.constrain model) coords in
  Format.printf "%a@." Norn.Summary.pp (Norn.Summary.v params ~stats draws)

The examples cover each part on small problems.

Libraries

  • norn holds distributions, bijectors, samplers, draws and diagnostics, over densities written as plain functions.
  • norn.model turns a generative function into those densities, simulates data from it, and maps draws back to values.

What's inside

  • Densities: a density maps a position, your structure with a leading chain axis, to one log density per chain. Norn.with_gradient states a density's gradient when rune cannot compute it, such as an adjoint solver's.
  • Distributions: Dist has continuous, discrete and vector families (normal to Student's t, gamma, beta, von Mises, Dirichlet, multivariate normal, LKJ Cholesky factors, Poisson, negative binomial, categorical), with iid, sorted, transform, truncated, mixture and hmm, whose states are summed out. Parameters are tensors and broadcast; a parameter outside its domain raises naming its index and value.
  • Supports and bijectors: Support names each family's set of values, and Bij maps unconstrained coordinates onto it with its log-determinant: exp, greater, less, interval, simplex, ordered, cholesky_corr, sum_to_zero and their compositions.
  • Samplers: Nuts runs the No-U-Turn sampler with a step size and geometry per chain, tuned in warmup; Hmc shares one step size, a tuned trajectory length and one geometry across chains; Ensemble uses the stretch move and needs no gradient. Each evaluates the density on many chains at once.
  • Evidence: Nested (nested sampling with slice moves) and Smc (tempered sequential Monte Carlo with Hamiltonian or slice moves) estimate the log evidence with a standard error, and give weighted posterior draws (Weighted).
  • Gaussians: Gaussian is diagonal plus low rank over your structure, built from a covariance or a precision: a Laplace approximation, or a sampler's geometry.
  • Draws and diagnostics: Draws holds draws with [chain; draw] axes; Diag computes rank-normalised split R-hat, nested R-hat, bulk and tail effective sample sizes, Monte Carlo errors, E-BFMI, divergence locations, simulation-based calibration ranks with an exact uniformity test, Pareto smoothed importance weights with their k-hat, and leave-one-out cross-validation over pointwise likelihoods. Summary tabulates draws or weighted draws and lists its findings as data.
  • Models: Norn_model.v reads one function that draws every random variable with sample. From it come the posterior, prior and likelihood densities over coordinates, starting points, prior draws, simulation, prediction, pointwise likelihoods, and the reparam, noncentre and fix transformations.

The samplers are formulas of tensor operations: a whole run, warmup included, compiles under Rune.jit as one program whose key is an argument.

Examples

  • 01-distributions ↗A distribution describes one whole tensor value.
  • 02-bijectors ↗A bijector maps unconstrained coordinates onto a support and reports its log-determinant.
  • 03-nuts ↗A density over a record of your own, written by hand, sampled with NUTS over four chains, then diagnosed and summarised.
  • 04-models ↗norn.model turns one function that draws every random variable into densities, starting points, simulations and predictions.
  • 05-hmc-and-ensemble ↗Two other samplers on a correlated Gaussian: HMC, which tunes one step size, trajectory length and geometry for all chains, and the ensemble stretch move, which needs no gradient and tunes nothing.
  • 06-diagnostics ↗Chains that start in different modes of a bimodal target never meet.
  • 07-evidence ↗The evidence is the probability of the data under the model.
  • 08-laplace ↗A Laplace approximation is the Gaussian whose precision is the curvature at the mode.
  • 09-calibration ↗Ranks of prior truths among posterior draws are uniform when the posterior is right.
  • 10-hmm ↗A hidden Markov model's states are discrete, so a gradient sampler cannot move them.

this page is tested with mdxedit this page ↗