-
Notifications
You must be signed in to change notification settings - Fork 14
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
Fix gamma mixture #222
Changes from 5 commits
cb9f085
c20a433
86742b0
21ce37c
171cd58
15148ff
fc1f8bb
7485ee8
7e80487
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
rng :: R | ||
nsamples :: Int | ||
bsamples :: Vector{T} | ||
|
@@ -22,6 +22,10 @@ struct ImportanceSamplingApproximation{T, R} | |
rsamples :: Vector{T} | ||
end | ||
|
||
function ImportanceSamplingApproximation(nsamples::Int; resampling::Bool = true) | ||
return ImportanceSamplingApproximation(Float64, Random.GLOBAL_RNG, nsamples; resampling = resampling) | ||
end | ||
|
||
function ImportanceSamplingApproximation(rng::R, nsamples::Int; resampling::Bool = true) where {R} | ||
return ImportanceSamplingApproximation(Float64, rng, nsamples; resampling = resampling) | ||
end | ||
|
@@ -83,3 +87,20 @@ function approximate_meancov(approximation::ImportanceSamplingApproximation, g:: | |
|
||
return m, v | ||
end | ||
|
||
is_point_mass_form_constraint(::ImportanceSamplingApproximation) = false | ||
default_form_check_strategy(::ImportanceSamplingApproximation) = FormConstraintCheckEach() | ||
default_prod_constraint(::ImportanceSamplingApproximation) = ProdGeneric() | ||
|
||
function constrain_form(approximation::ImportanceSamplingApproximation, distribution::DistProduct) | ||
m, v = approximate_meancov(approximation, x -> pdf(getright(distribution), x), getleft(distribution)) | ||
|
||
a = m^2 / v | ||
b = m / v | ||
return GammaShapeRate(a, b) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be changed somehow |
||
end | ||
|
||
constrain_form(::ImportanceSamplingApproximation, distribution::Any) = distribution | ||
|
||
make_form_constraint(::Type{ImportanceSamplingApproximation}, args...; kwargs...) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be removed in favor of separate |
||
ImportanceSamplingApproximation(args..., kwargs...) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets also add more |
||
return rand(rng, convert(GammaShapeScale, dist)) | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,8 @@ import SpecialFunctions: loggamma | |
|
||
@node GammaShapeRate Stochastic [out, (α, aliases = [shape]), (β, aliases = [rate])] | ||
|
||
@average_energy GammaShapeRate (q_out::Any, q_α::PointMass, 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_α::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) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if can simply put |
||
begin | ||
mean(loggamma, q_α) - mean(q_α) * mean(log, q_β) - (mean(q_α) - 1.0) * mean(log, q_out) + | ||
mean(q_β) * mean(q_out) | ||
end |
There was a problem hiding this comment.
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 anAbstractFormConstraint
. This is wrong from the semantic point of view. There should be an extraMomentsApproximationFormConstraint
(or something similar).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ismailsenoz @bartvanerp