ArviZ Quickstart

Set-up

Here we add the necessary packages for this notebook and load a few we will use throughout.

using ArviZ, ArviZPythonPlots, Distributions, LinearAlgebra, Random, StanSample, Turing
    CondaPkg Found dependencies: /home/runner/.julia/packages/DimensionalData/48C1z/CondaPkg.toml
    CondaPkg Found dependencies: /home/runner/.julia/packages/PythonCall/avYrV/CondaPkg.toml
    CondaPkg Found dependencies: /home/runner/.julia/packages/ArviZPythonPlots/TzoLB/CondaPkg.toml
    CondaPkg Found dependencies: /home/runner/.julia/packages/PythonPlot/oS8x4/CondaPkg.toml
    CondaPkg Resolving changes
             + arviz
             + libstdcxx-ng
             + matplotlib
             + numpy
             + openssl
             + pandas
             + python
             + xarray
    CondaPkg Initialising pixi
             │ /home/runner/.julia/artifacts/cefba4912c2b400756d043a2563ef77a0088866b/bin/pixi
             │ init
             │ --format pixi
             └ /home/runner/work/ArviZ.jl/ArviZ.jl/docs/.CondaPkg
✔ Created /home/runner/work/ArviZ.jl/ArviZ.jl/docs/.CondaPkg/pixi.toml
    CondaPkg Wrote /home/runner/work/ArviZ.jl/ArviZ.jl/docs/.CondaPkg/pixi.toml
             │ [dependencies]
             │ openssl = ">=3, <3.6, >=3, <3.6"
             │ libstdcxx-ng = ">=3.4,<15.0"
             │ matplotlib = ">=1"
             │ pandas = "*"
             │ xarray = "*"
             │ arviz = ">=0.15.0,<=0.18"
             │ numpy = "*"
             │ 
             │     [dependencies.python]
             │     channel = "conda-forge"
             │     build = "*cpython*"
             │     version = ">=3.8,<4"
             │ 
             │ [project]
             │ name = ".CondaPkg"
             │ platforms = ["linux-64"]
             │ channels = ["conda-forge"]
             │ channel-priority = "strict"
             └ description = "automatically generated by CondaPkg.jl"
    CondaPkg Installing packages
             │ /home/runner/.julia/artifacts/cefba4912c2b400756d043a2563ef77a0088866b/bin/pixi
             │ install
             └ --manifest-path /home/runner/work/ArviZ.jl/ArviZ.jl/docs/.CondaPkg/pixi.toml
✔ The default environment has been installed.
# ArviZPythonPlots ships with style sheets!
use_style("arviz-darkgrid")

Get started with plotting

To plot with ArviZ, we need to load the ArviZPythonPlots package. ArviZ is designed to be used with libraries like Stan, Turing.jl, and Soss.jl but works fine with raw arrays.

rng1 = Random.MersenneTwister(37772);
plot_posterior(randn(rng1, 100_000));

Plotting a dictionary of arrays, ArviZ will interpret each key as the name of a different random variable. Each row of an array is treated as an independent series of draws from the variable, called a chain. Below, we have 10 chains of 50 draws each for four different distributions.

s = (50, 10)
plot_forest((
    normal=randn(rng1, s),
    gumbel=rand(rng1, Gumbel(), s),
    student_t=rand(rng1, TDist(6), s),
    exponential=rand(rng1, Exponential(), s),
));

Plotting with MCMCChains.jl’s Chains objects produced by Turing.jl

ArviZ is designed to work well with high dimensional, labelled data. Consider the eight schools model, which roughly tries to measure the effectiveness of SAT classes at eight different schools. To show off ArviZ’s labelling, I give the schools the names of a different eight schools.

This model is small enough to write down, is hierarchical, and uses labelling. Additionally, a centered parameterization causes divergences (which are interesting for illustration).

First we create our data and set some sampling parameters.

J = 8
y = [28.0, 8.0, -3.0, 7.0, -1.0, 1.0, 18.0, 12.0]
σ = [15.0, 10.0, 16.0, 11.0, 9.0, 11.0, 10.0, 18.0]
schools = [
    "Choate",
    "Deerfield",
    "Phillips Andover",
    "Phillips Exeter",
    "Hotchkiss",
    "Lawrenceville",
    "St. Paul's",
    "Mt. Hermon",
]
ndraws = 1_000
ndraws_warmup = 1_000
nchains = 4;

Now we write and run the model using Turing:

Turing.@model function model_turing(y, σ, J=length(y))
    μ ~ Normal(0, 5)
    τ ~ truncated(Cauchy(0, 5), 0, Inf)
    θ ~ filldist(Normal(μ, τ), J)
    for i in 1:J
        y[i] ~ Normal(θ[i], σ[i])
    end
end;
rng2 = Random.MersenneTwister(16653);
param_mod_turing = model_turing(y, σ)
sampler = NUTS(ndraws_warmup, 0.8)

