pymc-devs / pymc

Bayesian Modeling and Probabilistic Programming in Python
https://docs.pymc.io/
Other
8.62k stars 1.99k forks source link

HalfNormal not correct #1196

Closed sammosummo closed 8 years ago

sammosummo commented 8 years ago

Neither the pm.HalfNormal distribution nor taking the absolute value of a zero-mean pm.Normal distribution seem to produce accurate half-normal distributions:

import pymc3 as pm
import matplotlib.pyplot as plt

with pm.Model() as model:

    x = pm.HalfNormal(name='x', sd=1)
    y = pm.Normal(name='y', sd=1)
    z = pm.Deterministic('z', abs(y))
    trace = pm.sample(500000)
    pm.traceplot(trace)
    plt.savefig('test.png')

test

ririw commented 8 years ago

I just noticed that the buggyness seems to change depending on the sampler. With Metropolis:

with pymc3.Model() as model:
    x = pymc3.HalfNormal(name='x', sd=1)
    y = pymc3.Normal(name='y', sd=1)
    z = pymc3.Deterministic('z', abs(y))
    step = pymc3.Metropolis()
    trace = pymc3.sample(500000, step=step)
    pymc3.traceplot(trace)

metrop

Anyway, just an observation, hopefully it helps. Perhaps the sampler is having trouble taking steps that take it close to zero, given that it'd be so easy to overshoot and end up overshooting and get rejected (in the half-normal case) or overshoot and end up further from zero (in the abs case)

twiecki commented 8 years ago

Looks a bit different when I run it with NUTS: image

I think you're not up-to-date. I also ran it without transformation and Metropolis and it looks the same. Probably just very difficult to sample extremely close to 0.

aloctavodia commented 8 years ago

I get essentially the same results with NUTS or metropolis (similar to those of @ririw and @twiecki ). I think part of the problem you are seeing is only due to visualization and not sampling. The Kernel Density Estimation implemented in SciPy does not take into account boundary effects. As a result underestimates values close to the boundaries (like 0). I have made a PR upstream to fix this problem. Once it get merged I will make the necessary changes into PyMC3.

twiecki commented 8 years ago

@aloctavodia good catch: image

sammosummo commented 8 years ago

Just updated by cloning the repo and installing from source, and the issue is resolved. Thanks!