Introduction

This package serves two purposes:

  1. Introduce a common API for packages that operate on log densities, which for these purposes are black box $\mathbb{R}^n \to \mathbb{R}$ mappings. Using the interface of introduced in this package, you can query $n$, evaluate the log density and optionally its gradient, and determine if a particular object supports these methods using traits. This usage is relevant primarily for package developers who write generic algorithms that use (log) densities that correspond to posteriors and likelihoods, eg MCMC, MAP, ML. An example is DynamicHMC.jl. This is documented in the API section.

  2. Make it easier for users who want to perform inference using the above methods (and packages) to

    • define their own log densities, either taking a vector of real numbers as input, or extracting and transforming parameters using the TransformVariables.jl package,
    • obtain gradients of these log densities using one of the supported automatic differentiation packages of Julia.

This is documented in the next section, with a worked example.

For the purposes of this package, log densities are still valid when shifted by a constant that may be unknown, but is consistent within calls. This is necessary for Bayesian inference, where log posteriors are usually calculated up to a constant. See LogDensityProblems.logdensity for details.

Working with log density problems

Consider an inference problem where IID draws are obtained from a normal distribution,

\[x_i \sim \mathcal{N}(\mu, \sigma)\]

for $i = 1, \dots, N$. It can be shown that the log likelihood conditional on $\mu$ and $\sigma$ is

\[\ell = -N\log \sigma - \sum_{i = 1}^N \frac{(x_i-\mu)^2}{2\sigma^2} = -N\left( \log \sigma + \frac{S + (\bar{x} - \mu)^2}{2\sigma^2} \right)\]

where we have dropped constant terms, and defined the sufficient statistics

\[\bar{x} = \frac{1}{N} \sum_{i = 1}^N x_i\]

and

\[S = \frac{1}{N} \sum_{i = 1}^N (x_i - \bar{x})^2\]

Finally, we use priors

\[\mu \sim \mathcal{N}(0, 5), \sigma \sim \mathcal{N}(0, 2)\]

which yield the log prior

\[-\sigma^2/8 - \mu^2/50\]

which is added to the log likelihood to obtain the log posterior.

It is useful to define a callable that implements this, taking some vector x as an input and calculating the summary statistics, then, when called with a NamedTuple containing the parameters, evaluating to the log posterior.

using Statistics, SimpleUnPack # imported for our implementation

struct NormalPosterior{T} # contains the summary statistics
    N::Int
    x̄::T
    S::T
end

# calculate summary statistics from a data vector
function NormalPosterior(x::AbstractVector)
    NormalPosterior(length(x), mean(x), var(x; corrected = false))
end

# define a callable that unpacks parameters, and evaluates the log likelihood
function (problem::NormalPosterior)(θ)
    @unpack μ, σ = θ
    @unpack N, x̄, S = problem
    loglikelihood = -N * (log(σ) + (S + abs2(μ - x̄)) / (2 * abs2(σ)))
    logprior = - abs2(σ)/8 - abs2(μ)/50
    loglikelihood + logprior
end

problem = NormalPosterior(randn(100))

Let's try out the posterior calculation:

julia> problem((μ = 0.0, σ = 1.0))-48.60297471151442
Note

Just evaluating your log density function like above is a great way to test and benchmark your implementation. See the “Performance Tips” section of the Julia manual for optimization advice.

Using the TransformVariables package

In our example, we require $\sigma > 0$, otherwise the problem is meaningless. However, many MCMC samplers prefer to operate on unconstrained spaces $\mathbb{R}^n$. The TransformVariables package was written to transform unconstrained to constrained spaces, and help with the log Jacobian correction (more on that later). That package has detailed documentation, now we just define a transformation from a length 2 vector to a NamedTuple with fields μ (unconstrained) and σ > 0.

Note

Since version 1.0, TransformedLogDensity has been moved to the package TransformedLogDensities.

julia> using LogDensityProblems, TransformVariables, TransformedLogDensities
julia> ℓ = TransformedLogDensity(as((μ = asℝ, σ = asℝ₊)), problem)TransformedLogDensity of dimension 2

Then we can query the dimension of this problem, and evaluate the log density:

julia> LogDensityProblems.dimension(ℓ)2
julia> LogDensityProblems.logdensity(ℓ, zeros(2))-48.60297471151442
Note

Before running time-consuming algorithms like MCMC, it is advisable to test and benchmark your log density evaluations separately. The same applies to LogDensityProblems.logdensity_and_gradient and LogDensityProblems.logdensity_gradient_and_hessian.

Manual unpacking and transformation

If you prefer to implement the transformation yourself, you just have to define the following three methods for your problem: declare that it can evaluate log densities (but not their gradient, hence the 0 order), allow the dimension of the problem to be queried, and then finally code the density calculation with the transformation. (Note that using TransformedLogDensities.TransformedLogDensity takes care of all of these for you, as shown above).

function LogDensityProblems.capabilities(::Type{<:NormalPosterior})
    LogDensityProblems.LogDensityOrder{0}()
end

LogDensityProblems.dimension(::NormalPosterior) = 2

function LogDensityProblems.logdensity(problem::NormalPosterior, x)
    μ, logσ = x
    σ = exp(logσ)
    problem((μ = μ, σ = σ)) + logσ
end
julia> LogDensityProblems.logdensity(problem, zeros(2))-48.60297471151442

Here we use the exponential function to transform from $\mathbb{R}$ to the positive reals, but this requires that we correct the log density with the logarithm of the Jacobian, which here happens to be $\log(\sigma)$.

Automatic differentiation

Using either definition, you can transform to another object which is capable of evaluating the gradient, using automatic differentiation. For this, you need the LogDensityProblemsAD.jl package.

Now observe that we can obtain gradients, too:

julia> import ForwardDiff
julia> using LogDensityProblemsAD
julia> ∇ℓ = ADgradient(:ForwardDiff, ℓ)ForwardDiff AD wrapper for TransformedLogDensity of dimension 2, w/ chunk size 2
julia> LogDensityProblems.capabilities(∇ℓ)LogDensityProblems.LogDensityOrder{1}()
julia> LogDensityProblems.logdensity_and_gradient(∇ℓ, zeros(2))(-48.60297471151442, [2.8381559225185673, -2.294050576971158])
Note

Before version 2.0, ADgradient was part of this package. To update older code, just add using LogDensityProblemsAD.

Manually calculated derivatives

If you prefer not to use automatic differentiation, you can wrap your own derivatives following the template

function LogDensityProblems.capabilities(::Type{<:NormalPosterior})
    LogDensityProblems.LogDensityOrder{1}() # can do gradient
end

LogDensityProblems.dimension(::NormalPosterior) = 2 # for this problem

function LogDensityProblems.logdensity_and_gradient(problem::NormalPosterior, x)
    logdens = ...
    grad = ...
    logdens, grad
end
Note

If the gradient is a mutable vector (eg Vector), it should not be reused for another purpose. Practically, each call to LogDensityProblems.logdensity_and_gradient should allocate a new one, or use immutables like StaticArrays.SVector for small dimensions.

Various utilities

You may find these utilities useful for debugging and optimization.

LogDensityProblems.stresstestFunction
stresstest(f, ℓ; N, rng, scale)

Test with random values.

N random vectors are drawn from a standard multivariate Cauchy distribution, scaled with scale (which can be a scalar or a conformable vector).

Each random vector x is then used as an argument in f(ℓ, x). logdensity, logdensity_and_gradient, and logdensity_gradient_and_hessian are recommended for f.

In case the call produces an error, the value is recorded as a failure, which are returned by the function.

Not exported, but part of the API.

source

Log densities API

Use the functions below for evaluating gradients and querying their dimension and other information. These symbols are not exported, as they are mostly used by package developers and in any case would need to be imported or qualified to add methods to.

LogDensityProblems.capabilitiesFunction
capabilities(T)

Test if the type (or a value, for convenience) supports the log density interface.

When nothing is returned, it doesn't support this interface. When LogDensityOrder{K}() is returned (typically with K == 0, K = 1, or K == 2), derivatives up to order K are supported. All other return values are invalid.

Interface description

The following methods need to be implemented for the interface:

  1. dimension returns the dimension of the domain,

  2. logdensity evaluates the log density at a given point.

  3. logdensity_and_gradient when K ≥ 1.

  4. logdensity_gradient_and_hessian when K ≥ 2.

See also LogDensityProblems.stresstest for stress testing.

source
LogDensityProblems.LogDensityOrderType
struct LogDensityOrder{K}

A trait that means that a log density supports evaluating derivatives up to order K.

Typical values for K are 0 (just the log density) and 1 (log density and gradient).

source
LogDensityProblems.logdensityFunction
logdensity(ℓ, x)

Evaluate the log density at x, which has length compatible with its dimension.

Return a real number, which may or may not be finite (can also be NaN). Non-finite values other than -Inf are invalid but do not error, caller should deal with these appropriately.

Note about constants

Log densities can be shifted by the same constant, as long as it is consistent between calls. For example,

logdensity(::StandardMultivariateNormal) = -0.5 * sum(abs2, x)

is a valid implementation for some callable StandardMultivariateNormal that would implement the standard multivariate normal distribution (dimension $k$) with pdf

\[(2\pi)^{-k/2} e^{-x'x/2}\]

source
LogDensityProblems.logdensity_and_gradientFunction
logdensity_and_gradient(ℓ, x)

Evaluate the log density and its gradient at x, which has length compatible with its dimension.

Return two values:

  • the log density as real number, which equivalent to logdensity(ℓ, x)

  • if the log density is finite, the gradient, an ::AbstractVector of real numbers, otherwise this value is arbitrary and should be ignored.

Note

Caller may assume ownership of results, ie that the gradient vector will not be overwritten or reused for a different purpose.

The first argument (the log density) can be shifted by a constant, see the note for logdensity.

source
LogDensityProblems.logdensity_gradient_and_hessianFunction
logdensity_gradient_and_hessian(ℓ, x)

Evaluate the log density , its gradient, and Hessian at x, which has length compatible with its dimension.

Return three values:

  • the log density as real number, which equivalent to logdensity(ℓ, x)

  • if the log density is finite, the gradient, an ::AbstractVector of real numbers, otherwise this value is arbitrary and should be ignored.

  • if the log density is finite, the Hessian, an ::AbstractMatrix of real numbers, otherwise this value is arbitrary and should be ignored.

Note

Caller may assume ownership of results, ie that the gradient and Hessian will not be overwritten or reused for a different purpose.

The first argument (the log density) can be shifted by a constant, see the note for logdensity.

source