turing_chns = Turing.sample(
    rng2, model_turing(y, σ), sampler, MCMCThreads(), ndraws, nchains
);
Sampling (2 threads)   0%|                              |  ETA: N/A
┌ Info: Found initial step size
└   ϵ = 0.2
┌ Info: Found initial step size
└   ϵ = 0.41250000000000003
Sampling (2 threads)   0%|▏                             |  ETA: 0:47:39
Sampling (2 threads)   1%|▎                             |  ETA: 0:23:44
Sampling (2 threads)   2%|▌                             |  ETA: 0:15:45
Sampling (2 threads)   2%|▋                             |  ETA: 0:11:46
Sampling (2 threads)   2%|▊                             |  ETA: 0:09:23
Sampling (2 threads)   3%|▉                             |  ETA: 0:07:47
Sampling (2 threads)   4%|█                             |  ETA: 0:06:38
Sampling (2 threads)   4%|█▎                            |  ETA: 0:05:47
Sampling (2 threads)   4%|█▍                            |  ETA: 0:05:07
Sampling (2 threads)   5%|█▌                            |  ETA: 0:04:35
Sampling (2 threads)   6%|█▋                            |  ETA: 0:04:09
Sampling (2 threads)   6%|█▊                            |  ETA: 0:03:47
Sampling (2 threads)   6%|██                            |  ETA: 0:03:29
Sampling (2 threads)   7%|██▏                           |  ETA: 0:03:13
Sampling (2 threads)   8%|██▎                           |  ETA: 0:02:59
Sampling (2 threads)   8%|██▍                           |  ETA: 0:02:47
Sampling (2 threads)   8%|██▌                           |  ETA: 0:02:36
Sampling (2 threads)   9%|██▊                           |  ETA: 0:02:27
Sampling (2 threads)  10%|██▉                           |  ETA: 0:02:19
Sampling (2 threads)  10%|███                           |  ETA: 0:02:11
Sampling (2 threads)  10%|███▏                          |  ETA: 0:02:04
Sampling (2 threads)  11%|███▎                          |  ETA: 0:01:58
Sampling (2 threads)  12%|███▌                          |  ETA: 0:01:52
Sampling (2 threads)  12%|███▋                          |  ETA: 0:01:47
Sampling (2 threads)  12%|███▊                          |  ETA: 0:01:42
Sampling (2 threads)  13%|███▉                          |  ETA: 0:01:38
Sampling (2 threads)  14%|████                          |  ETA: 0:01:33
Sampling (2 threads)  14%|████▎                         |  ETA: 0:01:30
Sampling (2 threads)  14%|████▍                         |  ETA: 0:01:26
Sampling (2 threads)  15%|████▌                         |  ETA: 0:01:23
Sampling (2 threads)  16%|████▋                         |  ETA: 0:01:20
Sampling (2 threads)  16%|████▊                         |  ETA: 0:01:17
Sampling (2 threads)  16%|█████                         |  ETA: 0:01:14
Sampling (2 threads)  17%|█████▏                        |  ETA: 0:01:11
Sampling (2 threads)  18%|█████▎                        |  ETA: 0:01:09
Sampling (2 threads)  18%|█████▍                        |  ETA: 0:01:07
Sampling (2 threads)  18%|█████▌                        |  ETA: 0:01:05
Sampling (2 threads)  19%|█████▊                        |  ETA: 0:01:02
Sampling (2 threads)  20%|█████▉                        |  ETA: 0:01:01
Sampling (2 threads)  20%|██████                        |  ETA: 0:00:59
Sampling (2 threads)  20%|██████▏                       |  ETA: 0:00:57
Sampling (2 threads)  21%|██████▎                       |  ETA: 0:00:55
Sampling (2 threads)  22%|██████▌                       |  ETA: 0:00:54
Sampling (2 threads)  22%|██████▋                       |  ETA: 0:00:52
Sampling (2 threads)  22%|██████▊                       |  ETA: 0:00:51
Sampling (2 threads)  23%|██████▉                       |  ETA: 0:00:50
Sampling (2 threads)  24%|███████                       |  ETA: 0:00:48
Sampling (2 threads)  24%|███████▎                      |  ETA: 0:00:47
Sampling (2 threads)  24%|███████▍                      |  ETA: 0:00:46
Sampling (2 threads)  25%|███████▌                      |  ETA: 0:00:44
Sampling (2 threads)  26%|███████▋                      |  ETA: 0:00:43
Sampling (2 threads)  26%|███████▊                      |  ETA: 0:00:42
Sampling (2 threads)  26%|████████                      |  ETA: 0:00:41
Sampling (2 threads)  27%|████████▏                     |  ETA: 0:00:40
Sampling (2 threads)  28%|████████▎                     |  ETA: 0:00:39
Sampling (2 threads)  28%|████████▍                     |  ETA: 0:00:38
Sampling (2 threads)  28%|████████▌                     |  ETA: 0:00:37
Sampling (2 threads)  29%|████████▊                     |  ETA: 0:00:36
Sampling (2 threads)  30%|████████▉                     |  ETA: 0:00:36
Sampling (2 threads)  30%|█████████                     |  ETA: 0:00:35
Sampling (2 threads)  30%|█████████▏                    |  ETA: 0:00:34
Sampling (2 threads)  31%|█████████▎                    |  ETA: 0:00:33
Sampling (2 threads)  32%|█████████▌                    |  ETA: 0:00:32
Sampling (2 threads)  32%|█████████▋                    |  ETA: 0:00:32
Sampling (2 threads)  32%|█████████▊                    |  ETA: 0:00:31
Sampling (2 threads)  33%|█████████▉                    |  ETA: 0:00:30
Sampling (2 threads)  34%|██████████                    |  ETA: 0:00:30
Sampling (2 threads)  34%|██████████▎                   |  ETA: 0:00:29
Sampling (2 threads)  34%|██████████▍                   |  ETA: 0:00:28
Sampling (2 threads)  35%|██████████▌                   |  ETA: 0:00:28
Sampling (2 threads)  36%|██████████▋                   |  ETA: 0:00:27
Sampling (2 threads)  36%|██████████▊                   |  ETA: 0:00:27
Sampling (2 threads)  36%|███████████                   |  ETA: 0:00:26
Sampling (2 threads)  37%|███████████▏                  |  ETA: 0:00:26
Sampling (2 threads)  38%|███████████▎                  |  ETA: 0:00:25
Sampling (2 threads)  38%|███████████▍                  |  ETA: 0:00:24
Sampling (2 threads)  38%|███████████▌                  |  ETA: 0:00:24
Sampling (2 threads)  39%|███████████▊                  |  ETA: 0:00:23
Sampling (2 threads)  40%|███████████▉                  |  ETA: 0:00:23
Sampling (2 threads)  40%|████████████                  |  ETA: 0:00:23
Sampling (2 threads)  40%|████████████▏                 |  ETA: 0:00:22
Sampling (2 threads)  41%|████████████▎                 |  ETA: 0:00:22
Sampling (2 threads)  42%|████████████▌                 |  ETA: 0:00:21
Sampling (2 threads)  42%|████████████▋                 |  ETA: 0:00:21
Sampling (2 threads)  42%|████████████▊                 |  ETA: 0:00:20
Sampling (2 threads)  43%|████████████▉                 |  ETA: 0:00:20
Sampling (2 threads)  44%|█████████████                 |  ETA: 0:00:20
Sampling (2 threads)  44%|█████████████▎                |  ETA: 0:00:23
Sampling (2 threads)  44%|█████████████▍                |  ETA: 0:00:23
┌ Info: Found initial step size
└   ϵ = 1.0
Sampling (2 threads)  45%|█████████████▌                |  ETA: 0:00:22
Sampling (2 threads)  46%|█████████████▋                |  ETA: 0:00:22
Sampling (2 threads)  46%|█████████████▊                |  ETA: 0:00:21
Sampling (2 threads)  46%|██████████████                |  ETA: 0:00:21
Sampling (2 threads)  47%|██████████████▏               |  ETA: 0:00:21
Sampling (2 threads)  48%|██████████████▎               |  ETA: 0:00:20
Sampling (2 threads)  48%|██████████████▍               |  ETA: 0:00:20
Sampling (2 threads)  48%|██████████████▌               |  ETA: 0:00:19
Sampling (2 threads)  49%|██████████████▊               |  ETA: 0:00:19
Sampling (2 threads)  50%|██████████████▉               |  ETA: 0:00:19
Sampling (2 threads)  50%|███████████████               |  ETA: 0:00:18
Sampling (2 threads)  50%|███████████████▏              |  ETA: 0:00:18
Sampling (2 threads)  51%|███████████████▎              |  ETA: 0:00:18
Sampling (2 threads)  52%|███████████████▌              |  ETA: 0:00:17
Sampling (2 threads)  52%|███████████████▋              |  ETA: 0:00:17
Sampling (2 threads)  52%|███████████████▊              |  ETA: 0:00:17
Sampling (2 threads)  53%|███████████████▉              |  ETA: 0:00:16
Sampling (2 threads)  54%|████████████████              |  ETA: 0:00:16
Sampling (2 threads)  54%|████████████████▎             |  ETA: 0:00:16
Sampling (2 threads)  55%|████████████████▍             |  ETA: 0:00:15
Sampling (2 threads)  55%|████████████████▌             |  ETA: 0:00:15
Sampling (2 threads)  56%|████████████████▋             |  ETA: 0:00:15
Sampling (2 threads)  56%|████████████████▊             |  ETA: 0:00:15
Sampling (2 threads)  56%|█████████████████             |  ETA: 0:00:14
Sampling (2 threads)  57%|█████████████████▏            |  ETA: 0:00:14
Sampling (2 threads)  57%|█████████████████▎            |  ETA: 0:00:14
┌ Info: Found initial step size
└   ϵ = 0.2
Sampling (2 threads)  58%|█████████████████▍            |  ETA: 0:00:13
Sampling (2 threads)  58%|█████████████████▌            |  ETA: 0:00:13
Sampling (2 threads)  59%|█████████████████▊            |  ETA: 0:00:13
Sampling (2 threads)  60%|█████████████████▉            |  ETA: 0:00:13
Sampling (2 threads)  60%|██████████████████            |  ETA: 0:00:12
Sampling (2 threads)  60%|██████████████████▏           |  ETA: 0:00:12
Sampling (2 threads)  61%|██████████████████▎           |  ETA: 0:00:12
Sampling (2 threads)  62%|██████████████████▌           |  ETA: 0:00:12
Sampling (2 threads)  62%|██████████████████▋           |  ETA: 0:00:11
Sampling (2 threads)  62%|██████████████████▊           |  ETA: 0:00:11
Sampling (2 threads)  63%|██████████████████▉           |  ETA: 0:00:11
Sampling (2 threads)  64%|███████████████████           |  ETA: 0:00:11
Sampling (2 threads)  64%|███████████████████▎          |  ETA: 0:00:10
Sampling (2 threads)  64%|███████████████████▍          |  ETA: 0:00:10
Sampling (2 threads)  65%|███████████████████▌          |  ETA: 0:00:10
Sampling (2 threads)  66%|███████████████████▋          |  ETA: 0:00:10
Sampling (2 threads)  66%|███████████████████▊          |  ETA: 0:00:10
Sampling (2 threads)  66%|████████████████████          |  ETA: 0:00:09
Sampling (2 threads)  67%|████████████████████▏         |  ETA: 0:00:09
Sampling (2 threads)  68%|████████████████████▎         |  ETA: 0:00:09
Sampling (2 threads)  68%|████████████████████▍         |  ETA: 0:00:09
Sampling (2 threads)  68%|████████████████████▌         |  ETA: 0:00:09
Sampling (2 threads)  69%|████████████████████▊         |  ETA: 0:00:08
Sampling (2 threads)  70%|████████████████████▉         |  ETA: 0:00:08
Sampling (2 threads)  70%|█████████████████████         |  ETA: 0:00:08
Sampling (2 threads)  70%|█████████████████████▏        |  ETA: 0:00:08
Sampling (2 threads)  71%|█████████████████████▎        |  ETA: 0:00:08
Sampling (2 threads)  72%|█████████████████████▌        |  ETA: 0:00:07
Sampling (2 threads)  72%|█████████████████████▋        |  ETA: 0:00:07
Sampling (2 threads)  72%|█████████████████████▊        |  ETA: 0:00:07
Sampling (2 threads)  73%|█████████████████████▉        |  ETA: 0:00:07
Sampling (2 threads)  74%|██████████████████████        |  ETA: 0:00:07
Sampling (2 threads)  74%|██████████████████████▎       |  ETA: 0:00:07
Sampling (2 threads)  74%|██████████████████████▍       |  ETA: 0:00:06
Sampling (2 threads)  75%|██████████████████████▌       |  ETA: 0:00:06
Sampling (2 threads)  76%|██████████████████████▋       |  ETA: 0:00:06
Sampling (2 threads)  76%|██████████████████████▊       |  ETA: 0:00:06
Sampling (2 threads)  76%|███████████████████████       |  ETA: 0:00:06
Sampling (2 threads)  77%|███████████████████████▏      |  ETA: 0:00:06
Sampling (2 threads)  78%|███████████████████████▎      |  ETA: 0:00:05
Sampling (2 threads)  78%|███████████████████████▍      |  ETA: 0:00:05
Sampling (2 threads)  78%|███████████████████████▌      |  ETA: 0:00:05
Sampling (2 threads)  79%|███████████████████████▊      |  ETA: 0:00:05
Sampling (2 threads)  80%|███████████████████████▉      |  ETA: 0:00:05
Sampling (2 threads)  80%|████████████████████████      |  ETA: 0:00:05
Sampling (2 threads)  80%|████████████████████████▏     |  ETA: 0:00:05
Sampling (2 threads)  81%|████████████████████████▎     |  ETA: 0:00:04
Sampling (2 threads)  82%|████████████████████████▌     |  ETA: 0:00:04
Sampling (2 threads)  82%|████████████████████████▋     |  ETA: 0:00:04
Sampling (2 threads)  82%|████████████████████████▊     |  ETA: 0:00:04
Sampling (2 threads)  83%|████████████████████████▉     |  ETA: 0:00:04
Sampling (2 threads)  84%|█████████████████████████     |  ETA: 0:00:04
Sampling (2 threads)  84%|█████████████████████████▎    |  ETA: 0:00:04
Sampling (2 threads)  84%|█████████████████████████▍    |  ETA: 0:00:03
Sampling (2 threads)  85%|█████████████████████████▌    |  ETA: 0:00:03
Sampling (2 threads)  86%|█████████████████████████▋    |  ETA: 0:00:03
Sampling (2 threads)  86%|█████████████████████████▊    |  ETA: 0:00:03
Sampling (2 threads)  86%|██████████████████████████    |  ETA: 0:00:03
Sampling (2 threads)  87%|██████████████████████████▏   |  ETA: 0:00:03
Sampling (2 threads)  88%|██████████████████████████▎   |  ETA: 0:00:03
Sampling (2 threads)  88%|██████████████████████████▍   |  ETA: 0:00:03
Sampling (2 threads)  88%|██████████████████████████▌   |  ETA: 0:00:02
Sampling (2 threads)  89%|██████████████████████████▊   |  ETA: 0:00:02
Sampling (2 threads)  90%|██████████████████████████▉   |  ETA: 0:00:02
Sampling (2 threads)  90%|███████████████████████████   |  ETA: 0:00:02
Sampling (2 threads)  90%|███████████████████████████▏  |  ETA: 0:00:02
Sampling (2 threads)  91%|███████████████████████████▎  |  ETA: 0:00:02
Sampling (2 threads)  92%|███████████████████████████▌  |  ETA: 0:00:02
Sampling (2 threads)  92%|███████████████████████████▋  |  ETA: 0:00:02
Sampling (2 threads)  92%|███████████████████████████▊  |  ETA: 0:00:02
Sampling (2 threads)  93%|███████████████████████████▉  |  ETA: 0:00:01
Sampling (2 threads)  94%|████████████████████████████  |  ETA: 0:00:01
Sampling (2 threads)  94%|████████████████████████████▎ |  ETA: 0:00:01
Sampling (2 threads)  94%|████████████████████████████▍ |  ETA: 0:00:01
Sampling (2 threads)  95%|████████████████████████████▌ |  ETA: 0:00:01
Sampling (2 threads)  96%|████████████████████████████▋ |  ETA: 0:00:01
Sampling (2 threads)  96%|████████████████████████████▊ |  ETA: 0:00:01
Sampling (2 threads)  96%|█████████████████████████████ |  ETA: 0:00:01
Sampling (2 threads)  97%|█████████████████████████████▏|  ETA: 0:00:01
Sampling (2 threads)  98%|█████████████████████████████▎|  ETA: 0:00:00
Sampling (2 threads)  98%|█████████████████████████████▍|  ETA: 0:00:00
Sampling (2 threads)  98%|█████████████████████████████▌|  ETA: 0:00:00
Sampling (2 threads)  99%|█████████████████████████████▊|  ETA: 0:00:00
Sampling (2 threads) 100%|█████████████████████████████▉|  ETA: 0:00:00
Sampling (2 threads) 100%|██████████████████████████████| Time: 0:00:19
Sampling (2 threads) 100%|██████████████████████████████| Time: 0:00:19

