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

MALA implementation results in the wrong covariance #95

Open
Red-Portal opened this issue Feb 13, 2024 · 0 comments
Open

MALA implementation results in the wrong covariance #95

Red-Portal opened this issue Feb 13, 2024 · 0 comments

Comments

@Red-Portal
Copy link

Red-Portal commented Feb 13, 2024

import AdvancedMH, LogDensityProblems, Distributions

struct TheNormalLogDensity{M}
    A::M
end

# can do gradient
LogDensityProblems.capabilities(::Type{<:TheNormalLogDensity}) = LogDensityProblems.LogDensityOrder{1}()

LogDensityProblems.dimension(d::TheNormalLogDensity) = size(d.A, 1)
LogDensityProblems.logdensity(d::TheNormalLogDensity, x) = -x' * d.A * x / 2

function LogDensityProblems.logdensity_and_gradient(d::TheNormalLogDensity, x)
    return -x' * d.A * x / 2, -d.A * x
end

Σ = [1.5 0.35; 0.35 1.0]
σ² = 0.5
spl = AdvancedMH.MALA(g -> Distributions.MvNormal((σ² / 2) .* g, σ² * I))
#spl = RWMH(MvNormal(zeros(2), I))


chain = AdvancedMH.sample(TheNormalLogDensity(inv(Σ)), spl, 500000; initial_params=ones(2))
data = stack([c.params for c = chain])
cov(data, dims=2)

Someone pointed out that MALA results in samples that have a wrong covariance.
It is not a mixing/variance issue since I'm hitting an acceptance rate of 0.57.
For the above example, I get:

julia> cov(data, dims=2)
2×2 Matrix{Float64}:
 0.500508  0.115434
 0.115434  0.332796

I didn't have the time to look into too much detail, but it seems that RWMH returns the correct covariance, while only MALA does not. For instance, RWMH yields:

julia> cov(data, dims=2)
2×2 Matrix{Float64}:
 1.47741   0.344062
 0.344062  0.995041
@Red-Portal Red-Portal changed the title MALA implementation has a bug MALA implementation results in the wrong covariance Feb 13, 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

No branches or pull requests

1 participant