Clarification needed on error handling when model is misspecified
albertpod opened this issue · 2 comments
In the context of the discussion, I encountered an unclear error message while working with an SSM. Below is a simplified version of the code that produces the error:
@model function example_error(y)
x0 ~ Normal(μ=0, γ=1)
for i in eachindex(y)
x[i] ~ Normal(μ=x0, γ=1)
y[i] ~ Normal(μ=x, γ=1)
x0 = x[i]
end
end
infer(
model = example_error(),
data = (y = randn(100), ),
showprogress = true
)
This code results in the following error message:
ERROR: Don't know how to alias interface μ in 3 for NormalMeanPrecision
Thanks to @bvdmitri, I realized that the model was incorrectly specified. In the problematic line:
y[i] ~ Normal(μ=x, γ=1)
I mistakenly used x
instead of x[i]
. The correct model, which addresses this error, is as follows:
@model function example_error(y)
x0 ~ Normal(μ=0, γ=1)
for i in eachindex(y)
x[i] ~ Normal(μ=x0, γ=1)
y[i] ~ Normal(μ=x[i], γ=1)
x0 = x[i]
end
end
It would be beneficial if the error message could be more descriptive to pinpoint mistakes such as referencing a whole array (x
) instead of an array element (x[i]
). A suggested error message might be:
ERROR: Incorrect variable reference. Expected an array element (e.g., 'x[i]'), but found an array ('x'). Please check your variable indices.
Thanks for posting @albertpod !
Using x
as an entire array is technically okay, which is what we do in mixture nodes. I think a more practical error message for this scenario could be something like:
Error: `NormalMeanPrecision` expects 2 input arguments, but 3 have been provided `(μ = x[1], μ = x[2], γ=1)`.
or something similar. What do you think?
Yes, that's a better error message indeed.