Most ArviZ functions work fine with Chains objects from Turing:

plot_autocorr(turing_chns; var_names=(:μ, :τ));

Convert to InferenceData

For much more powerful querying, analysis and plotting, we can use built-in ArviZ utilities to convert Chains objects to multidimensional data structures with named dimensions and indices. Note that for such dimensions, the information is not contained in Chains, so we need to provide it.

ArviZ is built to work with InferenceData, and the more groups it has access to, the more powerful analyses it can perform.

idata_turing_post = from_mcmcchains(
    turing_chns;
    coords=(; school=schools),
    dims=NamedTuple(k => (:school,) for k in (:y, :σ, :θ)),
    library="Turing",
)
InferenceData
posterior
1000×4×8 Dataset
├──────────────────┴──────────────────────────────────────────── dims ┐
  ↓ draw,
  → chain,
  ↗ school Categorical{String} ["Choate", …, "Mt. Hermon"] Unordered
├───────────────────────────────────────────────────────────── layers ┤
 eltype: Float64 dims: draw, chain size: 1000×4
 eltype: Float64 dims: draw, chain size: 1000×4
 eltype: Float64 dims: draw, chain, school size: 1000×4×8
├─────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 2 entries:
  "created_at" => "2025-08-19T09:39:52.91"
  "inference_library" => "Turing"
