Evaluation Modes

JuliaBUGS supports multiple evaluation modes that determine how the log density is computed. The evaluation mode also constrains which AD backends can be used.

Available Modes

ModeDescriptionAD Backends
UseGraph()Traverses computational graph (default)ReverseDiff, ForwardDiff
UseGeneratedLogDensityFunction()Compiles a Julia function for log densityMooncake
UseAutoMarginalization()Graph traversal with discrete variable marginalizationReverseDiff, ForwardDiff

UseGraph (Default)

The default mode evaluates the log density by traversing the computational graph. Works with ReverseDiff and ForwardDiff.

model = compile(model_def, data)
# UseGraph() is the default, no need to set explicitly

UseGeneratedLogDensityFunction

This mode generates and compiles a Julia function for the log density, which can be faster for some models.

model = compile(model_def, data)
model = set_evaluation_mode(model, UseGeneratedLogDensityFunction())

Use with Mooncake for AD:

model = BUGSModelWithGradient(model, AutoMooncake(; config=nothing))

UseAutoMarginalization

For models with discrete latent variables, auto-marginalization enables gradient-based inference by marginalizing out discrete parameters. See Auto-Marginalization for details.

model = compile(model_def, data)
model = settrans(model, true)  # requires transformed space
model = set_evaluation_mode(model, UseAutoMarginalization())

API

JuliaBUGS.Model.set_evaluation_modeFunction
set_evaluation_mode(model::BUGSModel, mode::EvaluationMode)

Set the evaluation mode for the BUGSModel.

The evaluation mode determines how the log-density of the model is computed. Possible modes are:

  • UseGeneratedLogDensityFunction(): Uses a statically generated function for log-density computation. This is often faster but may not be available for all models. The function is generated when switching to this mode. If generation fails, a warning is issued and the mode defaults to UseGraph().
  • UseGraph(): Computes the log-density by traversing the model's graph structure. This is always available but might be slower.

Arguments

  • model::BUGSModel: The BUGS model instance.
  • mode::EvaluationMode: The desired evaluation mode.

Returns

  • A new BUGSModel instance with the evaluation_mode field updated. If the original model is mutable, it might be modified in place.

Examples

# Assuming `model` is a compiled BUGSModel instance
model_with_graph_eval = set_evaluation_mode(model, UseGraph())
model_with_generated_eval = set_evaluation_mode(model, UseGeneratedLogDensityFunction())
source