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 mv normal mean scale precision manifold #22

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"

[compat]
BayesBase = "1.3"
ExponentialFamily = "1.5.1"
ExponentialFamily = "1.6.0"
LinearAlgebra = "1.10"
Manifolds = "0.9"
ManifoldsBase = "0.15"
Expand Down
24 changes: 24 additions & 0 deletions src/natural_manifolds/normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,28 @@ function partition_point(
)
k = first(dims)
return ArrayPartition(view(p, 1:k), reshape(view(p, (k + 1):(k + k^2)), (k, k)))
end

"""
get_natural_manifold_base(::Type{MvNormalMeanScalePrecision}, dims::Tuple{Int}, conditioner = nothing)

Get the natural manifold base for the `MvNormalMeanScalePrecision` distribution.
"""
function get_natural_manifold_base(
::Type{MvNormalMeanScalePrecision}, dims::Tuple{Int}, conditioner=nothing
)
k = first(dims)
return ProductManifold(Euclidean(k), ShiftedNegativeNumbers(static(0)))
end

"""
partition_point(::Type{MvNormalMeanCovariance}, dims::Tuple{Int}, p, conditioner = nothing)

Converts the `point` to a compatible representation for the natural manifold of type `MvNormalMeanCovariance`.
"""
function partition_point(
::Type{MvNormalMeanScalePrecision}, dims::Tuple{Int}, p, conditioner=nothing
)
k = first(dims)
return ArrayPartition(view(p, 1:k), view(p, k+1:k+1))
end
2 changes: 1 addition & 1 deletion test/manopt_setuptests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function (sc::StopWhenGradientNormLessNonAllocating)(mp, s, i)
return false
end

# Non allocating version of the same `ConstantStepsize` from `Manopt.jl`
# Non allocating version of the same `ConstantLength` from `Manopt.jl`
struct ConstantStepsizeNonAllocating{T} <: Stepsize
stepsize::T
end
Expand Down
11 changes: 11 additions & 0 deletions test/natural_manifolds/normal_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@ end
C = L * L' + k * I
return MvNormalMeanCovariance(m, C)
end
end

@testitem "Check `MvNormalMeanScalePrecision` natural manifold" begin
include("natural_manifolds_setuptests.jl")

test_natural_manifold() do rng
k = rand(rng, 1:10)
m = randn(rng, k)
γ = rand(rng)^2 + 1
return MvNormalMeanScalePrecision(m, γ)
end
end
8 changes: 4 additions & 4 deletions test/shifted_negative_numbers_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ end
b in (10.0, 5.0),
c in (1.0, 10.0, -1.0),
eps in (1e-4, 1e-5, 1e-8, 1e-10),
stepsize in (ConstantStepsize(0.1), ConstantStepsize(0.01), ConstantStepsize(0.001))
stepsize in (ConstantLength(0.1), ConstantLength(0.01), ConstantLength(0.001))

expected_q = -b / 2a
expected_minimum = c - b^2 / (4a)
Expand Down Expand Up @@ -232,11 +232,11 @@ end
obj = ManifoldGradientObjective(missing, grad_f!; evaluation=InplaceEvaluation())
dmp = DefaultManoptProblem(M, obj)
s = GradientDescentState(
M,
q;
M;
p = q,
stopping_criterion=StopWhenGradientNormLessNonAllocating(1e-8),
stepsize=ConstantStepsizeNonAllocating(0.1),
direction=IdentityUpdateRule(),
direction=Manopt.IdentityUpdateRule(),
retraction_method=default_retraction_method(M, typeof(q)),
X=zero_vector(M, q),
)
Expand Down
8 changes: 4 additions & 4 deletions test/shifted_positive_numbers_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ end
b in (-10.0, -5.0),
c in (1.0, 10.0, -1.0),
eps in (1e-4, 1e-5, 1e-8, 1e-10),
stepsize in (ConstantStepsize(0.1), ConstantStepsize(0.01), ConstantStepsize(0.001))
stepsize in (ConstantLength(0.1), ConstantLength(0.01), ConstantLength(0.001))

expected_q = -b / 2a
expected_minimum = c - b^2 / (4a)
Expand Down Expand Up @@ -231,11 +231,11 @@ end
obj = ManifoldGradientObjective(missing, grad_f!; evaluation=InplaceEvaluation())
dmp = DefaultManoptProblem(M, obj)
s = GradientDescentState(
M,
q;
M;
p = q,
stopping_criterion=StopWhenGradientNormLessNonAllocating(1e-8),
stepsize=ConstantStepsizeNonAllocating(0.1),
direction=IdentityUpdateRule(),
direction=Manopt.IdentityUpdateRule(),
retraction_method=default_retraction_method(M, typeof(q)),
X=zero_vector(M, q),
)
Expand Down
2 changes: 1 addition & 1 deletion test/single_point_manifold_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ end
b in (10.0, 5.0),
c in (1.0, 10.0, -1.0),
eps in (1e-4, 1e-5, 1e-8, 1e-10),
stepsize in (ConstantStepsize(0.1), ConstantStepsize(0.01), ConstantStepsize(0.001))
stepsize in (ConstantLength(0.1), ConstantLength(0.01), ConstantLength(0.001))

f(M, x) = (a .* x .^ 2 .+ b .* x .+ c)[1]
grad_f(M, x) = 2 .* a .* x .+ b
Expand Down
2 changes: 1 addition & 1 deletion test/symmetric_negative_definite_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ end
f,
g,
p0;
stepsize=ConstantStepsize(stepsize),
stepsize=ConstantLength(stepsize),
stopping_criterion=StopWhenGradientNormLess(eps),
)

Expand Down
Loading