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
ClosedFormExpectations.ExpLogSquare — Type
ExpLogSquare(μ, σ)ExpLogSquare is a type that represents the exp(-(log(x) - μ)^2/(2σ^2)) function.
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
ClosedFormExpectations.Square — Type
SquareSquare is a type that represents the x^2 function.
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
ClosedFormExpectations.Power — Type
Power
Power is a type that represents the x^N function.
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
ClosedFormExpectations.Abs — Type
AbsExpression type representing the absolute value function $|x|$.
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}$.
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
ClosedFormExpectations.GELU — Type
GELU()Expression type representing the exact Gaussian Error Linear Unit $x \Phi(x)$.
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.
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
ClosedFormExpectations.xlog2x — Function
Return x * log(x)^2 for x ≥ 0, handling $x = 0$ by taking the downward limit.
julia> xlog2x(0)
0.0Computes $x (\log x)^2$ with proper handling of $x = 0$.
xlogx
The package also uses xlogx from LogExpFunctions.jl, which computes $x \log x$.