sample_stats
1000×4 Dataset
├────────────────┴──────────────────────────────────────────── dims ┐
  ↓ draw, → chain
├─────────────────────────────────────────────────────────── layers ┤
  :energy           eltype: Float64 dims: draw, chain size: 1000×4
  :n_steps          eltype: Int64 dims: draw, chain size: 1000×4
  :diverging        eltype: Bool dims: draw, chain size: 1000×4
  :max_energy_error eltype: Float64 dims: draw, chain size: 1000×4
  :loglikelihood    eltype: Float64 dims: draw, chain size: 1000×4
  :energy_error     eltype: Float64 dims: draw, chain size: 1000×4
  :is_accept        eltype: Bool dims: draw, chain size: 1000×4
  :logprior         eltype: Float64 dims: draw, chain size: 1000×4
  :log_density      eltype: Float64 dims: draw, chain size: 1000×4
  :tree_depth       eltype: Int64 dims: draw, chain size: 1000×4
  :step_size        eltype: Float64 dims: draw, chain size: 1000×4
  :acceptance_rate  eltype: Float64 dims: draw, chain size: 1000×4
  :lp               eltype: Float64 dims: draw, chain size: 1000×4
  :step_size_nom    eltype: Float64 dims: draw, chain size: 1000×4
├───────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 2 entries:
  "created_at" => "2025-08-19T09:39:52.832"
  "inference_library" => "Turing"

Each group is a Dataset, a DimensionalData.AbstractDimStack that can be used identically to a DimensionalData.Dimstack. We can view a summary of the dataset.

idata_turing_post.posterior
┌ 1000×4×8 Dataset ┐
├──────────────────┴──────────────────────────────────────────── dims ┐
  ↓ draw,
  → chain,
  ↗ school Categorical{String} ["Choate", …, "Mt. Hermon"] Unordered
├───────────────────────────────────────────────────────────── layers ┤
  :μ eltype: Float64 dims: draw, chain size: 1000×4
  :τ eltype: Float64 dims: draw, chain size: 1000×4
  :θ eltype: Float64 dims: draw, chain, school size: 1000×4×8
├─────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 2 entries:
  "created_at"        => "2025-08-19T09:39:52.91"
  "inference_library" => "Turing"

Here is a plot of the trace. Note the intelligent labels.

plot_trace(idata_turing_post);

We can also generate summary stats…

summarystats(idata_turing_post)

[TABLE]

SummaryStats

…and examine the energy distribution of the Hamiltonian sampler.

plot_energy(idata_turing_post);

Additional information in Turing.jl

With a few more steps, we can use Turing to compute additional useful groups to add to the InferenceData.

To sample from the prior, one simply calls sample but with the Prior sampler:

