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

Slice sampler does not converge in simple MvNormal prior model #5815

Closed
ricardoV94 opened this issue May 28, 2022 · 5 comments · Fixed by #5816
Closed

Slice sampler does not converge in simple MvNormal prior model #5815

ricardoV94 opened this issue May 28, 2022 · 5 comments · Fixed by #5816

Comments

@ricardoV94
Copy link
Member

This example is taken from the step_tests here:

def test_step_continuous(self):

Please provide a minimal, self-contained, and reproducible example.

import pymc as pm
import numpy as np

mu = np.array([-0.1, 0.5, 1.1])
p = np.array([[2.0, 0, 0], [0.05, 0.1, 0], [1.0, -0.05, 5.5]])
tau = np.dot(p, p.T)

with pm.Model() as model:
    pm.MvNormal( "x",  mu, tau=tau,)

expected_mean = mu
expected_std = np.diag(np.linalg.inv(tau)) ** 0.5

with model:
    trace = pm.sample(
        tune=10_000,
        draws=10_000,
        chains=1,
        step=pm.Slice(),
        return_inferencedata=False,
    )

# Very bad, the second mean seems to be consistently biased
print(expected_mean, trace["x"].mean(axis=0))
# [-0.1  0.5  1.1] [-0.14368236  1.76355742  1.11952911]

# Quite good
print(expected_std, trace["x"].std(axis=0)) 
# [ 0.56672996 10.00041321  0.18181818] [0.56335647 9.88682831 0.18468393]

This was also the case in V3: https://colab.research.google.com/drive/1Pw7a-1J-bnb4dPsPvxwuWH6TdFENYb5m?usp=sharing

@ricardoV94 ricardoV94 changed the title Slice sampler performs horribly in simle MvNormal prior model Slice sampler does not convenge in simple MvNormal prior model May 28, 2022
@ricardoV94
Copy link
Member Author

ricardoV94 commented May 28, 2022

Well, Metropolis is even worse?

changing to step=pm.Metropolis(), I sometimes get stuff like:

# Very bad, the second mean seems to be consistently biased
print(expected_mean, trace["x"].mean(axis=0))
# [-0.1  0.5  1.1] [  0.29549842 -14.04723709   1.09591073]

# Also poor
print(expected_std, trace["x"].std(axis=0)) 
# [ 0.56672996 10.00041321  0.18181818] [0.53156382 4.79229857 0.18407155]

Edit: Metropolis can cope if we pass an appropriate S to it for the covariance of the proposal

@ricardoV94 ricardoV94 changed the title Slice sampler does not convenge in simple MvNormal prior model Metropolis and Slice sampler do not convenge in simple MvNormal prior model May 28, 2022
@twiecki
Copy link
Member

twiecki commented May 28, 2022

What do the traces look like?

@ricardoV94
Copy link
Member Author

Slice sampler:
image

Metropolis sampler:
image

NUTS:
image

@ricardoV94
Copy link
Member Author

ricardoV94 commented May 28, 2022

I understand the default Metropolis has one single proposal distribution (?), but the Slice should propose values independently per dimension, so I don't understand why is it still biased.

Maybe it's not too biased, it's just the crazy standard deviation of the second component?

Edit: Yeah, maybe the test was just too damn strict

@ricardoV94
Copy link
Member Author

I think I found a bug in the Slice sampler. It is no longer biased after fixing it

ricardoV94 added a commit to ricardoV94/pymc that referenced this issue May 28, 2022
The number of draws was set too high to accommodate the worst / buggy samplers (see pymc-devs#5815)
@ricardoV94 ricardoV94 changed the title Metropolis and Slice sampler do not convenge in simple MvNormal prior model Slice sampler do not convenge in simple MvNormal prior model May 28, 2022
ricardoV94 added a commit to ricardoV94/pymc that referenced this issue May 28, 2022
The number of draws was set too high to accommodate the worst / buggy samplers (see pymc-devs#5815)
ricardoV94 added a commit to ricardoV94/pymc that referenced this issue May 28, 2022
The number of draws was set too high to accommodate the worst / buggy samplers (see pymc-devs#5815)
@ricardoV94 ricardoV94 changed the title Slice sampler do not convenge in simple MvNormal prior model Slice sampler does not converge in simple MvNormal prior model May 28, 2022
ricardoV94 added a commit to ricardoV94/pymc that referenced this issue May 30, 2022
The number of draws was set too high to accommodate the worst / buggy samplers (see pymc-devs#5815)
ricardoV94 added a commit to ricardoV94/pymc that referenced this issue May 30, 2022
The number of draws was set too high to accommodate the worst / buggy samplers (see pymc-devs#5815)
ricardoV94 added a commit to ricardoV94/pymc that referenced this issue May 30, 2022
The number of draws was set too high to accommodate the worst / buggy samplers (see pymc-devs#5815)
michaelosthege pushed a commit that referenced this issue May 30, 2022
The number of draws was set too high to accommodate the worst / buggy samplers (see #5815)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants