API

BayesianWorkflow.jl also reexports everything that Turing exports.

BayesianWorkflow.BWModel — Type
BWModel(model, data)

A wrapper that stores a model together with its associated data.

model is intended to be backend-agnostic. However, right now, only a Turing.jl model (i.e., model::DynamicPPL.Model) is accepted.

Turing.jl

The model itself should be written in a way that does not hardcode data variables in the model definition. For example, if y here is meant to be data:

@model function mymodel()
    x ~ Normal()
    y ~ Normal(x)
end

then y should not be specified as an argument, nor should mymodel() be conditioned on y. Instead, just pass mymodel() to BWModel and then provide the data for y as the second argument.

source
BayesianWorkflow.prior_predictive_check — Function
prior_predictive_check([rng::Random.AbstractRNG,] bm::BWModel, N::Int; nchains=1, kwargs...)

Sample N draws from the full generative model (i.e., from the joint prior over both parameters and data). Returns a FlexiChain containing draws of all variables in the model.

This is useful for checking whether the prior implies plausible data: inspect the draws of the data variables to see if they fall within a reasonable range.

Positional arguments

  • rng: Random number generator. Defaults to Random.default_rng().
  • bm: A BWModel wrapping the model and data.
  • N: Number of draws.

Keyword arguments

  • nchains: Number of independent chains to sample. Defaults to 1.
  • variables: Which variables to include in the returned chain. Can be :all, :parameters, or :data (default).
  • kwargs...: Additional keyword arguments passed to AbstractMCMC.sample.
source
BayesianWorkflow.posterior — Function
posterior([rng::Random.AbstractRNG,] bm::BWModel, N::Int; sampler=default_sampler(bm), nchains=1, kwargs...)

Draw N samples from the posterior distribution of the model parameters, conditioned on the data in bm. Returns a FlexiChain containing posterior draws of the parameters only.

Positional arguments

  • rng: Random number generator. Defaults to Random.default_rng().
  • bm: A BWModel wrapping the model and data.
  • N: Number of posterior draws per chain.

Keyword arguments

  • sampler: The sampler to use. By default this looks up BayesianWorkflow.default_sampler(bm).
  • nchains: Number of independent chains to sample. Defaults to 1.
  • kwargs...: Additional keyword arguments passed to AbstractMCMC.sample.
source
BayesianWorkflow.posterior_predictive_check — Function
posterior_predictive_check([rng::Random.AbstractRNG,] bm::BWModel, chain::FlexiChain)

Generate predicted data from the model using posterior parameter draws from chain. For each posterior draw, the data variables are re-sampled from the model, giving a FlexiChain of predicted data that can be compared against the observed data in bm.data.

This is useful for checking whether the fitted model can reproduce key features of the observed data.

Positional arguments

  • rng: Random number generator. Defaults to Random.default_rng().
  • bm: A BWModel wrapping the model and data.
  • chain: Posterior samples, typically obtained from posterior.

Keyword arguments

  • variables: Which variables to include in the returned chain. Can be :all, :parameters, or :data (default).
source