prior = Turing.sample(rng2, param_mod_turing, Prior(), ndraws);
Sampling   0%|                                          |  ETA: N/A
Sampling   0%|▎                                         |  ETA: 0:01:26
Sampling   1%|▍                                         |  ETA: 0:00:43
Sampling   2%|▋                                         |  ETA: 0:00:28
Sampling   2%|▉                                         |  ETA: 0:00:21
Sampling   2%|█                                         |  ETA: 0:00:17
Sampling   3%|█▎                                        |  ETA: 0:00:14
Sampling   4%|█▌                                        |  ETA: 0:00:12
Sampling   4%|█▋                                        |  ETA: 0:00:10
Sampling   4%|█▉                                        |  ETA: 0:00:09
Sampling   5%|██▏                                       |  ETA: 0:00:08
Sampling   6%|██▎                                       |  ETA: 0:00:08
Sampling   6%|██▌                                       |  ETA: 0:00:07
Sampling   6%|██▊                                       |  ETA: 0:00:06
Sampling   7%|███                                       |  ETA: 0:00:06
Sampling   8%|███▏                                      |  ETA: 0:00:05
Sampling   8%|███▍                                      |  ETA: 0:00:05
Sampling   8%|███▋                                      |  ETA: 0:00:05
Sampling   9%|███▊                                      |  ETA: 0:00:04
Sampling  10%|████                                      |  ETA: 0:00:04
Sampling  10%|████▎                                     |  ETA: 0:00:04
Sampling  10%|████▍                                     |  ETA: 0:00:04
Sampling  11%|████▋                                     |  ETA: 0:00:04
Sampling  12%|████▉                                     |  ETA: 0:00:03
Sampling  12%|█████                                     |  ETA: 0:00:03
Sampling  12%|█████▎                                    |  ETA: 0:00:03
Sampling  13%|█████▌                                    |  ETA: 0:00:03
Sampling  14%|█████▋                                    |  ETA: 0:00:03
Sampling  14%|█████▉                                    |  ETA: 0:00:03
Sampling  14%|██████▏                                   |  ETA: 0:00:03
Sampling  15%|██████▎                                   |  ETA: 0:00:03
Sampling  16%|██████▌                                   |  ETA: 0:00:02
Sampling  16%|██████▊                                   |  ETA: 0:00:02
Sampling  16%|██████▉                                   |  ETA: 0:00:02
Sampling  17%|███████▏                                  |  ETA: 0:00:02
Sampling  18%|███████▍                                  |  ETA: 0:00:02
Sampling  18%|███████▌                                  |  ETA: 0:00:02
Sampling  18%|███████▊                                  |  ETA: 0:00:02
Sampling  19%|████████                                  |  ETA: 0:00:02
Sampling  20%|████████▎                                 |  ETA: 0:00:02
Sampling  20%|████████▍                                 |  ETA: 0:00:02
Sampling  20%|████████▋                                 |  ETA: 0:00:02
Sampling  21%|████████▉                                 |  ETA: 0:00:02
Sampling  22%|█████████                                 |  ETA: 0:00:02
Sampling  22%|█████████▎                                |  ETA: 0:00:02
Sampling  22%|█████████▌                                |  ETA: 0:00:02
Sampling  23%|█████████▋                                |  ETA: 0:00:02
Sampling  24%|█████████▉                                |  ETA: 0:00:02
Sampling  24%|██████████▏                               |  ETA: 0:00:01
Sampling  24%|██████████▎                               |  ETA: 0:00:01
Sampling  25%|██████████▌                               |  ETA: 0:00:01
Sampling  26%|██████████▊                               |  ETA: 0:00:01
Sampling  26%|██████████▉                               |  ETA: 0:00:01
Sampling  26%|███████████▏                              |  ETA: 0:00:01
Sampling  27%|███████████▍                              |  ETA: 0:00:01
Sampling  28%|███████████▌                              |  ETA: 0:00:01
Sampling  28%|███████████▊                              |  ETA: 0:00:01
Sampling  28%|████████████                              |  ETA: 0:00:01
Sampling  29%|████████████▏                             |  ETA: 0:00:01
Sampling  30%|████████████▍                             |  ETA: 0:00:01
Sampling  30%|████████████▋                             |  ETA: 0:00:01
Sampling  30%|████████████▊                             |  ETA: 0:00:01
Sampling  31%|█████████████                             |  ETA: 0:00:01
Sampling  32%|█████████████▎                            |  ETA: 0:00:01
Sampling  32%|█████████████▌                            |  ETA: 0:00:01
Sampling  32%|█████████████▋                            |  ETA: 0:00:01
Sampling  33%|█████████████▉                            |  ETA: 0:00:01
Sampling  34%|██████████████▏                           |  ETA: 0:00:01
Sampling  34%|██████████████▎                           |  ETA: 0:00:01
Sampling  34%|██████████████▌                           |  ETA: 0:00:01
Sampling  35%|██████████████▊                           |  ETA: 0:00:01
Sampling  36%|██████████████▉                           |  ETA: 0:00:01
Sampling  36%|███████████████▏                          |  ETA: 0:00:01
Sampling  36%|███████████████▍                          |  ETA: 0:00:01
Sampling  37%|███████████████▌                          |  ETA: 0:00:01
Sampling  38%|███████████████▊                          |  ETA: 0:00:01
Sampling  38%|████████████████                          |  ETA: 0:00:01
Sampling  38%|████████████████▏                         |  ETA: 0:00:01
Sampling  39%|████████████████▍                         |  ETA: 0:00:01
Sampling  40%|████████████████▋                         |  ETA: 0:00:01
Sampling  40%|████████████████▊                         |  ETA: 0:00:01
Sampling  40%|█████████████████                         |  ETA: 0:00:01
Sampling  41%|█████████████████▎                        |  ETA: 0:00:01
Sampling  42%|█████████████████▍                        |  ETA: 0:00:01
Sampling  42%|█████████████████▋                        |  ETA: 0:00:01
Sampling  42%|█████████████████▉                        |  ETA: 0:00:01
Sampling  43%|██████████████████                        |  ETA: 0:00:01
Sampling  44%|██████████████████▎                       |  ETA: 0:00:01
Sampling  44%|██████████████████▌                       |  ETA: 0:00:01
Sampling  44%|██████████████████▊                       |  ETA: 0:00:01
Sampling  45%|██████████████████▉                       |  ETA: 0:00:01
Sampling  46%|███████████████████▏                      |  ETA: 0:00:01
Sampling  46%|███████████████████▍                      |  ETA: 0:00:01
Sampling  46%|███████████████████▌                      |  ETA: 0:00:01
Sampling  47%|███████████████████▊                      |  ETA: 0:00:01
Sampling  48%|████████████████████                      |  ETA: 0:00:01
Sampling  48%|████████████████████▏                     |  ETA: 0:00:01
Sampling  48%|████████████████████▍                     |  ETA: 0:00:01
Sampling  49%|████████████████████▋                     |  ETA: 0:00:01
Sampling  50%|████████████████████▊                     |  ETA: 0:00:01
Sampling  50%|█████████████████████                     |  ETA: 0:00:00
Sampling  50%|█████████████████████▎                    |  ETA: 0:00:00
Sampling  51%|█████████████████████▍                    |  ETA: 0:00:00
Sampling  52%|█████████████████████▋                    |  ETA: 0:00:00
Sampling  52%|█████████████████████▉                    |  ETA: 0:00:00
Sampling  52%|██████████████████████                    |  ETA: 0:00:00
Sampling  53%|██████████████████████▎                   |  ETA: 0:00:00
Sampling  54%|██████████████████████▌                   |  ETA: 0:00:00
Sampling  54%|██████████████████████▋                   |  ETA: 0:00:00
Sampling  55%|██████████████████████▉                   |  ETA: 0:00:00
Sampling  55%|███████████████████████▏                  |  ETA: 0:00:00
Sampling  56%|███████████████████████▎                  |  ETA: 0:00:00
Sampling  56%|███████████████████████▌                  |  ETA: 0:00:00
Sampling  56%|███████████████████████▊                  |  ETA: 0:00:00
Sampling  57%|████████████████████████                  |  ETA: 0:00:00
Sampling  57%|████████████████████████▏                 |  ETA: 0:00:00
Sampling  58%|████████████████████████▍                 |  ETA: 0:00:00
Sampling  58%|████████████████████████▋                 |  ETA: 0:00:00
Sampling  59%|████████████████████████▊                 |  ETA: 0:00:00
Sampling  60%|█████████████████████████                 |  ETA: 0:00:00
Sampling  60%|█████████████████████████▎                |  ETA: 0:00:00
Sampling  60%|█████████████████████████▍                |  ETA: 0:00:00
Sampling  61%|█████████████████████████▋                |  ETA: 0:00:00
Sampling  62%|█████████████████████████▉                |  ETA: 0:00:00
Sampling  62%|██████████████████████████                |  ETA: 0:00:00
Sampling  62%|██████████████████████████▎               |  ETA: 0:00:00
Sampling  63%|██████████████████████████▌               |  ETA: 0:00:00
Sampling  64%|██████████████████████████▋               |  ETA: 0:00:00
Sampling  64%|██████████████████████████▉               |  ETA: 0:00:00
Sampling  64%|███████████████████████████▏              |  ETA: 0:00:00
Sampling  65%|███████████████████████████▎              |  ETA: 0:00:00
Sampling  66%|███████████████████████████▌              |  ETA: 0:00:00
Sampling  66%|███████████████████████████▊              |  ETA: 0:00:00
Sampling  66%|███████████████████████████▉              |  ETA: 0:00:00
Sampling  67%|████████████████████████████▏             |  ETA: 0:00:00
Sampling  68%|████████████████████████████▍             |  ETA: 0:00:00
Sampling  68%|████████████████████████████▌             |  ETA: 0:00:00
Sampling  68%|████████████████████████████▊             |  ETA: 0:00:00
Sampling  69%|█████████████████████████████             |  ETA: 0:00:00
Sampling  70%|█████████████████████████████▎            |  ETA: 0:00:00
Sampling  70%|█████████████████████████████▍            |  ETA: 0:00:00
Sampling  70%|█████████████████████████████▋            |  ETA: 0:00:00
Sampling  71%|█████████████████████████████▉            |  ETA: 0:00:00
Sampling  72%|██████████████████████████████            |  ETA: 0:00:00
Sampling  72%|██████████████████████████████▎           |  ETA: 0:00:00
Sampling  72%|██████████████████████████████▌           |  ETA: 0:00:00
Sampling  73%|██████████████████████████████▋           |  ETA: 0:00:00
Sampling  74%|██████████████████████████████▉           |  ETA: 0:00:00
Sampling  74%|███████████████████████████████▏          |  ETA: 0:00:00
Sampling  74%|███████████████████████████████▎          |  ETA: 0:00:00
Sampling  75%|███████████████████████████████▌          |  ETA: 0:00:00
Sampling  76%|███████████████████████████████▊          |  ETA: 0:00:00
Sampling  76%|███████████████████████████████▉          |  ETA: 0:00:00
Sampling  76%|████████████████████████████████▏         |  ETA: 0:00:00
Sampling  77%|████████████████████████████████▍         |  ETA: 0:00:00
Sampling  78%|████████████████████████████████▌         |  ETA: 0:00:00
Sampling  78%|████████████████████████████████▊         |  ETA: 0:00:00
Sampling  78%|█████████████████████████████████         |  ETA: 0:00:00
Sampling  79%|█████████████████████████████████▏        |  ETA: 0:00:00
Sampling  80%|█████████████████████████████████▍        |  ETA: 0:00:00
Sampling  80%|█████████████████████████████████▋        |  ETA: 0:00:00
Sampling  80%|█████████████████████████████████▊        |  ETA: 0:00:00
Sampling  81%|██████████████████████████████████        |  ETA: 0:00:00
Sampling  82%|██████████████████████████████████▎       |  ETA: 0:00:00
Sampling  82%|██████████████████████████████████▌       |  ETA: 0:00:00
Sampling  82%|██████████████████████████████████▋       |  ETA: 0:00:00
Sampling  83%|██████████████████████████████████▉       |  ETA: 0:00:00
Sampling  84%|███████████████████████████████████▏      |  ETA: 0:00:00
Sampling  84%|███████████████████████████████████▎      |  ETA: 0:00:00
Sampling  84%|███████████████████████████████████▌      |  ETA: 0:00:00
Sampling  85%|███████████████████████████████████▊      |  ETA: 0:00:00
Sampling  86%|███████████████████████████████████▉      |  ETA: 0:00:00
Sampling  86%|████████████████████████████████████▏     |  ETA: 0:00:00
Sampling  86%|████████████████████████████████████▍     |  ETA: 0:00:00
Sampling  87%|████████████████████████████████████▌     |  ETA: 0:00:00
Sampling  88%|████████████████████████████████████▊     |  ETA: 0:00:00
Sampling  88%|█████████████████████████████████████     |  ETA: 0:00:00
Sampling  88%|█████████████████████████████████████▏    |  ETA: 0:00:00
Sampling  89%|█████████████████████████████████████▍    |  ETA: 0:00:00
Sampling  90%|█████████████████████████████████████▋    |  ETA: 0:00:00
Sampling  90%|█████████████████████████████████████▊    |  ETA: 0:00:00
Sampling  90%|██████████████████████████████████████    |  ETA: 0:00:00
Sampling  91%|██████████████████████████████████████▎   |  ETA: 0:00:00
Sampling  92%|██████████████████████████████████████▍   |  ETA: 0:00:00
Sampling  92%|██████████████████████████████████████▋   |  ETA: 0:00:00
Sampling  92%|██████████████████████████████████████▉   |  ETA: 0:00:00
Sampling  93%|███████████████████████████████████████   |  ETA: 0:00:00
Sampling  94%|███████████████████████████████████████▎  |  ETA: 0:00:00
Sampling  94%|███████████████████████████████████████▌  |  ETA: 0:00:00
Sampling  94%|███████████████████████████████████████▊  |  ETA: 0:00:00
Sampling  95%|███████████████████████████████████████▉  |  ETA: 0:00:00
Sampling  96%|████████████████████████████████████████▏ |  ETA: 0:00:00
Sampling  96%|████████████████████████████████████████▍ |  ETA: 0:00:00
Sampling  96%|████████████████████████████████████████▌ |  ETA: 0:00:00
Sampling  97%|████████████████████████████████████████▊ |  ETA: 0:00:00
Sampling  98%|█████████████████████████████████████████ |  ETA: 0:00:00
Sampling  98%|█████████████████████████████████████████▏|  ETA: 0:00:00
Sampling  98%|█████████████████████████████████████████▍|  ETA: 0:00:00
Sampling  99%|█████████████████████████████████████████▋|  ETA: 0:00:00
Sampling 100%|█████████████████████████████████████████▊|  ETA: 0:00:00
Sampling 100%|██████████████████████████████████████████| Time: 0:00:00
Sampling 100%|██████████████████████████████████████████| Time: 0:00:00

