Skip to content

Probabilistic Programming API ​

This page is a flat index of the public exports of Reactant.ProbProg, Reactant's probabilistic-programming module. Names are qualified as ProbProg.<name>; using Reactant: ProbProg unqualifies them.

For walkthroughs and runnable end-to-end programs, follow the Probabilistic Programming tutorials:

The conceptual vocabulary (traces, choice maps, selections, generative functions) is the same as in Gen.jl; readers familiar with Gen can map Constraint onto Gen's ChoiceMap, select onto Gen.select, and so on.

Types ​

SymbolSignatureSee
Tracemutable struct Trace; choices, retval, weight, subtraces; endTraces and constrained inference
AddressAddress(syms::Symbol...)Traces and constrained inference
Selectionconst Selection = OrderedSet{Address}; built via selectTraces and constrained inference
ConstraintConstraint(pairs::Pair...)Traces and constrained inference
MCMCStatemutable struct MCMCState; position, gradient, potential_energy, step_size, inverse_mass_matrix, rng; endRunning and resuming chains

Distributions ​

ProbProg.sample(rng, dist; symbol=:x) works on any Distribution subtype. Built-in distributions (Normal, Exponential, LogNormal, Bernoulli) and the recipe for defining custom ones are described in the Sampling and distributions tutorial.

Modeling ​

SymbolSignatureSee
samplesample(rng, dist; symbol)  /  sample(rng, f, args...; symbol, logpdf, support, bounds)Sampling and distributions
untraced_calluntraced_call(rng, f, args...)Sampling and distributions
simulatesimulate(rng, f, args...) -> (trace_tensor, weight, retval)Traces and constrained inference
simulate_simulate_(rng, f, args...) -> (trace::Trace, weight)Traces and constrained inference
generategenerate(rng, constraint_tensor, f, args...; constrained_addresses)Traces and constrained inference
generate_generate_(rng, constraint, f, args...) -> (trace::Trace, weight)Traces and constrained inference

Inference ​

SymbolSignatureSee
mhmh(rng, trace, weight, f, args...; selection)MCMC: MH, HMC, NUTS
mcmcmcmc(rng, original_trace, f, args...; selection, algorithm, step_size, inverse_mass_matrix, num_warmup, num_samples, ...)  /  mcmc(state::MCMCState, ...)MCMC: MH, HMC, NUTS
mcmc_logpdfmcmc_logpdf(rng, logdensity_fn, initial_position, args...; algorithm, step_size, inverse_mass_matrix, ...)  /  mcmc_logpdf(state::MCMCState, ...)MCMC: MH, HMC, NUTS
run_chainrun_chain(rng, logpdf_fn, initial_position, args...; num_warmup, num_samples, chunk_size, ...)  /  run_chain(state::MCMCState, ...)Running and resuming chains

NUTS currently requires an explicit step_size; the default step_size = nothing is rejected by the pass implementation (find_reasonable_step_size is not yet implemented). Pass Reactant.ConcreteRNumber(0.1) (or similar) and rely on dual averaging when adapt_step_size = true.

Log density ​

SymbolSignature
compile_logpcompile_logp(f, args...; observations, selection, rng) -> CompiledLogp  /  compile_logp(f, observations::Constraint, args...)
compile_logp_and_gradcompile_logp_and_grad(f, args...; observations, selection, rng) -> CompiledLogpAndGrad  /  compile_logp_and_grad(f, observations::Constraint, args...)
logplogp(f, θ, args...; observations, selection, rng)  /  logp(rng, θ, f, args...; selection, observations_tensor, observation_addresses)
logp_and_gradlogp_and_grad(f, θ, args...; observations, selection, rng) -> (logp, grad)  /  logp_and_grad(rng, θ, f, args...; selection, observations_tensor, observation_addresses)
CompiledLogp(c::CompiledLogp)(θ) -> logp
CompiledLogpAndGrad(c::CompiledLogpAndGrad)(θ) -> (logp, grad)  /  (c::CompiledLogpAndGrad)(F, G, θ)
Reactant.ProbProg.compile_logp Function
julia
compile_logp(f::Function, args...; selection=nothing, observations=Constraint(), constraints=nothing, rng=nothing)
compile_logp(f::Function, observations::Constraint, args...; selection=nothing, rng=nothing)

Compile a reusable callable g(θ) -> logp evaluating the log-density of ProbProg model f at an arbitrary parameter vector θ (either a Julia AbstractArray or a Reactant AbstractConcreteArray). If selection is omitted, all sample addresses not present in observations are treated as parameters in model execution order.

source
Reactant.ProbProg.compile_logp_and_grad Function
julia
compile_logp_and_grad(f::Function, args...; selection=nothing, observations=Constraint(), constraints=nothing, rng=nothing)
compile_logp_and_grad(f::Function, observations::Constraint, args...; selection=nothing, rng=nothing)

Compile a reusable callable fg(θ) -> (logp, gradlogp) (also supporting the 3-argument fg(F, G, θ) only_fg! convention) for a ProbProg model f.

source
Reactant.ProbProg.logp Function
julia
logp(rng::AbstractRNG, θ, f::Function, args...; selection, observations_tensor=nothing, observation_addresses=Address[])
logp(f::Function, θ, args...; selection=nothing, observations=Constraint(), constraints=nothing, rng=nothing)

Evaluate the joint log-density defined by a ProbProg model f at parameter vector θ.

Inside a @compile optimize = :probprog context, pass rng as the first argument to emit the impulse.generate weight directly into the traced module. Outside @compile, calling logp(f, θ, args...; ...) compiles and evaluates the model log-density at θ.

source
Reactant.ProbProg.logp_and_grad Function
julia
logp_and_grad(rng::AbstractRNG, θ, f::Function, args...; selection, observations_tensor=nothing, observation_addresses=Address[])
logp_and_grad(f::Function, θ, args...; selection=nothing, observations=Constraint(), constraints=nothing, rng=nothing)

Evaluate both the model log-density and its gradient with respect to θ, returning (logp, gradlogp).

source

State persistence ​

SymbolSignatureSee
save_statesave_state(filename::String, state::MCMCState)Running and resuming chains
load_stateload_state(filename::String) -> MCMCStateRunning and resuming chains

Utilities ​

SymbolSignatureSee
selectselect(addrs::Address...) -> SelectionTraces and constrained inference
get_choicesget_choices(trace::Trace) -> Dict{Symbol,Any}Traces and constrained inference
with_tracewith_trace(f) -> (f_result, tt) (installs the Impulse tracing context for the duration of f(); collects the layout metadata unflatten_trace needs)Traces and constrained inference
unflatten_traceunflatten_trace(trace_tensor, weight, entries, retval) -> TraceTraces and constrained inference
filter_entries_by_selectionfilter_entries_by_selection(entries, selection)MCMC: MH, HMC, NUTS
extract_addressesextract_addresses(constraint::Constraint) -> Set{Address}Traces and constrained inference
flatten_constraintflatten_constraint(constraint::Constraint) -> ConcreteRArrayTraces and constrained inference
mcmc_summarymcmc_summary(samples; names)  /  mcmc_summary(trace::Trace)Running and resuming chains