Skip to content

Commit

Permalink
Merge pull request #21 from JuliaStats/dfk/norminvgamma-mode
Browse files Browse the repository at this point in the history
Use correct formula for normal-inverse chi2 mode and normal-inverse gamma mode
  • Loading branch information
mschauer authored Feb 13, 2018
2 parents 36fe174 + 0b1ec09 commit 8e3a6a7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/normalinversechisq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ end

function mode(d::NormalInverseChisq)
μ = d.μ
σ2 = d.ν*d.σ2/(d.ν - 1)
σ2 = d.ν*d.σ2/(d.ν + 3)
return μ, σ2
end

Expand Down
2 changes: 1 addition & 1 deletion src/normalinversegamma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ end

function mode(d::NormalInverseGamma)
mu = d.mu
sig2 = d.scale / (d.shape + 1.0)
sig2 = d.scale / (d.shape + 1.5)
return mu, sig2
end

Expand Down
16 changes: 15 additions & 1 deletion test/conjugates_normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ w = rand(100)
@test isa(ps, Normal)
@test insupport(ps,ps.μ) && ps.σ > zero(ps.σ)

for shape in [0.1, 1., 10.]
d = NormalInverseGamma(mu0, v0, scale0, shape)
μ, σ2 = mode(d)
@test pdf(d, μ, σ2) > pdf(d, μ, σ2 + 0.001)
@test pdf(d, μ, σ2) > pdf(d, μ, σ2 - 0.001)
end

end

@testset "NormalInverseChisq - Normal" begin
Expand All @@ -151,7 +158,7 @@ w = rand(100)

@test NormalInverseChisq(pri2) == pri

@test_broken mode(pri2) == mode(pri)
@test mode(pri2) == mode(pri)
@test mean(pri2) == mean(pri)
@test pdf(pri, mu_true, sig2_true) == pdf(pri2, mu_true, sig2_true)

Expand All @@ -169,6 +176,13 @@ w = rand(100)
@test logpdf(post, x...) logpdf(post2, x...)
end

for ν in [0.1, 1., 10.]
nix = NormalInverseChisq(0., 2., 3., ν)
μ, σ2 = mode(nix)
@test pdf(nix, μ, σ2) > pdf(nix, μ, σ2 + 0.001)
@test pdf(nix, μ, σ2) > pdf(nix, μ, σ2 - 0.001)
end

end

@testset "NormalGamma - Normal" begin
Expand Down

0 comments on commit 8e3a6a7

Please sign in to comment.