CamDavidsonPilon / Probabilistic-Programming-and-Bayesian-Methods-for-Hackers

aka "Bayesian Methods for Hackers": An introduction to Bayesian methods + probabilistic programming with a computation/understanding-first, mathematics-second point of view. All in pure Python ;)
http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/
MIT License
26.51k stars 7.84k forks source link

Bug in Chapter 1 Pyro Version: Exponential Distribution Input #535

Closed JAEarly closed 2 years ago

JAEarly commented 2 years ago

In the Pyro Version of Chapter 1, the following code block has an issue:

lambda_ = [0.5, 1]
d = dist.Exponential(torch.tensor(lambda_))

x = torch.linspace(0, 4, 100).view(-1, 1)
y = torch.exp(d.log_prob(x))

It raises the error:

ValueError: The value argument must be within the support

Problem: The underlying PyTorch code for the exponential distribution only allows inputs greater than zero, so as x includes zero, this block fails. I think the underlying issue is with PyTorch, so I've opened a PR to change the constraints for the exponential function from positive (>0) to non-negative (>=0). If that change goes through, this issue is moot, but if they don't change it, then the block will need to be updated.

JAEarly commented 2 years ago

A quick fix is just to add a small value to x[0], e.g.:

x[0] += 1e-8

Then this block runs and the subsequent plot looks correct.

JAEarly commented 2 years ago

My PR to PyTorch to fix this has been accepted, so this bug will no longer happen if you have the latest version of PyTorch. Closing as fixed.