
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/animation/bayes_update.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_animation_bayes_update.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_animation_bayes_update.py:


================
The Bayes update
================

This animation displays the posterior estimate updates as it is refitted when
new data arrives.
The vertical line represents the theoretical value to which the plotted
distribution should converge.

Output generated via `matplotlib.animation.Animation.to_jshtml`.

.. GENERATED FROM PYTHON SOURCE LINES 13-73

.. code-block:: Python


    import math

    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib.animation import FuncAnimation


    def beta_pdf(x, a, b):
        return (x**(a-1) * (1-x)**(b-1) * math.gamma(a + b)
                / (math.gamma(a) * math.gamma(b)))


    class UpdateDist:
        def __init__(self, ax, prob=0.5):
            self.success = 0
            self.prob = prob
            self.line, = ax.plot([], [], 'k-')
            self.x = np.linspace(0, 1, 200)
            self.ax = ax

            # Set up plot parameters
            self.ax.set_xlim(0, 1)
            self.ax.set_ylim(0, 10)
            self.ax.grid(True)

            # This vertical line represents the theoretical value, to
            # which the plotted distribution should converge.
            self.ax.axvline(prob, linestyle='--', color='black')

        def start(self):
            # Used for the *init_func* parameter of FuncAnimation; this is called when
            # initializing the animation, and also after resizing the figure.
            return self.line,

        def __call__(self, i):
            # This way the plot can continuously run and we just keep
            # watching new realizations of the process
            if i == 0:
                self.success = 0
                self.line.set_data([], [])
                return self.line,

            # Choose success based on exceed a threshold with a uniform pick
            if np.random.rand() < self.prob:
                self.success += 1
            y = beta_pdf(self.x, self.success + 1, (i - self.success) + 1)
            self.line.set_data(self.x, y)
            return self.line,

    # Fixing random state for reproducibility
    np.random.seed(19680801)


    fig, ax = plt.subplots()
    ud = UpdateDist(ax, prob=0.7)
    anim = FuncAnimation(fig, ud, init_func=ud.start, frames=100, interval=100, blit=True)
    plt.show()




.. image-sg:: /gallery/animation/images/sphx_glr_bayes_update_001.png
   :alt: bayes update
   :srcset: /gallery/animation/images/sphx_glr_bayes_update_001.png, /gallery/animation/images/sphx_glr_bayes_update_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 74-75

.. tags:: animation, plot-type: line


.. _sphx_glr_download_gallery_animation_bayes_update.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: bayes_update.ipynb <bayes_update.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: bayes_update.py <bayes_update.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: bayes_update.zip <bayes_update.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
