Enforcing mean-field constraints in simple Gaussian linear model? #500
Replies: 6 comments 1 reply
-
Hey! Thanks for the interest in RxInfer! Thus being said, you can double-check that as in your original code the specification inside the |
Beta Was this translation helpful? Give feedback.
-
Thanks for your quick reply @bvdmitri . I have unsuccesfuly tried different versions of the model to enforce the mean field approximation. Below is a version following your suggestions. using RxInfer
# Define the model generator
@model function lg(x, a)
z ~ MvNormal(mean = [0.0, 0.0], cov = Matrix{Float64}(I, 2, 2))
x ~ MvNormal(mean = a * z, cov = Matrix{Float64}(I, 1, 1))
end
a = [2.0 3.0]
x = [4.0]
# Perform inference
bayesian_result = infer(
model = lg(a=a),
data = (x = x,),
)
@constraints function mf_constraints()
q(z) = q(z[1])q(z[2])
end
mf_result = infer(
model = lg(a=a),
data = (x = x,),
constraints = mf_constraints(),
)
# Print results
println("*** Bayesian ***")
mu = mean(bayesian_result.posteriors[:z])
covM = cov(bayesian_result.posteriors[:z])
println("mu_1=$mu[1]")
println("mu_2=$mu[2]")
println("var_1=$(covM[1,1])")
println("var_2=$(covM[2,2])")
println("cov_12=$(covM[1,2])")
println("*** Variational ***")
mu = mean(mf_result.posteriors[:z])
covM = cov(mf_result.posteriors[:z])
println("mu_1=$mu[1]")
println("mu_2=$mu[2]")
println("var_1=$(covM[1,1])")
println("var_2=$(covM[2,2])")
println("cov_12=$(covM[1,2])") Output
The fact that we observe a non-zero covariance between z_1 and z_2 suggests that RxInfer is not applying the constraint. What can I do to enforce it? |
Beta Was this translation helpful? Give feedback.
-
Well, no, its not what I suggested. I suggested to either use
for your original model or simply |
Beta Was this translation helpful? Give feedback.
-
The second example of yours cannot apply a mean-field constraint within a single variable Thus being said, I will still reemphasize that in my opinion, in this model @model function lg(x, a)
z1 ~ Normal(mean=0.0, var=1.0)
z2 ~ Normal(mean=0.0, var=1.0)
x ~ Normal(mean=a[1]*z1 + a[2]*z2, var=1.0)
end result should not change with or without What are you trying to solve in particular? |
Beta Was this translation helpful? Give feedback.
-
In the simple linear Guassian model above, I am trying to get the variational mean-field approximation of p(z|x). With all the model specifications and inference methods that I have tried, I can only get the uncontrained posterior p(z|x). Can I get the variational mean-field approximation of p(z|x) in RxInfer? Both the unconstrained posterior and the mean-field approximation can be solved analytically for this simple model (details here). With the parameter a and observation x in the above example, for the unconstrained posterior we should get mean(z1) = 0.5714285714285714 and for the mean-field approximation we should get mean(z1) = 0.5714285714285714 Above you said:
In the model @model function lg(x, a)
z1 ~ Normal(mean=0.0, var=1.0)
z2 ~ Normal(mean=0.0, var=1.0)
x ~ Normal(mean=a[1]*z1 + a[2]*z2, var=1.0)
end z1 and z2 are independent a priori, but observing x makes them dependent. Thus, applying the mean-field constraint should change the inferred values, as shown above. How should I specify the model and the inference method in RxInfer to obtain the mean-field approximation? mean(z1) = 0.5714285714285714 |
Beta Was this translation helpful? Give feedback.
-
@joacorapela sorry this conversation stopped, didn't the the last message. Look, if you really want to enforce mean-field, you'd have to change your model a bit: @model function lg(x, a)
z1 ~ Normal(mean=0.0, var=1.0)
z2 ~ Normal(mean=0.0, var=1.0)
tmp1 ~ a[1]*z1
tmp2 ~ a[2]*z2
x ~ SoftAddition(tmp1, tmp2, 1.0)
end You then would have to introduce a node and workout rules for variational message passing and belief propagation if you want to compare these thing, or you introduce only variational updates and compare the variances between this model and the previous model you had. Additionally, I believe that something with projection methods might work here, but not sure yet. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am new to RxInfer. Sorry if the question is naive.
It seems that RxInfer is not applying the mean field constraints, as the variance of the mean-field approximation should be smaler than that of the unconstrained posterior. Can I enforce the mean field constraints?
Output
The mean-field variances should be smaller than the unconstrained ones, which suggests that RxInfer is not applying the mean-field constraints. Can I enforce them?
Beta Was this translation helpful? Give feedback.
All reactions