Custom Distributions

The package defines additional distribution types that are not part of Distributions.jl.

LogGamma

ClosedFormExpectations.LogGamma — Type
LogGamma(α, β)

The LogGamma distribution is a continuous probability distribution on the real numbers. It is defined as:

The probability density function of the LogGamma distribution is defined as:

```math
\mathcal{LG}(x \mid a, b) = \frac{e^{b x} e^{-e^{x}/a}}{a^{b} \Gamma(b)}, \quad -\infty < x < \infty, a > 0, b > 0.
```

Ref: https://www.math.wm.edu/~leemis/chart/UDR/PDFs/Loggamma.pdf

source

The LogGamma distribution is a continuous distribution on the real line with probability density function:

\[\mathcal{LG}(x \mid \alpha, \beta) = \frac{e^{\beta x} \, e^{-e^x / \alpha}}{\alpha^\beta \, \Gamma(\beta)}, \quad -\infty < x < \infty, \; \alpha > 0, \; \beta > 0\]

where $\alpha$ is the scale parameter and $\beta$ is the shape parameter. Both must be positive. For the unnormalised log-density with any $\beta$, such as a Gamma message pulled back through exp, use LogGammaKernel.

Reference: Log-Gamma distribution (William & Mary)

Usage

using ClosedFormExpectations, Distributions

d = LogGamma(2.0, 3.0)
logpdf(d, 0.5)

# Used as a target in expectations
BayesBase.expectation(ClosedFormExpectation(), Base.Fix1(logpdf, LogGamma(2.0, 3.0)), Normal(0, 1))

LogGammaKernel

ClosedFormExpectations.LogGammaKernel — Type
LogGammaKernel(α, β)

The unnormalised log-density of a LogGamma distribution, as an integrand:

\[f(x) = \beta x - e^{x} / \alpha, \quad \alpha > 0, \; \beta \in \mathbb{R}.\]

For $\beta > 0$ it differs from Base.Fix1(logpdf, LogGamma(α, β)) by the constant $\beta \log \alpha + \log \Gamma(\beta)$. For $\beta \le 0$ the density $e^{f}$ cannot be normalised, so there is no LogGamma distribution, but f is still a log-message: a Gamma message $z^{a-1} e^{-b z}$ pulled back through $z = e^{x}$ is LogGammaKernel(1 / b, a - 1), and for a Poisson count of zero $a = 1$. Under a univariate Normal its expectation and gradient have closed forms, for any $\beta$.

source

Usage

using ClosedFormExpectations, Distributions

# The message of a Poisson count of zero towards its log-rate, exp(-eˣ)
f = LogGammaKernel(1.0, 0.0)
BayesBase.expectation(ClosedFormExpectation(), f, Normal(0, 1))
BayesBase.expectation_gradient(ClosedFormExpectation(), f, Normal(0, 1))

ReLU Factor Messages

These types represent the exact factor-to-variable messages of a hard ReLU factor node $y = \max(0, x)$, given Gaussian incoming messages on either side.

ClosedFormExpectations.ReLUForwardMessage — Type
ReLUForwardMessage{T}

Represents the exact forward message from a ReLU factor node to its output variable $y$, given an incoming Gaussian message on $x$ with mean $m_x$ and variance $v_x$.

The full message is a mixed distribution:

\[m_{f \to y}(y) = \Phi\!\left(\frac{-m_x}{\sqrt{v_x}}\right)\delta(y) + \mathbf{1}_{y>0}\,\mathcal{N}(y;\,m_x,\,v_x)\]

The logpdf method returns the log density of the continuous component (defined for $y > 0$). For $y \leq 0$ it returns $-\infty$. The atom weight at $y = 0$ is $\Phi(-m_x / \sqrt{v_x})$.

source
ClosedFormExpectations.ReLUBackwardMessage — Type
ReLUBackwardMessage{T}

Represents the exact backward message from a ReLU factor node to its input variable $x$, given an incoming Gaussian message on $y$ with mean $m_y$ and variance $v_y$.

The message is proportional to $\mathcal{N}(\operatorname{ReLU}(x);\,m_y,\,v_y)$:

\[m_{f \to x}(x) \propto \begin{cases} \mathcal{N}(0;\,m_y,\,v_y), & x \leq 0,\\ \mathcal{N}(x;\,m_y,\,v_y), & x > 0. \end{cases}\]

This is an unnormalized density on $\mathbb{R}$: constant on the negative half-line and Gaussian-shaped on the positive half-line. The logpdf method returns $\log\mathcal{N}(\max(0,x);\,m_y,\,v_y)$.

source

Usage

using ClosedFormExpectations, Distributions, BayesBase

# Forward message: given Normal incoming on x, evaluate log density at y > 0
d_fwd = ReLUForwardMessage(1.0, 2.0)   # m_x = 1.0, v_x = 2.0
logpdf(d_fwd, 0.5)                     # log 𝒩(0.5; 1.0, 2.0)
logpdf(d_fwd, -0.1)                    # -Inf (no support for y ≤ 0)

# Backward message: given Normal incoming on y, evaluate log density at any x
d_bwd = ReLUBackwardMessage(1.0, 2.0)  # m_y = 1.0, v_y = 2.0
logpdf(d_bwd, 0.5)                     # log 𝒩(0.5; 1.0, 2.0)
logpdf(d_bwd, -0.5)                    # log 𝒩(0.0; 1.0, 2.0)  (constant for x ≤ 0)

# Closed-form expectations
BayesBase.expectation(ClosedFormExpectation(), Base.Fix1(logpdf, ReLUForwardMessage(1.0, 2.0)), Gamma(3.0, 1.0))
BayesBase.expectation(ClosedFormExpectation(), Base.Fix1(logpdf, ReLUBackwardMessage(1.0, 2.0)), Normal(0.5, 1.0))

LinearLogGamma

ClosedFormExpectations.LinearLogGamma — Type
LinearLogGamma(α, β, weights)

An unnormalized multivariate distribution derived from the LogGamma distribution. (see LogGamma)

The LinearLogGamma distribution is an distribution on a multidimensional x, derived from the LogGamma distribution. It is defined as:

\[ LLG(x | lpha, eta, w) = LG(x^T w | a, b),\]

where weights is a fixed vector of covariates, and lpha and eta are the scale and shape parameters of the LogGamma distribution, respectively. Fields

α::T: The scale parameter of the LogGamma distribution.

β::T: The shape parameter of the LogGamma distribution.

weights::C: The fixed vector of covariates.

source

The LinearLogGamma distribution is a multivariate distribution derived from the LogGamma distribution via a linear projection:

\[\mathrm{LLG}(\mathbf{x} \mid \alpha, \beta, \mathbf{w}) = \mathcal{LG}(\mathbf{w}^\top \mathbf{x} \mid \alpha, \beta)\]

where $\mathbf{w}$ is a fixed vector of weights/covariates.

Usage

using ClosedFormExpectations, Distributions

d = LinearLogGamma(2.0, 3.0, [0.5, 0.3, 0.2])
logpdf(d, [1.0, 2.0, 3.0])

# Used as a target in expectations with multivariate Normal
BayesBase.expectation(ClosedFormExpectation(), Base.Fix1(logpdf, LinearLogGamma(2.0, 3.0, [0.5, 0.3])), MvNormal([0.0, 0.0], [1.0 0.0; 0.0 1.0]))