PosteriorDB models

Wren provides a PosteriorDB extension where some PosteriorDB models are available for use. (Not all have been added yet, but the intent is that Wren will eventually provide a complete collection.)

Loading models

To list the available models, use:

using Wren, PosteriorDBavailable_posteriordb_models()
80-element Vector{String}:
 "GLMM_Poisson_data-GLMM_Poisson_model"
 "GLM_Binomial_data-GLM_Binomial_model"
 "GLM_Poisson_Data-GLM_Poisson_model"
 "Mh_data-Mh_model"
 "Rate_1_data-Rate_1_model"
 "Rate_2_data-Rate_2_model"
 "Rate_3_data-Rate_3_model"
 "Rate_4_data-Rate_4_model"
 "Rate_5_data-Rate_5_model"
 "arK-arK"
 ⋮
 "wells_data-wells_daae_c_model"
 "wells_data-wells_dae_c_model"
 "wells_data-wells_dae_inter_model"
 "wells_data-wells_dae_model"
 "wells_data-wells_dist"
 "wells_data-wells_dist100_model"
 "wells_data-wells_dist100ars_model"
 "wells_data-wells_interaction_c_model"
 "wells_data-wells_interaction_model"
Benchmarks

Some of these PosteriorDB models are also benchmarked in order to track performance over time: see https://juliabayes.org/Wren.jl/bench.

And to load one of these:

model = posteriordb_model("eight_schools-eight_schools_centered")
Model eight_schools_centered
├─ arguments
│  ├─ y => [28, 8, -3, 7, -1, 1, 18, 12]
│  ├─ sigma => [15, 10, 16, 11, 9, 11, 10, 18]
│  └─ J => 8
├─ conditioned (none)
└─ expressions
     mu ~ WD.Normal(0.0, 5.0)
     tau ~ WD.HalfCauchy(0.0, 5.0)
     @iid(theta ~ WD.Normal(mu, tau), size = J)
     @batch(name = Symbol("##batch#216"), for i = eachindex(y)
             @observe y[i] ~ WD.Normal(theta[i], sigma[i])
         end)
     return nothing

You can then e.g. sample with it:

using Enzyme # Wren's default AD backend.nuts(model, 1000, 4)
╭─FlexiChain (1000 iterations, 4 chains) ──────────────────────────────────────╮
│ ↓ iter  = 1:1000                                                             │
│ → chain = 1:4                                                                │
│                                                                              │
│ Parameters (3) ── VarName                                                    │
│  Float64          mu, tau                                                    │
│  Vector{Float64}  theta (8,)                                                 │
│                                                                              │
│ Extras (10)                                                                  │
│  Float64  step_size, acceptance_rate, log_density, logprior, loglikelihood   │
│           logjoint                                                           │
│  Int64    tree_depth                                                         │
│  Bool     is_divergent, is_max_depth                                         │
│  Nothing  return_value                                                       │
╰──────────────────────────────────────────────────────────────────────────────╯

eference posteriors

PosteriorDB.jl also provides reference posteriors, i.e., chains sampled in Stan, for several of the models. FlexiChains contains an extension which lets you load these as VNChain objects.

This is not part of Wren itself. However, since FlexiChains is a dependency of Wren, you will automatically be able to access these when importing Wren and PosteriorDB, so we mention it here for completeness.

using FlexiChains, PosteriorDB# Show available reference posteriorsFlexiChains.available_posteriordb_references()
58-element Vector{String}:
 "Rate_1_data-Rate_1_model"
 "Rate_2_data-Rate_2_model"
 "Rate_3_data-Rate_3_model"
 "Rate_5_data-Rate_5_model"
 "arK-arK"
 "arma-arma11"
 "bball_drive_event_0-hmm_drive_0"
 "bball_drive_event_1-hmm_drive_1"
 "diamonds-diamonds"
 "dogs-dogs"
 ⋮
 "nes2000-nes"
 "nes_logit_data-nes_logit_model"
 "one_comp_mm_elim_abs-one_comp_mm_elim_abs"
 "radon_all-radon_pooled"
 "radon_mn-radon_pooled"
 "sblrc-blr"
 "sblri-blr"
 "wells_data-wells_dist"
 "wells_data-wells_dist100_model"
# Load a reference posteriorFlexiChains.from_posteriordb("eight_schools-eight_schools_centered")
╭─FlexiChain (1000 iterations, 10 chains) ─────────────────────────────────────╮
│ ↓ iter  = 10010:10:20000                                                     │
│ → chain = 1:10                                                               │
│                                                                              │
│ Parameters (3) ── VarName                                                    │
│  Vector{Float64}  theta (8,)                                                 │
│  Float64          mu, tau                                                    │
│                                                                              │
│ Extras (0)                                                                   │
│  (none)                                                                      │
╰──────────────────────────────────────────────────────────────────────────────╯

Please see the FlexiChains docs for more info.

Model source code

If you want to see the Wren implementations, here is a handy link to the relevant directory on GitHub.

Differences with Stan models

In general, the vast majority of PosteriorDB implementations in Wren have been very faithfully translated from Stan. There are a few differences which we note here:

  1. Stochastic generated quantities haven't been included in the model (for example things like generated quantities { x = binomial_rng(n, p); }). We probably want to implement these in Wren properly: see issue #330.

    Where possible we have already included deterministic generated quantities, as well as transformed parameters, as @track nodes in the Wren model. In particular, for transformed parameters, this matches Stan's behaviour by including them in the chain. Note that for benchmarking purposes these tracked quantities will never contribute to performance as log-density evaluations are always performed with tracking off.

  2. Some constant log-density terms have been added / dropped. For example, where Stan uses a flat constrained prior Wren sometimes uses Uniform, which adds a constant log-density of -log(upper - lower). Wren's HalfNormal and HalfCauchy distributions also include a constant normalisation term which Stan drops.

  3. Mh is somewhat rewritten in order to make use of the batching in Wren; it has the same log-density but is expressed quite differently.

  4. In the Rates models we replace Beta(1, 1) with Uniform(0, 1) which is the same distribution but has a cheaper log-density evaluation.

Docstrings

WrenCore.posteriordb_model — Function
posteriordb_model(name::AbstractString) -> WrenCore.Model

Return a Wren model for the PosteriorDB posterior called name, e.g. "arK-arK", using the data from PosteriorDB.

This requires PosteriorDB.jl and WrenDists.jl to be loaded. Only some posteriors are available; an error is thrown for any others, which lists the available ones.

source