Expression Types

The package defines an Expression abstract type and several concrete expression types that can be used as the integrand f in BayesBase.expectation(ClosedFormExpectation(), f, q). These types support function composition via the ∘ operator.

ExpLogSquare

Represents the function:

\[\text{ExpLogSquare}(\mu, \sigma)(x) = \exp\left(-\frac{(\log x - \mu)^2}{2\sigma^2}\right)\]

Typically used in composed form with log:

# Computes E_q[-(log x - μ)² / (2σ²)]
BayesBase.expectation(ClosedFormExpectation(), log ∘ ExpLogSquare(μ, σ), q)

Square

Represents $x^2$. Typically used composed with log to compute expectations of $(\log x)^2$:

# Computes E_q[(log x)²]
BayesBase.expectation(ClosedFormExpectation(), Square() ∘ log, q)

Power

Represents $x^N$ for compile-time constant $N$. Used composed with log:

# Computes E_q[(log x)³]
BayesBase.expectation(ClosedFormExpectation(), Power(Val(3)) ∘ log, q)

Abs

Represents the absolute value function $|x|$:

# Computes E_q[|x|] where q is Normal
BayesBase.expectation(ClosedFormExpectation(), Abs(), Normal(0, 1))

Probit

ClosedFormExpectations.Probit — Type
Probit(a, b)
Probit()

Expression type representing the probit function $\Phi(a x + b)$ for scalar $a, b$ (default $a = 1, b = 0$), or $\Phi(\mathbf{w}^\top \mathbf{x} + b)$ for a weight vector $\mathbf{w}$.

source

Represents the probit function $\Phi(a x + b)$ (or $\Phi(\mathbf{w}^\top \mathbf{x} + b)$ for a weight vector $\mathbf{w}$):

# Computes E_q[Φ(1.5x - 0.3)] where q is Normal
BayesBase.expectation(ClosedFormExpectation(), ClosedFormExpectations.Probit(1.5, -0.3), Normal(0, 1))

# Computes E_q[Φ(wᵀx + b)] where q is MvNormal
BayesBase.expectation(ClosedFormExpectation(), ClosedFormExpectations.Probit([0.5, -1.0], 0.2), MvNormal(zeros(2), [1.0 0.0; 0.0 1.0]))

GELU

Represents the exact Gaussian Error Linear Unit $x \Phi(x)$:

# Computes E_q[x Φ(x)] where q is Normal
BayesBase.expectation(ClosedFormExpectation(), ClosedFormExpectations.GELU(), Normal(0, 1))

Product

ClosedFormExpectations.Product — Type
Product(multipliers)

Expression type representing a product of multiple functions. When composed with log, the product decomposes into a sum of logarithms.

source

Represents a product of multiple expressions. When composed with log, the logarithm of the product decomposes into a sum:

\[\mathbb{E}_q[\log(f_1 \cdot f_2 \cdots f_n)] = \sum_i \mathbb{E}_q[\log f_i]\]

Standalone Functions

xlog2x

Computes $x (\log x)^2$ with proper handling of $x = 0$.

xlogx

The package also uses xlogx from LogExpFunctions.jl, which computes $x \log x$.