To draw from the prior and posterior predictive distributions we can instantiate a “predictive model”, i.e. a Turing model but with the observations set to missing, and then calling predict on the predictive model and the previously drawn samples:

# Instantiate the predictive model
param_mod_predict = model_turing(similar(y, Missing), σ)
# and then sample!
prior_predictive = Turing.predict(rng2, param_mod_predict, prior)
posterior_predictive = Turing.predict(rng2, param_mod_predict, turing_chns);

And to extract the pointwise log-likelihoods, which is useful if you want to compute metrics such as loo,

log_likelihood = let
    log_likelihood = Turing.pointwise_loglikelihoods(
        param_mod_turing, MCMCChains.get_sections(turing_chns, :parameters)
    )
    # Ensure the ordering of the loglikelihoods matches the ordering of `posterior_predictive`
    ynames = string.(keys(posterior_predictive))
    log_likelihood_y = getindex.(Ref(log_likelihood), ynames)
    (; y=cat(log_likelihood_y...; dims=3))
end;

This can then be included in the from_mcmcchains call from above:

idata_turing = from_mcmcchains(
    turing_chns;
    posterior_predictive,
    log_likelihood,
    prior,
    prior_predictive,
    observed_data=(; y),
    coords=(; school=schools),
    dims=NamedTuple(k => (:school,) for k in (:y, :σ, :θ)),
    library=Turing,
)
InferenceData
posterior
1000×4×8 Dataset
├──────────────────┴──────────────────────────────────────────── dims ┐
  ↓ draw,
  → chain,
  ↗ school Categorical{String} ["Choate", …, "Mt. Hermon"] Unordered
