-
Notifications
You must be signed in to change notification settings - Fork 19
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
Fixed posterior for NormalWishart. #28
base: master
Are you sure you want to change the base?
Changes from all commits
13ba68b
a323067
c14f301
57f52b1
098841d
5e5e4d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ struct NormalWishart{T<:Real} <: ContinuousMultivariateDistribution | |
zeromean::Bool | ||
mu::Vector{T} | ||
kappa::T | ||
Tchol::Cholesky{T} # Precision matrix (well, sqrt of one) | ||
Tchol::Cholesky{T} #Prior inverse scale matrix | ||
nu::T | ||
|
||
function NormalWishart{T}(mu::Vector{T}, kappa::T, | ||
|
@@ -40,7 +40,7 @@ end | |
function insupport(::Type{NormalWishart}, x::Vector{T}, Lam::Matrix{T}) where T<:Real | ||
return (all(isfinite(x)) && | ||
size(Lam, 1) == size(Lam, 2) && | ||
isApproxSymmmetric(Lam) && | ||
#isApproxSymmmetric(Lam) && | ||
size(Lam, 1) == length(x) && | ||
hasCholesky(Lam)) | ||
end | ||
|
@@ -52,7 +52,7 @@ function logpdf(nw::NormalWishart, x::Vector{T}, Lam::Matrix{T}) where T<:Real | |
if !insupport(NormalWishart, x, Lam) | ||
return -Inf | ||
else | ||
p = length(x) | ||
p=length(x) | ||
|
||
nu = nw.nu | ||
kappa = nw.kappa | ||
|
@@ -62,10 +62,10 @@ function logpdf(nw::NormalWishart, x::Vector{T}, Lam::Matrix{T}) where T<:Real | |
hp = 0.5 * p | ||
|
||
# Normalization | ||
logp = hp*(log(kappa) - Float64(log2π)) | ||
logp = hp*(log(kappa) - Float64(log(2*π))) | ||
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. why get rid of 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. My bad, I think it was giving me errors when I tried to run it. Maybe I just wasn't importing the required package or something. 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. Yeah I think it's necessary to add StatsFuns (which also provides 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. logmvgamma is from Distributions. maybe the old lpgamma is a version from StatsFuns? 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 think Distributions gets it from StatsFuns: https://github.com/JuliaStats/StatsFuns.jl/blob/e66dd973650c375bc1739c820e5b96bb5bd000a8/src/misc.jl#L3-L15 |
||
logp -= hnu * logdet(Tchol) | ||
logp -= hnu * p * log(2.) | ||
logp -= lpgamma(p, hnu) | ||
logp -= logmvgamma(p, hnu) | ||
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'm a little disturbed that this hadn't been fixed until now...... 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. There were other areas too where the current logpdf function crashes. I think somewhere in insupport there is a call to a function that doesn't exist. I didn't fix that in this pull request because it was a separate issue, and I forgot to make a new issue for it. |
||
|
||
# Wishart (MvNormal contributes 0.5 as well) | ||
logp += (hnu - hp) * logdet(Lam) | ||
|
@@ -76,13 +76,15 @@ function logpdf(nw::NormalWishart, x::Vector{T}, Lam::Matrix{T}) where T<:Real | |
logp -= 0.5 * kappa * dot(z, Lam * z) | ||
|
||
return logp | ||
|
||
end | ||
end | ||
|
||
function rand(nw::NormalWishart) | ||
Lam = rand(Wishart(nw.nu, nw.Tchol)) | ||
Lsym = PDMat(Symmetric(inv(Lam) ./ nw.kappa)) | ||
mu = rand(MvNormal(nw.mu, Lsym)) | ||
# forcibly symmetrize to pass checks in Distributions.Wishart | ||
V = PDMat(Symmetric(inv(nw.Tchol))) | ||
Lam = rand(Wishart(nw.nu, V)) | ||
# forcibly symmetrize to pass checks in Distributions.MvNormal | ||
Σsym = PDMat(Symmetric(inv(Lam) ./ nw.kappa)) | ||
mu = rand(MvNormal(nw.mu, Σsym)) | ||
return (mu, Lam) | ||
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.
did you mean to remove this? seems like it's needed below...
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.
alternatively, you could use
nw.dim
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.
I think this was an accident