-
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 #439 from ReactiveBayes/generic-transition
Generic implementaton of Transition node
- Loading branch information
Showing
19 changed files
with
861 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import SpecialFunctions: loggamma | ||
import Base.Broadcast: BroadcastFunction | ||
|
||
@node TensorDirichlet Stochastic [out, a] | ||
|
||
@average_energy TensorDirichlet (q_out::TensorDirichlet, q_a::PointMass) = begin | ||
m_a = mean(q_a) | ||
logmean = mean(BroadcastFunction(log), q_out) | ||
return sum(-loggamma.(sum(m_a, dims = 1)) .+ sum(loggamma.(m_a), dims = 1) .- sum((m_a .- 1.0) .* logmean, dims = 1)) | ||
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
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,4 @@ | ||
|
||
@marginalrule TensorDirichlet(:out_a) (m_out::TensorDirichlet, m_a::PointMass) = begin | ||
return convert_paramfloattype((out = prod(ClosedProd(), TensorDirichlet(mean(m_a)), m_out), a = m_a)) | ||
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,4 @@ | ||
|
||
@rule TensorDirichlet(:out, Marginalisation) (m_a::PointMass,) = TensorDirichlet(mean(m_a)) | ||
|
||
@rule TensorDirichlet(:out, Marginalisation) (q_a::PointMass,) = TensorDirichlet(mean(q_a)) |
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
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,17 @@ | ||
import Base.Broadcast: BroadcastFunction | ||
|
||
function ReactiveMP.rule( | ||
fform::Type{<:Transition}, | ||
on::Val{S}, | ||
vconstraint::Marginalisation, | ||
messages_names::Val{m_names}, | ||
messages::Tuple, | ||
marginals_names::Val{(:a,)}, | ||
marginals::Tuple, | ||
meta::Any, | ||
addons::Any, | ||
::Any | ||
) where {S, m_names} | ||
interface_index = parse(Int, String(S)[2:end]) + 2 | ||
return __reduce_td_from_messages(messages, first(marginals), interface_index), addons | ||
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,56 @@ | ||
|
||
@testitem "TensorDirichletNode" begin | ||
using ReactiveMP, Random, BayesBase, ExponentialFamily, Distributions, StableRNGs | ||
|
||
@testset "AverageEnergy" begin | ||
begin | ||
rng = StableRNG(123456) | ||
for i in 1:100 | ||
α = rand(rng, 2, 2) | ||
a = rand(rng, 2, 2) | ||
q_out = TensorDirichlet(α) | ||
q_a = PointMass(a) | ||
|
||
marginals = (Marginal(q_out, false, false, nothing), Marginal(q_a, false, false, nothing)) | ||
avg_energy = score(AverageEnergy(), TensorDirichlet, Val{(:out, :a)}(), marginals, nothing) | ||
|
||
q_out = MatrixDirichlet(α) | ||
q_a = PointMass(a) | ||
|
||
marginals = (Marginal(q_out, false, false, nothing), Marginal(q_a, false, false, nothing)) | ||
avg_energy_matrix = score(AverageEnergy(), MatrixDirichlet, Val{(:out, :a)}(), marginals, nothing) | ||
|
||
@test avg_energy ≈ avg_energy_matrix | ||
end | ||
end | ||
|
||
begin | ||
for rank in 3:5 | ||
for dim in 2:5 | ||
for i in 1:100 | ||
dims = ntuple(d -> dim, rank) | ||
α = rand(rng, dims...) | ||
a = rand(rng, dims...) | ||
|
||
q_out = TensorDirichlet(α) | ||
q_a = PointMass(a) | ||
|
||
marginals = (Marginal(q_out, false, false, nothing), Marginal(q_a, false, false, nothing)) | ||
avg_energy = score(AverageEnergy(), TensorDirichlet, Val{(:out, :a)}(), marginals, nothing) | ||
|
||
q_out = Dirichlet.(eachslice(α, dims = ntuple(d -> d + 1, rank - 1))) | ||
q_a = PointMass.(eachslice(a, dims = ntuple(d -> d + 1, rank - 1))) | ||
|
||
avg_energy_matrix = 0.0 | ||
for (dir, a) in zip(q_out, q_a) | ||
marginals = (Marginal(dir, false, false, nothing), Marginal(a, false, false, nothing)) | ||
avg_energy_matrix += score(AverageEnergy(), Dirichlet, Val{(:out, :a)}(), marginals, nothing) | ||
end | ||
|
||
@test avg_energy ≈ avg_energy_matrix | ||
end | ||
end | ||
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,86 @@ | ||
@testitem "TransitionNode" begin | ||
using Test, ReactiveMP, Random, Distributions, BayesBase, ExponentialFamily | ||
|
||
@testset "Transition node properties" begin | ||
@test ReactiveMP.sdtype(Transition) == Stochastic() | ||
@test ReactiveMP.alias_interface(Transition, 1, :out) == :out | ||
@test ReactiveMP.alias_interface(Transition, 2, :in) == :in | ||
@test ReactiveMP.alias_interface(Transition, 3, :in) == :a | ||
@test ReactiveMP.alias_interface(Transition, 4, :in) == :T1 | ||
|
||
@test ReactiveMP.collect_factorisation(Transition, ()) == () | ||
end | ||
@testset "AverageEnergy(q_out_in::Contingency, q_a::MatrixDirichlet)" begin end | ||
|
||
@testset "AverageEnergy(q_out_in::Contingency, q_a::PointMass)" begin | ||
contingency_matrix = [0.2 0.3; 0.4 0.1] | ||
a_matrix = [0.7 0.3; 0.2 0.8] | ||
|
||
q_out_in = Contingency(contingency_matrix) | ||
q_a = PointMass(a_matrix) | ||
|
||
marginals = (Marginal(q_out_in, false, false, nothing), Marginal(q_a, false, false, nothing)) | ||
|
||
# Expected value calculated by hand | ||
expected = -sum(contingency_matrix .* log.(clamp.(a_matrix, tiny, Inf))) | ||
|
||
@test score(AverageEnergy(), Transition, Val{(:out_in, :a)}(), marginals, nothing) ≈ expected | ||
|
||
contingency_matrix = [0.2 0.3; 0.4 0.1] | ||
a_matrix = [1.0 0.0; 0.0 1.0] | ||
|
||
q_out_in = Contingency(contingency_matrix) | ||
q_a = PointMass(a_matrix) | ||
|
||
marginals = (Marginal(q_out_in, false, false, nothing), Marginal(q_a, false, false, nothing)) | ||
|
||
expected = -sum(contingency_matrix .* log.(clamp.(a_matrix, tiny, Inf))) | ||
|
||
@test score(AverageEnergy(), Transition, Val{(:out_in, :a)}(), marginals, nothing) ≈ expected | ||
|
||
contingency_matrix = prod.(Iterators.product([0, 1, 0], [0.1, 0.4, 0.5])) | ||
a_matrix = diageye(3) | ||
|
||
q_out_in = Contingency(contingency_matrix) | ||
q_a = PointMass(a_matrix) | ||
|
||
marginals = (Marginal(q_out_in, false, false, nothing), Marginal(q_a, false, false, nothing)) | ||
|
||
expected = -sum(contingency_matrix .* log.(clamp.(a_matrix, tiny, Inf))) | ||
|
||
@test score(AverageEnergy(), Transition, Val{(:out_in, :a)}(), marginals, nothing) ≈ expected | ||
|
||
contingency_matrix = [0.0 0.0 0.0; 0.0 1.0 0.0; 0.0 0.0 0.0] | ||
q_out_in = Contingency(contingency_matrix) | ||
q_a = PointMass(diageye(3)) | ||
|
||
marginals = (Marginal(q_out_in, false, false, nothing), Marginal(q_a, false, false, nothing)) | ||
|
||
expected = -sum(contingency_matrix .* log.(clamp.(a_matrix, tiny, Inf))) | ||
@test score(AverageEnergy(), Transition, Val{(:out_in, :a)}(), marginals, nothing) ≈ expected | ||
end | ||
|
||
@testset "AverageEnergy(q_out::Any, q_in::Any, q_a::PointMass)" begin | ||
q_out = Categorical([0.3, 0.7]) | ||
q_in = Categorical([0.8, 0.2]) | ||
q_a = PointMass([0.7 0.3; 0.2 0.8]) | ||
|
||
marginals = (Marginal(q_out, false, false, nothing), Marginal(q_in, false, false, nothing), Marginal(q_a, false, false, nothing)) | ||
|
||
contingency = probvec(q_out) * probvec(q_in)' | ||
expected = -sum(contingency .* log.(clamp.(mean(q_a), tiny, Inf))) | ||
|
||
@test score(AverageEnergy(), Transition, Val{(:out, :in, :a)}(), marginals, nothing) ≈ expected | ||
|
||
q_out = Categorical([0.0, 1.0]) | ||
q_in = Categorical([0.0, 1.0]) | ||
q_a = PointMass([1.0 0.0; 1.0 0.0]) | ||
|
||
marginals = (Marginal(q_out, false, false, nothing), Marginal(q_in, false, false, nothing), Marginal(q_a, false, false, nothing)) | ||
|
||
contingency = probvec(q_out) * probvec(q_in)' | ||
|
||
expected = -sum(contingency .* log.(clamp.(mean(q_a), tiny, Inf))) | ||
@test score(AverageEnergy(), Transition, Val{(:out, :in, :a)}(), marginals, nothing) ≈ expected | ||
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
Oops, something went wrong.