-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from biaslab/224-gaussian-mixture-relax-const…
…raints-all-around 224 gaussian mixture relax constraints all around
- Loading branch information
Showing
11 changed files
with
280 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
export rule | ||
|
||
@rule NormalMixture((:m, k), Marginalisation) (q_out::Any, q_switch::Any, q_p::GammaDistributionsFamily) = begin | ||
@rule NormalMixture((:m, k), Marginalisation) (q_out::Any, q_switch::Any, q_p::Any) = begin | ||
pv = probvec(q_switch) | ||
T = eltype(pv) | ||
z_bar = clamp.(pv, tiny, one(T) - tiny) | ||
return NormalMeanVariance(mean(q_out), inv(z_bar[k] * mean(q_p))) | ||
end | ||
|
||
@rule NormalMixture((:m, k), Marginalisation) (q_out::Any, q_switch::Any, q_p::Wishart) = begin | ||
pv = probvec(q_switch) | ||
T = eltype(pv) | ||
z_bar = clamp.(pv, tiny, one(T) - tiny) | ||
return MvNormalMeanCovariance(mean(q_out), cholinv(z_bar[k] * mean(q_p))) | ||
F = variate_form(q_out) | ||
|
||
return convert(promote_variate_type(F, NormalMeanPrecision), mean(q_out), z_bar[k] * mean(q_p)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
export rule | ||
|
||
@rule NormalMixture((:p, k), Marginalisation) (q_out::Any, q_switch::Any, q_m::UnivariateNormalDistributionsFamily) = begin | ||
@rule NormalMixture((:p, k), Marginalisation) (q_out::Any, q_switch::Any, q_m::Any) = begin | ||
m_mean_k, v_mean_k = mean_cov(q_m) | ||
m_out, v_out = mean_cov(q_out) | ||
z_bar = probvec(q_switch) | ||
|
||
return rule_nm_p_k(variate_form(q_out), m_mean_k, v_mean_k, m_out, v_out, z_bar, k) | ||
end | ||
|
||
function rule_nm_p_k(::Type{Univariate}, m_mean_k, v_mean_k, m_out, v_out, z_bar, k) | ||
return GammaShapeRate(one(eltype(z_bar)) + z_bar[k] / 2, z_bar[k] * (v_out + v_mean_k + abs2(m_out - m_mean_k)) / 2) | ||
end | ||
|
||
@rule NormalMixture((:p, k), Marginalisation) (q_out::Any, q_switch::Any, q_m::MultivariateNormalDistributionsFamily) = begin | ||
m_mean_k, v_mean_k = mean_cov(q_m) | ||
m_out, v_out = mean_cov(q_out) | ||
z_bar = probvec(q_switch) | ||
d = length(m_mean_k) | ||
return WishartMessage(one(eltype(z_bar)) + z_bar[k] + d, z_bar[k] * (v_out + v_mean_k + (m_out - m_mean_k) * (m_out - m_mean_k)')) | ||
function rule_nm_p_k(::Type{Multivariate}, m_mean_k, v_mean_k, m_out, v_out, z_bar, k) | ||
return WishartMessage(one(eltype(z_bar)) + z_bar[k] + length(m_mean_k), z_bar[k] * (v_out + v_mean_k + (m_out - m_mean_k) * (m_out - m_mean_k)')) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
module NodesNormalMixtureTest | ||
|
||
using Test | ||
using ReactiveMP | ||
using Random | ||
using Distributions | ||
|
||
import ReactiveMP: @test_rules | ||
import ReactiveMP: WishartMessage, ManyOf | ||
|
||
@testset "NormalMixtureNode" begin | ||
@testset "AverageEnergy" begin | ||
begin | ||
q_out = NormalMeanVariance(0.0, 1.0) | ||
q_switch = Bernoulli(0.2) | ||
q_m = (NormalMeanVariance(1.0, 2.0), NormalMeanVariance(3.0, 4.0)) | ||
q_p = (GammaShapeRate(2.0, 3.0), GammaShapeRate(4.0, 5.0)) | ||
|
||
marginals = ( | ||
Marginal(q_out, false, false), | ||
Marginal(q_switch, false, false), | ||
ManyOf(map(q_m_ -> Marginal(q_m_, false, false), q_m)), | ||
ManyOf(map(q_p_ -> Marginal(q_p_, false, false), q_p)) | ||
) | ||
|
||
ref_val = | ||
0.2 * (score(AverageEnergy(), NormalMeanPrecision, Val{(:out, :μ, :τ)}, map((q) -> Marginal(q, false, false), (q_out, q_m[1], q_p[1])), nothing)) + | ||
0.8 * (score(AverageEnergy(), NormalMeanPrecision, Val{(:out, :μ, :τ)}, map((q) -> Marginal(q, false, false), (q_out, q_m[2], q_p[2])), nothing)) | ||
@test score(AverageEnergy(), NormalMixture, Val{(:out, :switch, :m, :p)}, marginals, nothing) ≈ ref_val | ||
end | ||
|
||
begin | ||
q_out = NormalMeanVariance(1.0, 1.0) | ||
q_switch = Bernoulli(0.4) | ||
q_m = (NormalMeanVariance(3.0, 2.0), NormalMeanVariance(3.0, 4.0)) | ||
q_p = (GammaShapeRate(2.0, 3.0), GammaShapeRate(1.0, 5.0)) | ||
|
||
marginals = ( | ||
Marginal(q_out, false, false), | ||
Marginal(q_switch, false, false), | ||
ManyOf(map(q_m_ -> Marginal(q_m_, false, false), q_m)), | ||
ManyOf(map(q_p_ -> Marginal(q_p_, false, false), q_p)) | ||
) | ||
|
||
ref_val = | ||
0.4 * (score(AverageEnergy(), NormalMeanPrecision, Val{(:out, :μ, :τ)}, map((q) -> Marginal(q, false, false), (q_out, q_m[1], q_p[1])), nothing)) + | ||
0.6 * (score(AverageEnergy(), NormalMeanPrecision, Val{(:out, :μ, :τ)}, map((q) -> Marginal(q, false, false), (q_out, q_m[2], q_p[2])), nothing)) | ||
@test score(AverageEnergy(), NormalMixture, Val{(:out, :switch, :m, :p)}, marginals, nothing) ≈ ref_val | ||
end | ||
|
||
begin | ||
q_out = NormalMeanVariance(0.0, 1.0) | ||
q_switch = Categorical([0.5, 0.5]) | ||
q_m = (NormalMeanPrecision(1.0, 2.0), NormalMeanPrecision(3.0, 4.0)) | ||
q_p = (GammaShapeRate(3.0, 3.0), GammaShapeRate(4.0, 5.0)) | ||
|
||
marginals = ( | ||
Marginal(q_out, false, false), | ||
Marginal(q_switch, false, false), | ||
ManyOf(map(q_m_ -> Marginal(q_m_, false, false), q_m)), | ||
ManyOf(map(q_p_ -> Marginal(q_p_, false, false), q_p)) | ||
) | ||
|
||
ref_val = | ||
0.5 * (score(AverageEnergy(), NormalMeanPrecision, Val{(:out, :μ, :τ)}, map((q) -> Marginal(q, false, false), (q_out, q_m[1], q_p[1])), nothing)) + | ||
0.5 * (score(AverageEnergy(), NormalMeanPrecision, Val{(:out, :μ, :τ)}, map((q) -> Marginal(q, false, false), (q_out, q_m[2], q_p[2])), nothing)) | ||
@test score(AverageEnergy(), NormalMixture, Val{(:out, :switch, :m, :p)}, marginals, nothing) ≈ ref_val | ||
end | ||
|
||
begin | ||
q_out = MvNormalMeanCovariance([0.0], [1.0]) | ||
q_switch = Categorical([0.5, 0.5]) | ||
q_m = (MvNormalMeanPrecision([1.0], [2.0]), MvNormalMeanPrecision([3.0], [4.0])) | ||
q_p = (WishartMessage(3.0, fill(3.0, 1, 1)), WishartMessage(4.0, fill(5.0, 1, 1))) | ||
|
||
marginals = ( | ||
Marginal(q_out, false, false), | ||
Marginal(q_switch, false, false), | ||
ManyOf(map(q_m_ -> Marginal(q_m_, false, false), q_m)), | ||
ManyOf(map(q_p_ -> Marginal(q_p_, false, false), q_p)) | ||
) | ||
|
||
ref_val = | ||
0.5 * (score(AverageEnergy(), MvNormalMeanPrecision, Val{(:out, :μ, :Λ)}, map((q) -> Marginal(q, false, false), (q_out, q_m[1], q_p[1])), nothing)) + | ||
0.5 * (score(AverageEnergy(), MvNormalMeanPrecision, Val{(:out, :μ, :Λ)}, map((q) -> Marginal(q, false, false), (q_out, q_m[2], q_p[2])), nothing)) | ||
@test score(AverageEnergy(), NormalMixture, Val{(:out, :switch, :m, :p)}, marginals, nothing) ≈ ref_val | ||
end | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module RulesNormalMixtureMTest | ||
|
||
using Test | ||
using ReactiveMP | ||
using Random | ||
using Distributions | ||
|
||
import ReactiveMP: @test_rules | ||
|
||
@testset "rules:NormalMixture:m" begin | ||
@testset "Variational : (m_out::UnivariateNormalDistributionsFamily..., m_p::GammaDistributionsFamily...) k=1" begin | ||
@test_rules [with_float_conversions = true] NormalMixture{2}((:m, k = 1), Marginalisation) [ | ||
(input = (q_out = NormalMeanVariance(8.5, 0.5), q_switch = Bernoulli(0.2), q_p = GammaShapeRate(1.0, 2.0)), output = NormalMeanPrecision(8.5, 0.1)), | ||
( | ||
input = (q_out = NormalWeightedMeanPrecision(3 / 10, 6 / 10), q_switch = Categorical([0.5, 0.5]), q_p = GammaShapeRate(1.0, 1.0)), | ||
output = NormalMeanPrecision(0.5, 0.5) | ||
), | ||
( | ||
input = (q_out = NormalWeightedMeanPrecision(5.0, 1 / 4), q_switch = Categorical([0.75, 0.25]), q_p = GammaShapeScale(1.0, 1.0)), | ||
output = NormalMeanPrecision(20.0, 0.75) | ||
), | ||
(input = (q_out = NormalWeightedMeanPrecision(1, 1), q_switch = Categorical([1.0, 0.0]), q_p = GammaShapeRate(1.0, 2.0)), output = NormalMeanPrecision(1.0, 0.5)) | ||
] | ||
end | ||
|
||
@testset "Variational : (m_out::UnivariateNormalDistributionsFamily..., m_p::GammaDistributionsFamily...) k=2" begin | ||
@test_rules [with_float_conversions = true] NormalMixture{2}((:m, k = 2), Marginalisation) [ | ||
(input = (q_out = NormalMeanVariance(8.5, 0.5), q_switch = Bernoulli(0.2), q_p = GammaShapeRate(1.0, 2.0)), output = NormalMeanPrecision(8.5, 0.4)), | ||
( | ||
input = (q_out = NormalWeightedMeanPrecision(3 / 10, 6 / 10), q_switch = Categorical([0.5, 0.5]), q_p = GammaShapeRate(1.0, 1.0)), | ||
output = NormalMeanPrecision(0.5, 0.5) | ||
), | ||
( | ||
input = (q_out = NormalWeightedMeanPrecision(5.0, 1 / 4), q_switch = Categorical([0.75, 0.25]), q_p = GammaShapeScale(1.0, 1.0)), | ||
output = NormalMeanPrecision(20.0, 0.25) | ||
) | ||
] | ||
end | ||
|
||
@testset "Variational : (m_out::MultivariateNormalDistributionsFamily..., m_p::Wishart...) k=1" begin | ||
@test_rules [with_float_conversions = true, atol = 1e-4] NormalMixture{2}((:m, k = 1), Marginalisation) [ | ||
( | ||
input = ( | ||
q_out = MvNormalWeightedMeanPrecision([6.75, 12.0], [4.5 -0.75; -0.75 4.5]), q_switch = Categorical([0.5, 0.5]), q_p = Wishart(3.0, [2.0 -0.25; -0.25 1.0]) | ||
), | ||
output = MvNormalMeanPrecision([2.0, 3.0], [3.0 -0.375; -0.375 1.5]) | ||
), | ||
( | ||
input = ( | ||
q_out = MvNormalMeanPrecision([3.75, 10.3125], [5.25 -0.75; -0.75 3.75]), q_switch = Categorical([0.75, 0.25]), q_p = Wishart(3.0, [2.0 -0.25; -0.25 1.0]) | ||
), | ||
output = MvNormalMeanPrecision([3.75, 10.3125], [4.5 -0.5625; -0.5625 2.25]) | ||
), | ||
( | ||
input = (q_out = MvNormalMeanPrecision([0.75, 17.25], [3.0 -0.75; -0.75 6.0]), q_switch = Categorical([1.0, 0.0]), q_p = Wishart(3.0, [2.0 -0.25; -0.25 1.0])), | ||
output = MvNormalMeanPrecision([0.75, 17.25], [6.0 -0.75; -0.75 3.0]) | ||
) | ||
] | ||
end | ||
|
||
@testset "Variational : (m_out::UnivariateNormalDistributionsFamily..., m_p::GammaDistributionsFamily...) k=1" begin | ||
@test_rules [with_float_conversions = true] NormalMixture{2}((:m, k = 1), Marginalisation) [ | ||
(input = (q_out = PointMass(8.5), q_switch = Bernoulli(0.2), q_p = GammaShapeRate(1.0, 2.0)), output = NormalMeanPrecision(8.5, 0.1)), | ||
(input = (q_out = NormalWeightedMeanPrecision(3 / 10, 6 / 10), q_switch = Categorical([0.5, 0.5]), q_p = PointMass(1.0)), output = NormalMeanPrecision(0.5, 0.5)) | ||
] | ||
end | ||
end | ||
|
||
end |
Oops, something went wrong.
866780d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
866780d
There was a problem hiding this comment.
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/71698
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: