Functions and densities
Define deterministic calculations once and include their generated Stan implementations in a probabilistic model.
Deterministic functions
@deffun defines a Stan function. Argument and result annotations specify carrier types or dispatch when needed. @juliacompat additionally permits a supported deterministic definition to be called from ordinary Julia.
using StanBlocks
@deffun @juliacompat saturating_response(x::real, half::real)::real = x / (half + x)
show(stdout, MIME"text/plain"(), saturating_response(2.0, 3.0)); println()0.4The same definition is included when a model calls it. Unlike a submodel, a function introduces no probability statements or latent parameters of its own.
An @deffun @inline helper expands into its caller while resolving sibling helpers and omitted keyword defaults in its defining module. Explicit arguments resolve in the caller's module, as do statements after the call.
Loading ReactiveKernels 0.1.175 or newer alongside StanBlocks enables an optional source provider for complete named positional @deffun @juliacompat methods. ReactiveKernels' jax_source can translate their loops and branches, including host prepare_data; dimension guards remain active. Source follows Julia dispatch and survives precompilation. Replacing a method stops use of its old source. Keyword, default, vararg and anonymous arguments retain the existing lowered-code route. Ordinary dense matrix_exp calls use JAX/SciPy's matrix exponential; padded ragged matrices are unsupported. See the ReactiveKernels JAX guide for the supported source subset. Native execution and Stan emission retain their existing implementations.
response = @slic (; x=2.0, y=0.45) begin
half ~ lognormal(0, 1)
expected = saturating_response(x, half)
y ~ normal(expected, 0.1)
end
d = stan_descriptor(response)
show(stdout, MIME"text/plain"(), [(name=f.name, signature=f.signature) for f in d.definitions]); println()2-element Vector{@NamedTuple{name::Symbol, signature::String}}:
(name = :saturating_response, signature = "real saturating_response(real x, real half)")
(name = :normal_lpdfs, signature = "real normal_lpdfs(real args1, real args2, real args3)")The descriptor lists the actual emitted function signatures. stan_definition retrieves one definition, and stan_definition_closure retrieves its dependency closure. Use these APIs when building an editor or exporter; do not maintain a parallel list of helper names.
Control flow
A function body may use conditional branches, loops, local buffers and indexed assignment. A model body instead describes probability statements and uses the annotated loops. This distinction keeps a numerical algorithm's local work separate from inferential parameters.
@deffun absolute_sum(x::vector[n])::real = begin
total = 0.0
for i in 1:n
if x[i] < 0
total += -x[i]
else
total += x[i]
end
end
total
end
with_function = @slic (; x=[-1.0, 0.5], y=1.4) begin
scale ~ lognormal(0, 1)
y ~ normal(scale * absolute_sum(x), 1)
end
println(stan_code(with_function))functions {
// Return the normalized log probability for a single observation.
real normal_lpdfs(
real args1,
real args2,
int args3
) {
return normal_lpdf(args1 | args2, args3);
}
real absolute_sum(
vector x
) {
int n = dims(x)[1];
real total = 0.0;
for(i in 1:n) {
if((x[i] < 0)) {
total += (-x[i]);
} else {
total += x[i];
}
}
return total;
}
}
data {
real y;
int x_n;
vector[x_n] x;
}
transformed data {
}
parameters {
real<lower=0.0> scale;
}
transformed parameters {
}
model {
scale ~ lognormal(0, 1);
y ~ normal((scale * absolute_sum(x)), 1);
}
generated quantities {
real y_likelihood = normal_lpdfs(y, (scale * absolute_sum(x)), 1);
real y_gen = normal_rng((scale * absolute_sum(x)), 1);
}A @deffun is Stan-only unless Julia compatibility is requested. Do not call a Stan-only helper from Julia preprocessing. Keep preprocessing as ordinary Julia code, or use @juliacompat when the required operators have Julia methods. Module-level numeric constants are not implicit data arguments. Pass values explicitly; built-in constants such as pi use their Julia spelling.
Probability families
A custom family can provide an aggregate density, pointwise contributions and a random-number generator. These serve different operations. @lpxf registers the density, while @lhs supplies left-hand-side inference when the family needs to determine a latent carrier. family_support reports the observation type and intrinsic support; ValueFamily and ValueUDF are alternatives for producers that construct families as values.
A custom density must have the intended support, normalization and transformation Jacobian. A function that returns zero can be an explicitly chosen improper kernel, but it does not establish a uniform probability law on a transformed space. A density implementation alone also does not supply a predictive random-number generator. Check the operations the descriptor actually offers before attempting prediction.
Use the model API reference for these registration interfaces. The epidemic example shows a typed ODE right-hand side, its emitted Stan definition and a numerical trajectory.