├───────────────────────────────────────────────────────────── layers ┤
 eltype: Float64 dims: draw, chain size: 1000×4
 eltype: Float64 dims: draw, chain size: 1000×4
 eltype: Float64 dims: draw, chain, school size: 1000×4×8
├─────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 3 entries:
  "created_at" => "2025-08-19T09:40:24.737"
  "inference_library_version" => "0.40.2"
  "inference_library" => "Turing"
posterior_predictive
1000×4×8 Dataset
├──────────────────┴──────────────────────────────────────────── dims ┐
  ↓ draw,
  → chain,
  ↗ school Categorical{String} ["Choate", …, "Mt. Hermon"] Unordered
├───────────────────────────────────────────────────────────── layers ┤
  :y eltype: Float64 dims: draw, chain, school size: 1000×4×8
├─────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 3 entries:
  "created_at" => "2025-08-19T09:40:24.274"
  "inference_library_version" => "0.40.2"
  "inference_library" => "Turing"
log_likelihood
1000×4×8 Dataset
├──────────────────┴──────────────────────────────────────────── dims ┐
  ↓ draw,
  → chain,
  ↗ school Categorical{String} ["Choate", …, "Mt. Hermon"] Unordered
├───────────────────────────────────────────────────────────── layers ┤
  :y eltype: Float64 dims: draw, chain, school size: 1000×4×8
├─────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 3 entries:
  "created_at" => "2025-08-19T09:40:24.577"
  "inference_library_version" => "0.40.2"
  "inference_library" => "Turing"
sample_stats
1000×4 Dataset
├────────────────┴──────────────────────────────────────────── dims ┐
  ↓ draw, → chain
├─────────────────────────────────────────────────────────── layers ┤
  :energy           eltype: Float64 dims: draw, chain size: 1000×4
  :n_steps          eltype: Int64 dims: draw, chain size: 1000×4
  :diverging        eltype: Bool dims: draw, chain size: 1000×4
  :max_energy_error eltype: Float64 dims: draw, chain size: 1000×4
  :loglikelihood    eltype: Float64 dims: draw, chain size: 1000×4
  :energy_error     eltype: Float64 dims: draw, chain size: 1000×4
  :is_accept        eltype: Bool dims: draw, chain size: 1000×4
  :logprior         eltype: Float64 dims: draw, chain size: 1000×4
  :log_density      eltype: Float64 dims: draw, chain size: 1000×4
  :tree_depth       eltype: Int64 dims: draw, chain size: 1000×4
  :step_size        eltype: Float64 dims: draw, chain size: 1000×4
  :acceptance_rate  eltype: Float64 dims: draw, chain size: 1000×4
  :lp               eltype: Float64 dims: draw, chain size: 1000×4
  :step_size_nom    eltype: Float64 dims: draw, chain size: 1000×4
├───────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 3 entries:
  "created_at" => "2025-08-19T09:40:24.737"
  "inference_library_version" => "0.40.2"
  "inference_library" => "Turing"
prior
1000×1×8 Dataset
├──────────────────┴──────────────────────────────────────────── dims ┐
  ↓ draw,
  → chain,
  ↗ school Categorical{String} ["Choate", …, "Mt. Hermon"] Unordered
├───────────────────────────────────────────────────────────── layers ┤
 eltype: Float64 dims: draw, chain size: 1000×1
 eltype: Float64 dims: draw, chain size: 1000×1
 eltype: Float64 dims: draw, chain, school size: 1000×1×8
├─────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 3 entries:
  "created_at" => "2025-08-19T09:40:25.306"
  "inference_library_version" => "0.40.2"
  "inference_library" => "Turing"
prior_predictive
1000×1×8 Dataset
├──────────────────┴──────────────────────────────────────────── dims ┐
  ↓ draw,
  → chain,
  ↗ school Categorical{String} ["Choate", …, "Mt. Hermon"] Unordered
├───────────────────────────────────────────────────────────── layers ┤
  :y eltype: Float64 dims: draw, chain, school size: 1000×1×8
├─────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 3 entries:
  "created_at" => "2025-08-19T09:40:25.138"
  "inference_library_version" => "0.40.2"
  "inference_library" => "Turing"
sample_stats_prior
1000×1 Dataset
├────────────────┴───────────────────────────────────────── dims ┐
  ↓ draw, → chain
├──────────────────────────────────────────────────────── layers ┤
  :logprior      eltype: Float64 dims: draw, chain size: 1000×1
  :lp            eltype: Float64 dims: draw, chain size: 1000×1
  :loglikelihood eltype: Float64 dims: draw, chain size: 1000×1
├────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 3 entries:
  "created_at" => "2025-08-19T09:40:25.233"
  "inference_library_version" => "0.40.2"
  "inference_library" => "Turing"
observed_data
8-element Dataset
├───────────────────┴──────────────────────────────────────────── dims ┐
  ↓ school Categorical{String} ["Choate", …, "Mt. Hermon"] Unordered
├────────────────────────────────────────────────────────────── layers ┤
  :y eltype: Float64 dims: school size: 8
├──────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 3 entries:
  "created_at" => "2025-08-19T09:40:25.503"
  "inference_library_version" => "0.40.2"
  "inference_library" => "Turing"

Then we can for example compute the expected leave-one-out (LOO) predictive density, which is an estimate of the out-of-distribution predictive fit of the model:

loo(idata_turing) # higher ELPD is better
PSISLOOResult with estimates
 elpd  elpd_mcse    p  p_mcse 
  -31        1.4  0.9    0.33

and PSISResult with 1000 draws, 4 chains, and 8 parameters
Pareto shape (k) diagnostic values:
                    Count      Min. ESS
 (-Inf, 0.5]  good  6 (75.0%)  313
  (0.5, 0.7]  okay  2 (25.0%)  1049

