Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logdensity_gradient_and_hessian. #101

Merged
merged 2 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ LogDensityProblems.logdensity(ℓ, zeros(2))
```

!!! 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`](@ref).
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`](@ref) and [`LogDensityProblems.logdensity_gradient_and_hessian`](@ref).

## Manual unpacking and transformation

Expand Down Expand Up @@ -184,4 +184,5 @@ LogDensityProblems.LogDensityOrder
LogDensityProblems.dimension
LogDensityProblems.logdensity
LogDensityProblems.logdensity_and_gradient
LogDensityProblems.logdensity_gradient_and_hessian
```
38 changes: 33 additions & 5 deletions src/LogDensityProblems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ $(SIGNATURES)

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` or `K = 1`), derivatives up to order `K` are supported.
*All other return values are invalid*.
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

Expand All @@ -59,6 +60,8 @@ The following methods need to be implemented for the interface:

3. [`logdensity_and_gradient`](@ref) when `K ≥ 1`.

4. [`logdensity_gradient_and_hessian`](@ref) when `K ≥ 2`.

See also [`LogDensityProblems.stresstest`](@ref) for stress testing.
"""
capabilities(T::Type) = nothing
Expand Down Expand Up @@ -105,8 +108,8 @@ function logdensity end
"""
logdensity_and_gradient(ℓ, x)

Evaluate the log density `ℓ` and its gradient at `x`, which has length compatible with its
[`dimension`](@ref).
Evaluate the log density `ℓ` and its gradient at `x`, which has length
compatible with its [`dimension`](@ref).

Return two values:

Expand All @@ -124,6 +127,31 @@ The first argument (the log density) can be shifted by a constant, see the note
"""
function logdensity_and_gradient end

"""
logdensity_gradient_and_hessian(ℓ, x)

Evaluate the log density `ℓ`, its gradient, and Hessian at `x`, which
has length compatible with its [`dimension`](@ref).

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,
devmotion marked this conversation as resolved.
Show resolved Hide resolved
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`](@ref).
"""
function logdensity_gradient_and_hessian end

include("utilities.jl")

end # module
5 changes: 3 additions & 2 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ 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 is then used as an argument in `f(ℓ, ...)`. `logdensity` and
`logdensity_and_gradient` are recommended for `f`.
Each random vector `x` is then used as an argument in `f(ℓ, x)`. [`logdensity`](@ref),
[`logdensity_and_gradient`](@ref), and [`logdensity_gradient_and_hessian`](@ref) are
recommended for `f`.

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