Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. %matplotlib inline
  2. from IPython.core.pylabtools import figsize
  3. import numpy as np
  4. from matplotlib import pyplot as plt
  5. figsize(11, 9)
  6.  
  7. import scipy.stats as stats
  8.  
  9. dist = stats.beta
  10. n_trials = [0, 1, 2, 3, 4, 5, 8, 15, 50, 500]
  11. data = stats.bernoulli.rvs(0.5, size=n_trials[-1])
  12. x = np.linspace(0, 1, 100)
  13.  
  14. # For the already prepared, I'm using Binomial's conj. prior.
  15. for k, N in enumerate(n_trials):
  16. sx = plt.subplot(len(n_trials) / 2, 2, k + 1)
  17. plt.xlabel("$p$, probability of heads")
  18. if k in [0, len(n_trials) - 1] else None
  19. plt.setp(sx.get_yticklabels(), visible=False)
  20. heads = data[:N].sum()
  21. y = dist.pdf(x, 1 + heads, 1 + N - heads)
  22. plt.plot(x, y, label="observe %d tosses,n %d heads" % (N, heads))
  23. plt.fill_between(x, 0, y, color="#348ABD", alpha=0.4)
  24. plt.vlines(0.5, 0, 4, color="k", linestyles="--", lw=1)
  25.  
  26. leg = plt.legend()
  27. leg.get_frame().set_alpha(0.4)
  28. plt.autoscale(tight=True)
  29.  
  30.  
  31. plt.suptitle("Bayesian updating of posterior probabilities",
  32. y=1.02,
  33. fontsize=14)
  34.  
  35. plt.tight_layout()
  36.  
  37. x = np.linspace(0, 1, 100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement