API Reference
The Backend
ClosedFormExpectations.ClosedFormExpectation — Type
ClosedFormExpectation()A backend of BayesBase's expectation protocol that computes expectations and their gradients in closed form. BayesBase.expectation(ClosedFormExpectation(), f, q) computes $\mathbb{E}_q[f]$, and BayesBase.expectation_gradient(ClosedFormExpectation(), f, q) its gradient with respect to the parameters of q, for the pairs listed in Supported Pairs. BayesBase.supports_expectation(ClosedFormExpectation(), f, q) and BayesBase.supports_expectation_gradient(ClosedFormExpectation(), f, q) say whether a pair is supported. The backend labels its results exact and is registered when the package is loaded. See The Expectation Protocol of BayesBase.
The gradients are the formulas written in the package. For a gradient that has no formula, but whose expectation has a closed form, see ClosedFormAD.
ClosedFormExpectations.ClosedFormAD — Type
ClosedFormAD(ad)A backend of BayesBase's expectation protocol that computes the gradient of an expectation by differentiating its closed form with automatic differentiation, using the identity
\[\mathbb{E}_q[f(x)\,\nabla_\eta \log q(x;\eta)] = \nabla_\eta \mathbb{E}_q[f(x)]\]
ad is an automatic differentiation backend from ADTypes.jl. AutoEnzyme() is supported once Enzyme.jl is loaded, see Enzyme Extension. The gradient is with respect to the natural parameters η of a univariate ExponentialFamilyDistribution{T}, wherever ClosedFormExpectation() has the expectation under T. The backend computes nothing else, so it is meant to follow ClosedFormExpectation() in a router, which then uses a formula where the package has one:
BayesBase.ExpectationFirstOf(ClosedFormExpectation(), ClosedFormAD(AutoEnzyme()), quadrature)Its results are labelled exact. It is not registered with BayesBase's dynamic router.
Computing Expectations and Gradients
The package computes through the expectation protocol of BayesBase:
# The expectation E_q[f(x)]
BayesBase.expectation(ClosedFormExpectation(), f, q)
# Its gradient with respect to the parameters θ of q, ∇_θ E_q[f(x)] = E_q[f(x) ∇_θ log q(x; θ)]
BayesBase.expectation_gradient(ClosedFormExpectation(), f, q)
# Whether a pair has a closed form
BayesBase.supports_expectation(ClosedFormExpectation(), f, q)
BayesBase.supports_expectation_gradient(ClosedFormExpectation(), f, q)f is the integrand. It can be a function such as log, the log-density Base.Fix1(logpdf, p) of a distribution p, or an expression such as Square() ∘ log or log ∘ ExpLogSquare(μ, σ). q is the distribution the expectation is taken under. A pair without a closed form throws a BayesBase.UnsupportedExpectationError.
The expectation is a number. The gradient is a vector, in the parametrization of q: with respect to params(q) for a distribution of Distributions.jl, with respect to the mean and the variance for a NormalMeanVariance, and with respect to the natural parameters for an ExponentialFamilyDistribution. For the exponential family form of a Categorical it is the full vector of length K in the softmax logits.
julia> using ClosedFormExpectations, BayesBase, Distributions
julia> BayesBase.expectation(ClosedFormExpectation(), log, Gamma(2.0, 1.0)) ≈ 1 - Base.MathConstants.eulergamma
true
julia> BayesBase.expectation_gradient(ClosedFormExpectation(), log, Gamma(2.0, 1.0)) ≈ [π^2 / 6 - 1, 1.0]
true
julia> BayesBase.supports_expectation(ClosedFormExpectation(), Base.Fix1(logpdf, Beta(2.0, 2.0)), Normal(0.0, 1.0))
falseExponentialFamily Support
The package works with the ExponentialFamilyDistribution objects of ExponentialFamily.jl. For an expectation, q is converted to its standard Distributions.jl form. The gradient is with respect to the natural parameters, and the package has formulas for:
NormalMeanVariance, through the Jacobian from $(\mu, \sigma)$ to $(\mu, v)$;ExponentialFamilyDistribution{NormalMeanVariance}, through the Jacobian to the natural parameters;ExponentialFamilyDistribution{Gamma}, likewise;ExponentialFamilyDistribution{Categorical}, the full softmax-logit score gradient, see Categorical Distribution.
For any other univariate ExponentialFamilyDistribution{T} whose expectation has a closed form, load Enzyme.jl and use ClosedFormAD(AutoEnzyme()) after ClosedFormExpectation() in a router, see the Enzyme extension.
ProductOf Support
The log-density of a ProductOf distribution from ExponentialFamily.jl is the sum of the log-densities of its parts:
\[\mathbb{E}_q[\log p_1(x) p_2(x)] = \mathbb{E}_q[\log p_1(x)] + \mathbb{E}_q[\log p_2(x)]\]
So Base.Fix1(logpdf, prod(GenericProd(), p₁, p₂)) has a closed form, and its gradient too, wherever both parts have one. Nested products are split the same way.
Migrating from 0.4
Version 1.0 computes everything through BayesBase's protocol. The mean methods, the ClosedWilliamsProduct strategy, the EnzymeBackend, EnzymeForward and EnzymeReverse types and the Logpdf wrapper are removed.
| 0.4 | 1.0 |
|---|---|
mean(ClosedFormExpectation(), f, q) | BayesBase.expectation(ClosedFormExpectation(), f, q) |
mean(ClosedWilliamsProduct(), f, q) | BayesBase.expectation_gradient(ClosedFormExpectation(), f, q) |
mean(ClosedWilliamsProduct(EnzymeBackend()), f, q) | BayesBase.expectation_gradient(ClosedFormAD(AutoEnzyme()), f, q), with AutoEnzyme from ADTypes.jl |
EnzymeBackend(EnzymeForward()) | AutoEnzyme(mode = Enzyme.Forward) |
LogGamma(α, β; check_args = false) with β ≤ 0, as a log-message | LogGammaKernel(α, β) |
Logpdf(p), or the distribution p as the integrand | Base.Fix1(logpdf, p) |
a closed form under an Exponential, a number | a vector of length one, with respect to the scale |