Skip to content

Commit

Permalink
Merge pull request #72 from JuliaGaussianProcesses/positive-semidefinite
Browse files Browse the repository at this point in the history
Add epsilon to `PositiveDefinite`
  • Loading branch information
simsurace authored Feb 26, 2024
2 parents 902fbe2 + 0c5a728 commit febc192
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ParameterHandling"
uuid = "2412ca09-6db7-441c-8e3a-88d5709968c5"
authors = ["Invenia Technical Computing Corporation"]
version = "0.4.10"
version = "0.5.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
27 changes: 16 additions & 11 deletions src/parameters_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,36 @@ function flatten(::Type{T}, X::Orthogonal) where {T<:Real}
end

"""
positive_definite(X::AbstractMatrix{<:Real})
positive_definite(X::AbstractMatrix{<:Real}, ε = eps(T))
Produce a parameter whose `value` is constrained to be a positive-definite matrix. The argument `X` needs to
be a positive-definite matrix (see https://en.wikipedia.org/wiki/Definite_matrix).
Produce a parameter whose `value` is constrained to be a strictly positive-definite
matrix. The argument `X` minus `ε` times the identity needs to be a positive-definite matrix
(see https://en.wikipedia.org/wiki/Definite_matrix). The optional second argument `ε` must
be a positive real number.
The unconstrained parameter is a `LowerTriangular` matrix, stored as a vector.
"""
function positive_definite(X::AbstractMatrix{<:Real})
isposdef(X) || throw(ArgumentError("X is not positive-definite"))
return PositiveDefinite(tril_to_vec(cholesky(X).L))
function positive_definite(X::AbstractMatrix{T}, ε=eps(T)) where {T<:Real}
ε > 0 || throw(ArgumentError("ε must be positive."))
_X = X - ε * I
isposdef(_X) || throw(ArgumentError("X-ε*I is not positive-definite for ε="))
return PositiveDefinite(tril_to_vec(cholesky(_X).L), ε)
end

struct PositiveDefinite{TL<:AbstractVector{<:Real}} <: AbstractParameter
struct PositiveDefinite{TL<:AbstractVector{<:Real},Tε<:Real} <: AbstractParameter
L::TL
ε::Tε
end

Base.:(==)(X::PositiveDefinite, Y::PositiveDefinite) = X.L == Y.L

A_At(X) = X * X'

value(X::PositiveDefinite) = A_At(vec_to_tril(X.L))
Base.:(==)(X::PositiveDefinite, Y::PositiveDefinite) = X.L == Y.L && X.ε == Y.ε

value(X::PositiveDefinite) = A_At(vec_to_tril(X.L)) + X.ε * I

function flatten(::Type{T}, X::PositiveDefinite) where {T<:Real}
v, unflatten_v = flatten(T, X.L)
unflatten_PositiveDefinite(v_new::Vector{T}) = PositiveDefinite(unflatten_v(v_new))
unflatten_PositiveDefinite(v_new::Vector{T}) = PositiveDefinite(unflatten_v(v_new), X.ε)
return v, unflatten_PositiveDefinite
end

Expand Down
20 changes: 12 additions & 8 deletions test/parameters_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ using ParameterHandling: vec_to_tril, tril_to_vec
end
X_mat = ParameterHandling.A_At(rand(3, 3)) # Create a positive definite object
X = positive_definite(X_mat)
@test X == X
@test value(X) X_mat
@test isposdef(value(X))
@test vec_to_tril(X.L) cholesky(X_mat).L
@test_throws ArgumentError positive_definite(rand(3, 3))

# Zeroing the unconstrained value preserve positivity
X.L .= 0
@test isposdef(value(X))

# Check that zeroing the unconstrained value does not affect the original array
@test X != positive_definite(X_mat)

@test positive_definite(X_mat, 1e-5) != positive_definite(X_mat, 1e-6)
@test_throws ArgumentError positive_definite(zeros(3, 3))
@test_throws ArgumentError positive_definite(X_mat, 0.0)

test_parameter_interface(X)

x, re = flatten(X)
Expand All @@ -46,12 +56,6 @@ using ParameterHandling: vec_to_tril, tril_to_vec
return logdet(value(X))
end,
)
ΔL = first(
Zygote.gradient(vec_to_tril(X.L)) do L
return logdet(L * L')
end,
)
@test vec_to_tril(Δl) == tril(ΔL)
ChainRulesTestUtils.test_rrule(vec_to_tril, x)
end
end

2 comments on commit febc192

@simsurace
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/101681

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.0 -m "<description of version>" febc192bfd2929e22e49e1a27b1b6e1e6a958110
git push origin v0.5.0

Please sign in to comment.