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.5k stars 7.84k forks source link

Chapter 4 indicator function - fix probability calculation #488

Closed nstjhp closed 4 years ago

nstjhp commented 4 years ago

Currently there is an error here because the len function returns 1 for any N - change N to see how the "Probability Estimate" changes as 1/N.

Running my corrected version gives a much closer estimate of the true probability (~0.08208).

Also just to point out this TFP code is anyway different from the PyMC3 version because numpy use the scale rather than rate parameterisation of the exponential distribution. Thus the equivalent is shown below too.

N = 10000

print("Probability Estimate: ", len(np.where(evaluate(tfd.Exponential(rate=0.5).sample(sample_shape=N)) > 5))/N )

print("Corrected Probability Estimate: ", np.shape(np.where(evaluate(tfd.Exponential(rate=0.5).sample(sample_shape=N)) > 5))[1]/N )

print("From Pymc3 Probability Estimate: ",  np.mean( [ np.random.exponential(scale=2, size=N) > 5 ] ) )
CamDavidsonPilon commented 4 years ago

nice catch!