Linear model with log-link on sigma (exp(sigma)) errors: are there plans to support it in the future? #358
Replies: 4 comments 4 replies
-
Hi @DominiqueMakowski. There's inherently no issue with RxInfer here, so I will transfer this issue into a discussion. using RxInfer
using ExponentialFamilyProjection
using BayesBase
using Distributions
# Define the custom distribution first
struct TransformedNormalDistribution{H,T} <: ContinuousUnivariateDistribution
h::H
t::T
end
BayesBase.logpdf(dist::TransformedNormalDistribution, x) = logpdf(Normal(dist.h, exp(dist.t)), x)
BayesBase.insupport(dist::TransformedNormalDistribution, x) = true
@node TransformedNormalDistribution Stochastic [out, h, t]
# Define the model
@model function model_Gaussian(y)
μ ~ Normal(mean=0.3, variance=0.5)
σ ~ Normal(mean=log(0.2), variance=1.0)
y .~ TransformedNormalDistribution(μ, σ)
end
# Define constraints
@constraints function non_conjugate_model_constraints()
q(μ) :: ProjectedTo(NormalMeanVariance)
q(σ) :: ProjectedTo(NormalMeanVariance)
q(σ, μ) = q(σ)q(μ)
end
# Define initialization
@initialization function model_initialization()
q(σ) = NormalMeanVariance(0.0, 1.0)
# q(σ) = GammaShapeRate(1.0, 1.0) # also works
end
# Generate sample data
y = randn(100)
# Perform inference
result = infer(
model = model_Gaussian(),
data = (y = y,),
constraints = non_conjugate_model_constraints(),
initialization = model_initialization(),
options = (rulefallback = NodeFunctionRuleFallback(),),
showprogress = true,
iterations = 10
)
println(result.posteriors[:μ][end])
println(result.posteriors[:σ][end]) I haven't checked the inference result, but this should give you a good starting point. There are other ways to run inference on your model, but I opted for the fastest one now. As for support for Distributions.jl, I acknowledge it can sometimes be inconvenient. It's important to note that "primitive" nodes in GraphPPL are not the same as distributions from If you need any further clarification or have additional questions, feel free to ask. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the thorough response. I feel like it's still a bit too complicated for me for now (this was mostly a simple toy example, and the solution contains quite a lot of extra steps that I would have no idea how to adapt to a more complex real-life scenario) - I need to learn more first before being able to fully utilize RxInfer Maybe someday there will be a way to handle these steps somewhat automatically
That's a fair point, though I think many newcomer users might be confused seeing "Distributions.jl-like" syntax (as they might overlook the non-default named arguments) and expect things to just work, but I guess they'll have to read the docs :p |
Beta Was this translation helpful? Give feedback.
-
@ismailsenoz seems like this is a simpler version of GCV node from here |
Beta Was this translation helpful? Give feedback.
-
The original issue in your example arises because we use the
I believe this is mentioned in the official GraphPPL documentation, but it is not covered in the RxInfer documentation. I’ll open a separate issue for that.
No, not with a plain However, as @albertpod pointed out, you’ll still need to provide additional context for the inference engine. Even though the example might seem simple, the underlying inference is quite complex due to its non-conjugate structure. To handle this, you’ll need to write additional code. The best approach to solve the inference for this model is probably this example, which uses the |
Beta Was this translation helpful? Give feedback.
-
I am trying to fit a simple linear model with some parameters expressed using a link-function (e.g.,
exp()
) which is common to express priors in an unconstrained space.Unfortunately, the above fails with:
In general, my question is about whether arbitrary link functions (such as that of StatsFuns) will be supported in the future?
Also, are there plans to make RxInfer work with a "standard"
Distributions.Normal()
rather than the bespokeNormalMeanVariance()
?Thanks for the clarifications!
Beta Was this translation helpful? Give feedback.
All reactions