.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/probability/bayes/plot_01_bayes_rule_of_succession.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_probability_bayes_plot_01_bayes_rule_of_succession.py: Bayes's problem: learning a probability from data ======================================================== Starting from Bayes's uniform prior, the posterior for an unknown success probability :math:`p` after :math:`k` successes in :math:`n` trials is :math:`\mathrm{Beta}(k+1, n-k+1)`. It narrows around the true value as data accumulate, and its mean is Laplace's rule of succession :math:`(k+1)/(n+2)`. .. GENERATED FROM PYTHON SOURCE LINES 13-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.probability import beta_binomial_posterior, rule_of_succession .. GENERATED FROM PYTHON SOURCE LINES 19-21 Posteriors after more and more trials ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-39 .. code-block:: Python p_true = 0.3 rng = np.random.default_rng(0) flips = rng.random(1000) < p_true x = np.linspace(0.0, 1.0, 500) fig, ax = plt.subplots() for n in (0, 5, 20, 100, 1000): k = int(flips[:n].sum()) post = beta_binomial_posterior(k, n) lo, hi = post.ppf([0.025, 0.975]) print(f"n={n:>4}, k={k:>3}: next-success probability {rule_of_succession(k, n):.4f}, 95% interval [{lo:.3f}, {hi:.3f}]") ax.plot(x, post.pdf(x), label=f"n = {n}") ax.axvline(p_true, color="0.3", ls="--", label="true p") ax.set_xlabel("p") ax.set_ylabel("posterior density") ax.legend() ax.set_title("Bayes (1763): Beta posteriors from a uniform prior") .. image-sg:: /api/gallery/probability/bayes/images/sphx_glr_plot_01_bayes_rule_of_succession_001.png :alt: Bayes (1763): Beta posteriors from a uniform prior :srcset: /api/gallery/probability/bayes/images/sphx_glr_plot_01_bayes_rule_of_succession_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none n= 0, k= 0: next-success probability 0.5000, 95% interval [0.025, 0.975] n= 5, k= 3: next-success probability 0.5714, 95% interval [0.223, 0.882] n= 20, k= 7: next-success probability 0.3636, 95% interval [0.181, 0.570] n= 100, k= 25: next-success probability 0.2549, 95% interval [0.176, 0.343] n=1000, k=277: next-success probability 0.2774, 95% interval [0.250, 0.306] Text(0.5, 1.0, 'Bayes (1763): Beta posteriors from a uniform prior') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.034 seconds) .. _sphx_glr_download_api_gallery_probability_bayes_plot_01_bayes_rule_of_succession.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_bayes_rule_of_succession.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_bayes_rule_of_succession.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_bayes_rule_of_succession.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_