Java builder
ModelBuilder combines immutable data, named constrained parameters, and additive factors. Analytic factors implement DifferentiableModelFactor.
Reference guide · 0.8.0
A compact operating manual for model construction, samplers, adaptation, diagnostics, graphing, reproducibility, and performance. New users should begin with the fully worked CSV/JDM analysis.
Available in JDistlib 0.8.0Model layer
ModelBuilder combines immutable data, named constrained parameters, and additive factors. Analytic factors implement DifferentiableModelFactor.
ModelScript parses the documented Stan-compatible core and lowers to the same BayesianModel. It includes shaped real/complex/sparse containers, tuples, external and numerical callbacks, control flow, and more than thirty scalar probability families.
Constraints cover real, positive, bounded, ordered, simplex, unit/sum-to-zero, and covariance/correlation parameters. Compiled factors evaluate on reusable reverse tapes in unconstrained space and add the correct log-Jacobian. Host applications read CSV, JSON, databases, or other sources and bind primitive arrays; follow the file-backed example. Run Gradients.check before HMC/NUTS.
Sampler selection
| Situation | Start with | Reason |
|---|---|---|
| Continuous, analytic gradients | NoUTurnSampler | Adaptive path length and metric; strongest general default. |
| Fixed trajectory budget | HamiltonianMonteCarlo | Predictable leapfrog count. |
| No gradients | RandomWalkMetropolis or component-wise Metropolis | Simple robust baseline. |
| Difficult scalar conditional | SliceSampler | No proposal scale acceptance decision. |
| Known conditionals or discrete state | GibbsSampler | Combines exact, ARS, and Metropolis blocks. |
| Variable model dimension | ReversibleJumpSampler | Accounts for reverse proposals, move-selection probabilities, and Jacobians. |
NUTS never silently uses finite differences. Opt in with allowFiniteDifferences(true) only when its cost and fragility are acceptable.
Trans-dimensional inference · expanded in 0.9.1
ReversibleJumpState stores a model identifier and variable-length parameters. A ReversibleJumpMove declares its reverse, forward/reverse proposal densities, and Jacobian; the sampler supplies boundary-aware move-selection probabilities. Warmup may adapt schedule weights, per-model continuous scales, and birth proposals, but retained sampling uses a frozen checkpointable process.
For up to 62 covariates or loci, start with SubsetSelectionTarget and SubsetSelectionRj. The general add/drop/swap path produces model and inclusion probabilities with ESS/MCSE/R-hat, transition and round-trip diagnostics, conditional coefficient summaries, and tidy ragged output. Read the small complete worked analysis.
For thousands of candidates with a small active cap, use SparseSubsetTarget, SparseSubsetState, and SparseSubsetRjSampler. Sorted integer states, state-dependent candidate proposals, online summaries, and exact interruption-safe checkpoints avoid dense model identities and unbounded retained storage. The 17,000-gene GSE93272 tutorial adds a mixed model, reproducible data preparation, and CUDA/OpenCL-assisted residual scores.
Adaptation
Dual averaging targets the requested acceptance probability. Windowed covariance learns a diagonal metric by default or a dense metric when enabled. Examine WarmupResult for final step size, inverse mass matrix, and mean warmup acceptance.
Diagnostics
| Signal | Meaning | Response |
|---|---|---|
| R-hat ≥ 1.01 | Split rank distributions differ across chains. | Look for modes, poor initialization, trends, or bad parameterization. |
| Low bulk ESS | Means and central summaries remain noisy. | Improve geometry or collect more retained draws. |
| Low tail ESS | Intervals and tail probabilities remain noisy. | Reparameterize; add draws only after chains mix. |
| Divergence | Hamiltonian integration encountered difficult curvature. | Check gradients and scales; non-center hierarchical models. |
| Tree-depth saturation | NUTS exhausted its trajectory budget. | Inspect pairs and ESS before raising the cap. |
| E-BFMI < 0.3 | Momentum resampling explores energy poorly. | Inspect energy plot; rescale or reparameterize. |
JDistlib reports rank-normalized split/folded R-hat, bulk/tail ESS, MCSE, posterior summaries, acceptance, divergences, depth, failures, and the minimum per-chain E-BFMI.
Graphing
Do chains overlap without drifts, sticking, or different bands?
Does each chain occupy the pooled rank range uniformly?
How quickly does within-chain dependence decay?
Do energy transitions support acceptable E-BFMI?
Where do funnels, ridges, correlations, or divergent regions occur?
Which data and parameters feed each factor?
Every ChartSpec exports JSON, CSV, and standalone SVG. InferenceHtmlReport embeds plots into one offline HTML artifact.
Performance
Chains.parallel keeps deterministic independent streams.0.8.0 also reuses random-walk proposal buffers and covariance scratch storage, avoids temporary metric velocities, reads immutable observation vectors without cloning, and uses scalar accessors during diagnostics/export. Prefer thinning only for storage or downstream cost. Use the included smoke benchmark for regression checks and JMH for serious comparisons.
Failure handling
ChainResult.Status distinguishes success, cancellation, invalid initial state, and numerical failure. Retained warnings and transition statistics preserve the evidence needed for review. Versioned NUTS checkpoints clone the exact RNG stream and restore the adapted metric, dual-averaging state, covariance accumulator, and warmup position; unsupported checkpoint formats fail explicitly.