Replies: 1 comment 2 replies
-
Hi, thank you for trying out You have two variables Now, if we look at the documentation for defining a custom node, we can what we should do. here we see the update equations for sum-product message passing, where here we see the update rules for variational message passing. There is a subtle difference between these two update equations: The sum-product message passing update rule only depends on Since you don't impose any additional constraints on the variational posterior, initialization = @initialization begin
μ(r) = GammaShapeRate(1.0, 1.0)
μ(p) = Beta(1.0, 1.0)
end Now if you try to run inference with this initialization, you'll run into the next error telling you that there's no rules available for the combination of your node and the incoming messages. This is because you defined your node yourself and using RxInfer, Random, Distributions, ExponentialFamilyProjection
# Register the Negative Binomial node
@node Distributions.NegativeBinomial Stochastic [out, r, p]
# Define the model for a single gene using RxInfer
@model function simple_nb(data, γ_α, γ_β, β_α, β_β)
# Set prior for r
r ~ GammaShapeRate(γ_α, γ_β)
# Set prior for p
p ~ Beta(β_α, β_β)
# Negative Binomial likelihood
data .~ NegativeBinomial(r, p)
end
# Generate synthetic dataset
Random.seed!(42)
n_samples = 100
# Ground truth parameters for p
β_α = 1.0
β_β = 1.0
p_true = rand(Beta(β_α, β_β))
# Ground truth parameters for r
γ_α = 5.0
γ_β = 0.1
r_true = rand(GammaShapeRate(γ_α, γ_β))
# Simulate the data
data = rand(NegativeBinomial(r_true, p_true), n_samples)
initialization = @initialization begin
μ(r) = GammaShapeRate(1.0, 1.0)
μ(p) = Beta(1.0, 1.0)
end
constraints = @constraints begin
q(r) :: ProjectedTo(Gamma)
q(p) :: ProjectedTo(Beta)
μ(r) :: ProjectedTo(Gamma)
μ(p) :: ProjectedTo(Beta)
end
# Run inference
result = infer(
model = simple_nb(γ_α=γ_α, γ_β=γ_β, β_α=β_α, β_β=β_β),
data = (data = data,),
initialization = initialization,
constraints = constraints,
options = (
rulefallback = NodeFunctionRuleFallback(),
),
iterations=10
) This gives you an inference result. You can improve the inference by deriving rules yourself. Hopefully you can get started with EDIT: After messing around a bit more with your model, I have found a way to improve the inference result. For this, I imposed a Mean Field constraint on using RxInfer, Random, Distributions, ExponentialFamilyProjection
# Register the Negative Binomial node
@node Distributions.NegativeBinomial Stochastic [out, r, p]
# Define the model for a single gene using RxInfer
@model function simple_nb(data, γ_α, γ_β, β_α, β_β)
# Set prior for r
r ~ GammaShapeRate(γ_α, γ_β)
# Set prior for p
p ~ Beta(β_α, β_β)
# Negative Binomial likelihood
data .~ NegativeBinomial(r, p)
end
# Generate synthetic dataset
Random.seed!(42)
n_samples = 100
# Ground truth parameters for p
β_α = 1.0
β_β = 1.0
p_true = rand(Beta(β_α, β_β))
# Ground truth parameters for r
γ_α = 5.0
γ_β = 0.1
r_true = rand(GammaShapeRate(γ_α, γ_β))
# Simulate the data
data = rand(NegativeBinomial(r_true, p_true), n_samples)
initialization = @initialization begin
q(r) = GammaShapeRate(1.0, 1.0)
q(p) = Beta(1.0, 1.0)
end
constraints = @constraints begin
q(r) :: ProjectedTo(Gamma)
q(p) :: ProjectedTo(Beta)
q(r, p) = MeanField()
end
# Run inference
result = infer(
model = simple_nb(γ_α=γ_α, γ_β=γ_β, β_α=β_α, β_β=β_β),
data = (data = data,),
initialization = initialization,
constraints = constraints,
options = (
rulefallback = NodeFunctionRuleFallback(),
),
iterations = 10
) The inference result I get with this code is a lot better than when we approximate the message first and the posterior marginal afterwards, so hopefully this method is a bit more useful. |
Beta Was this translation helpful? Give feedback.
-
Dear RxInfer team,
I’m trying to implement a simple Negative Binomial model using RxInfer.jl, but I’m encountering an error when running inference. Despite reviewing the initialization section in the documentation, I’m unsure how to proceed.
Code to reproduce the issue:
Error message:
What I’ve tried:
Question:
Could you please help me understand why this error occurs and how to initialize the variables p and r for this model properly? Any guidance on correctly setting up the inference for the Negative Binomial model would be greatly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions