Contigency factor node #454
-
Hi all, Is there a Contingency factor node in RxInfer equivalent to the one in ForneyLab? Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hi Mateus, there is not exactly the Contingency factor node. However, there is the general |
Beta Was this translation helpful? Give feedback.
-
@mateusjoffily I've ported the ForneyLab implementation of the The node definition and rules are given by, using RxInfer
using Base: BroadcastFunction
using ReactiveMP: normalize!
using LinearAlgebra: Diagonal, tr
@node Contingency Stochastic [ out1, out2, p ]
@average_energy Contingency (q_out1_out2::Contingency, q_p::Any) = begin
return -tr(components(q_out1_out2)' * mean(BroadcastFunction(log), q_p))
end
#------------
# Sum-Product
#------------
@rule Contingency(:out1, Marginalisation) (m_out2::Categorical, m_p::PointMass) = begin
p = mean(m_p) * probvec(m_out2)
normalize!(p, 1)
return Categorical(p)
end
@rule Contingency(:out2, Marginalisation) (m_out1::Categorical, m_p::PointMass) = begin
p = mean(m_p)' * probvec(m_out1)
normalize!(p, 1)
return Categorical(p)
end
#------------
# Variational
#------------
@rule Contingency(:out1, Marginalisation) (m_out2::Categorical, q_p::Any) = begin
p = clamp.(exp.(mean(BroadcastFunction(log), q_p) * probvec(m_out2)), tiny, Inf)
normalize!(p, 1)
return Categorical(p)
end
@rule Contingency(:out2, Marginalisation) (m_out1::Categorical, q_p::Any) = begin
p = clamp.(exp.(mean(BroadcastFunction(log), q_p)' * probvec(m_out1)), tiny, Inf)
normalize!(p, 1)
return Categorical(p)
end
#----------
# Marginals
#----------
@marginalrule Contingency(:out1_out2) (m_out1::Categorical, m_out2::Categorical, q_p::Any) = begin
D = map(e -> clamp(exp(e), tiny, huge), mean(BroadcastFunction(log), q_p))
B = Diagonal(probvec(m_out1)) * D * Diagonal(probvec(m_out2))
P = map!(Base.Fix2(/, sum(B)), B, B) # In-place normalization
return Contingency(P, Val(false))
end And a simple example with sumproduct message passing, # An example for the contingency node with sum-product message passing.
# In this example, the contingency node models a joint distribution p(y_1, y_2 | B),
# while a discrete transition node would model the conditional p(y_1 | y_2, B).
@model function contingency_sp(B_hat)
y[1] ~ Categorical([0.5, 0.5])
y[2] ~ Categorical([0.5, 0.5])
y[1] ~ Contingency(y[2], B_hat)
end
result = infer(model = contingency_sp(),
data = (B_hat=[0.4 0.1; 0.1 0.4],), # All entries in the matrix normalize
free_energy = true) |
Beta Was this translation helpful? Give feedback.
I kind of fail to see where the actual data you have about either
x
ory
enters the model, but the following code puts a prior ony
that models a joint dependency between the two (they are always equal):Adding an additional prior on
y[2]
allows you to fully mod…