I am learning this post.

The book gives this figure to illustrate how posterior probabilities shift and move around

enter image description here

Here is the code

%matplotlib inline
from IPython.core.pylabtools import figsize
import numpy as np
from matplotlib import pyplot as plt
figsize(11, 9)

import scipy.stats as stats

dist = stats.beta
n_trials = [0, 1, 2, 3, 4, 5, 8, 15, 50, 500]
data = stats.bernoulli.rvs(0.5, size=n_trials[-1])
x = np.linspace(0, 1, 100)

# For the already prepared, I'm using Binomial's conj. prior.
for k, N in enumerate(n_trials):
    sx = plt.subplot(len(n_trials) / 2, 2, k + 1)
    plt.xlabel("$p$, probability of heads") \
        if k in [0, len(n_trials) - 1] else None
    plt.setp(sx.get_yticklabels(), visible=False)
    heads = data[:N].sum()
    y = dist.pdf(x, 1 + heads, 1 + N - heads)
    plt.plot(x, y, label="observe %d tosses,\n %d heads" % (N, heads))
    plt.fill_between(x, 0, y, color="#348ABD", alpha=0.4)
    plt.vlines(0.5, 0, 4, color="k", linestyles="--", lw=1)

    leg = plt.legend()

plt.suptitle("Bayesian updating of posterior probabilities",


Each element in the n_trials list indicates how many times a coin has been tossed.

data represents the records of the trials of tossing a coin.

what does this line mean?

x = np.linspace(0, 1, 100)
  • $\begingroup$ I believe it creates a linear set of values starting with the first argument, 0, finishing with the second argument, 1, and creating 100-2 = 98 linearly spaced values in between. In this case x would be [ 0 0.0101 0.0202 0.0303 ... 0.9899 1 ]. It is used in this example as the x-axis. $\endgroup$
    – MikeP
    Jul 18, 2019 at 16:42

1 Answer 1


for simplicity, start with this figure to illustrate the mechanism, others also hold.

enter image description here

in this case, x ranges from 0 to 1, y from 2 to 0.

y represents a pdf, which indicates a function has a 2% probability to get a 0 for the value of x, 1.98% probability for 0.01.

naive Bayes model learned from data, adjust its guess from equally likely to 0 has the highest probability (2%).

more data comes in, naive Bayes model has a more precise guess about the pdf, eventually the model guess the 0.5 has the highest probability, this is the last figure.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.