If the model is well-calibrated, i.e. it replicates the true generative process well, the CDF of the pointwise LOO values should be similarly distributed to a uniform distribution. This can be inspected visually:

plot_loo_pit(idata_turing; y=:y, ecdf=true);

Plotting with Stan.jl outputs

StanSample.jl comes with built-in support for producing InferenceData outputs.

Here is the same centered eight schools model in Stan:

schools_code = """
data {
    int<lower=0> J;
    array[J] real y;
    array[J] real<lower=0> sigma;
}

parameters {
    real mu;
    real<lower=0> tau;
    array[J] real theta;
}

model {
    mu ~ normal(0, 5);
    tau ~ cauchy(0, 5);
    theta ~ normal(mu, tau);
    y ~ normal(theta, sigma);
}

generated quantities {
    vector[J] log_lik;
    vector[J] y_hat;
    for (j in 1:J) {
        log_lik[j] = normal_lpdf(y[j] | theta[j], sigma[j]);
        y_hat[j] = normal_rng(theta[j], sigma[j]);
    }
}
"""

schools_data = Dict("J" => J, "y" => y, "sigma" => σ)
idata_stan = mktempdir() do path
    stan_model = SampleModel("schools", schools_code, path)
    _ = stan_sample(
        stan_model;
        data=schools_data,
        num_chains=nchains,
        num_warmups=ndraws_warmup,
        num_samples=ndraws,
        seed=28983,
        summary=false,
    )
    return StanSample.inferencedata(
        stan_model;
        posterior_predictive_var=:y_hat,
        observed_data=(; y),
        log_likelihood_var=:log_lik,
        coords=(; school=schools),
        dims=NamedTuple(
            k => (:school,) for k in (:y, :sigma, :theta, :log_lik, :y_hat)
        ),
    )
end
[ Info: /tmp/jl_q9qkTM/schools.stan updated.
InferenceData
posterior
1000×4×8 Dataset
├──────────────────┴──────────────────────────────────────────── dims ┐
  ↓ draw,
  → chain,
  ↗ school Categorical{String} ["Choate", …, "Mt. Hermon"] Unordered
├───────────────────────────────────────────────────────────── layers ┤
  :mu    eltype: Float64 dims: draw, chain size: 1000×4
  :tau   eltype: Float64 dims: draw, chain size: 1000×4
  :theta eltype: Float64 dims: draw, chain, school size: 1000×4×8
├─────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 1 entry:
  "created_at" => "2025-08-19T09:41:06.675"
posterior_predictive
1000×4×8 Dataset
├──────────────────┴──────────────────────────────────────────── dims ┐
  ↓ draw,
  → chain,
  ↗ school Categorical{String} ["Choate", …, "Mt. Hermon"] Unordered
├───────────────────────────────────────────────────────────── layers ┤
  :y_hat eltype: Float64 dims: draw, chain, school size: 1000×4×8
├─────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 1 entry:
  "created_at" => "2025-08-19T09:41:06.262"
log_likelihood
1000×4×8 Dataset
├──────────────────┴──────────────────────────────────────────── dims ┐
  ↓ draw,
  → chain,
  ↗ school Categorical{String} ["Choate", …, "Mt. Hermon"] Unordered
├───────────────────────────────────────────────────────────── layers ┤
  :log_lik eltype: Float64 dims: draw, chain, school size: 1000×4×8
├─────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 1 entry:
  "created_at" => "2025-08-19T09:41:06.584"
sample_stats
1000×4 Dataset
├────────────────┴─────────────────────────────────────────── dims ┐
  ↓ draw, → chain
├────────────────────────────────────────────────────────── layers ┤
  :tree_depth      eltype: Int64 dims: draw, chain size: 1000×4
  :energy          eltype: Float64 dims: draw, chain size: 1000×4
  :diverging       eltype: Bool dims: draw, chain size: 1000×4
  :acceptance_rate eltype: Float64 dims: draw, chain size: 1000×4
  :n_steps         eltype: Int64 dims: draw, chain size: 1000×4
  :lp              eltype: Float64 dims: draw, chain size: 1000×4
  :step_size       eltype: Float64 dims: draw, chain size: 1000×4
├──────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 1 entry:
  "created_at" => "2025-08-19T09:41:06.401"
observed_data
8-element Dataset
├───────────────────┴──────────────────────────────────────────── dims ┐
  ↓ school Categorical{String} ["Choate", …, "Mt. Hermon"] Unordered
├────────────────────────────────────────────────────────────── layers ┤
  :y eltype: Float64 dims: school size: 8
├──────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 1 entry:
  "created_at" => "2025-08-19T09:41:06.731"
plot_density(idata_stan; var_names=(:mu, :tau));

Here is a plot showing where the Hamiltonian sampler had divergences:

plot_pair(
    idata_stan;
    coords=Dict(:school => ["Choate", "Deerfield", "Phillips Andover"]),
    divergences=true,
);

Environment

using Pkg
Pkg.status()
Status `~/work/ArviZ.jl/ArviZ.jl/docs/Project.toml`
⌃ [cbdf2221] AlgebraOfGraphics v0.11.4
  [131c737c] ArviZ v0.12.3 `~/work/ArviZ.jl/ArviZ.jl`
  [2f96bb34] ArviZExampleData v0.2.0
  [4a6e88f0] ArviZPythonPlots v0.1.9
  [13f3f980] CairoMakie v0.15.6
  [a93c6f00] DataFrames v1.7.0
  [0703355e] DimensionalData v0.29.23
  [31c24e10] Distributions v0.25.120
  [e30172f5] Documenter v1.14.1
  [f6006082] EvoTrees v0.17.4
  [b5cf5a8d] InferenceObjects v0.4.12
  [be115224] MCMCDiagnosticTools v0.3.15
  [a7f614a8] MLJBase v1.8.2
  [614be32b] MLJIteration v0.6.3
  [ce719bf2] PSIS v0.9.8
  [7f36be82] PosteriorStats v0.2.8
  [c1514b29] StanSample v7.10.2
  [a19d573c] StatisticalMeasures v0.2.1
  [10745b16] Statistics v1.11.1
  [2913bbd2] StatsBase v0.34.6
  [fce5fe82] Turing v0.40.2
  [f43a241f] Downloads v1.6.0
  [37e2e46d] LinearAlgebra v1.11.0
Info Packages marked with ⌃ have new versions available and may be upgradable.
versioninfo()
Julia Version 1.11.6
Commit 9615af0f269 (2025-07-09 12:58 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × AMD EPYC 7763 64-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver3)
Threads: 2 default, 0 interactive, 1 GC (on 4 virtual cores)
Environment:
  JULIA_PKG_SERVER_REGISTRY_PREFERENCE = eager
  JULIA_PROJECT = @.
  JULIA_NUM_THREADS = 2
  JULIA_LOAD_PATH = @:@stdlib
  JULIA_PYTHONCALL_EXE = /home/runner/work/ArviZ.jl/ArviZ.jl/docs/.CondaPkg/.pixi/envs/default/bin/python