Skip to content

API Reference

Model Definition

StanBlocks.@slic Macro

Defines SlicModels (see test/slic.jl for usage examples).

The defining module is captured automatically via __module__, so that @deffun functions defined in the same module (e.g. a package extension) are found during symbol resolution.

source
StanBlocks.@deffun Macro
julia
@deffun function_definition

Define a Stan-compatible function with type inference and code generation.

Parses a Julia-style function definition (with type-annotated arguments and return type), generates the corresponding Stan function, and registers type-inference signatures so the transpiler can propagate types through calls to this function.

For functions ending in _lpdf/_lpmf/_lcdf/_lccdf, the return type is automatically set to real and companion _lpdfs/_rng stubs are generated for use in generated_quantities.

Example

julia
@deffun garch11_lpdf(y::vector[T], mu::real, alpha0::real, alpha1::real, beta1::real)::real = begin
    sigma2 = alpha0
    rv = 0.
    for t in 1:T
        rv += normal_lpdf(y[t], mu, sqrt(sigma2))
        sigma2 = alpha0 + alpha1 * square(y[t] - mu) + beta1 * sigma2
    end
    return rv
end

See src/slic_stan/builtin.jl for many more examples.

source
StanBlocks.@defsig Macro

Utility macro to define function signatures (see src/slic_stan/builtin.jl for usage examples).

Note:

This macro is mainly useful for bulk built-in function signature definitions. StanBlocks.jl users should generally prefer using @deffun.

source

Model Inspection and Compilation

StanBlocks.stan_code Function

Return the stan code of its first argument (a StanBlocks.SlicModel or a StanBlocks.StanModel) as a string.

source
StanBlocks.stan_model Function

Traces through its first argument (a StanBlocks.SlicModel) and returns the inferred StanBlocks.StanModel.

source
StanBlocks.stan_instantiate Function

Returns the StanLogDensityProblem (a compiled posterior).

Warning:

Requires loading StanLogDensityProblems.jl and JSON.jl.

source

Types

StanBlocks.SlicModel Type

The AST and the data, pre-tracing. Can be instantiated via stan_instantiate.

The mod field stores the defining module (set automatically by @slic), used for symbol resolution during tracing — functions defined via @deffun in package extensions are found by checking mod before falling back to Main.

Warning:

Repeatedly instantiating SlicModels is inefficient, as the tracing is redone for every instantiation. Instead, get the StanModel first (via model = stan_model(slic_model)) and update its data (via new_model = model(;x=new_x)).

source
StanBlocks.StanModel Type

The inferred Stan model, post-tracing. Can be instantiated via stan_instantiate.

source

Errors

StanBlocks.StanBlocksError Type
julia
StanBlocksError <: Exception

Wraps errors that occur during transpilation, compilation, or evaluation of Stan models.

Fields

  • phase::Symbol: the pipeline stage where the error occurred (:transpile, :compile, or :evaluate)

  • context::String: a description of what was being processed (e.g. "model: eight_schools")

  • cause::Any: the underlying error, typically an (exception, backtrace) tuple

source
You are viewing the dev branch. This branch may include code written with Claude Code with less human supervision. Only human-approved code is merged into main.