Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gamma mixture #222

Closed
wants to merge 9 commits into from
Closed

Fix gamma mixture #222

wants to merge 9 commits into from

Conversation

albertpod
Copy link
Member

Initially, this PR aimed to solve #84.

It enables importance sampling for the shape parameters of the Gamma Mixture node (see supporting notebook on Gamma Mixture).

We have filled in the missing functions for ImportanceSamplingApproximation.

We have added the missing rand function for the GammaShapeRate type.

Average energy functions for GammaShapeRate were fused into a single function.

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@@ -13,7 +13,7 @@ convenient functions to generate samples and weights to approximate expectations
- `rng`: random number generator objects
- `nsamples`: number of samples generated by default
"""
struct ImportanceSamplingApproximation{T, R}
struct ImportanceSamplingApproximation{T, R} <: AbstractFormConstraint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ImportanceSamplingApproximation should not be declared as an AbstractFormConstraint. This is wrong from the semantic point of view. There should be an extra MomentsApproximationFormConstraint (or something similar).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


a = m^2 / v
b = m / v
return GammaShapeRate(a, b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be changed somehow


constrain_form(::ImportanceSamplingApproximation, distribution::Any) = distribution

make_form_constraint(::Type{ImportanceSamplingApproximation}, args...; kwargs...) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed in favor of separate MomentsApproximationFormConstraint

@@ -65,3 +65,7 @@ end

Distributions.pdf(dist::GammaShapeRate, x::Real) = (rate(dist)^shape(dist)) / gamma(shape(dist)) * x^(shape(dist) - 1) * exp(-rate(dist) * x)
Distributions.logpdf(dist::GammaShapeRate, x::Real) = shape(dist) * log(rate(dist)) - loggamma(shape(dist)) + (shape(dist) - 1) * log(x) - rate(dist) * x

function Random.rand(rng::AbstractRNG, dist::GammaShapeRate)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets also add more rand methods (which accepts size and inplace versions).

@average_energy GammaShapeRate (q_out::Any, q_α::GammaDistributionsFamily, q_β::Any) = begin
mean(loggamma, q_α) - mean(q_α) * mean(log, q_β) - (mean(q_α) - 1.0) * mean(log, q_out) + mean(q_β) * mean(q_out)
end
@average_energy GammaShapeRate (q_out::Any, q_α::Union{PointMass, SampleList, GammaDistributionsFamily}, q_β::Any) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if can simply put Any there

@albertpod albertpod closed this Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants