{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# The Bayes update\n\nThis animation displays the posterior estimate updates as it is refitted when\nnew data arrives.\nThe vertical line represents the theoretical value to which the plotted\ndistribution should converge.\n\nOutput generated via `matplotlib.animation.Animation.to_jshtml`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import math\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.animation import FuncAnimation\n\n\ndef beta_pdf(x, a, b):\n    return (x**(a-1) * (1-x)**(b-1) * math.gamma(a + b)\n            / (math.gamma(a) * math.gamma(b)))\n\n\nclass UpdateDist:\n    def __init__(self, ax, prob=0.5):\n        self.success = 0\n        self.prob = prob\n        self.line, = ax.plot([], [], 'k-')\n        self.x = np.linspace(0, 1, 200)\n        self.ax = ax\n\n        # Set up plot parameters\n        self.ax.set_xlim(0, 1)\n        self.ax.set_ylim(0, 10)\n        self.ax.grid(True)\n\n        # This vertical line represents the theoretical value, to\n        # which the plotted distribution should converge.\n        self.ax.axvline(prob, linestyle='--', color='black')\n\n    def start(self):\n        # Used for the *init_func* parameter of FuncAnimation; this is called when\n        # initializing the animation, and also after resizing the figure.\n        return self.line,\n\n    def __call__(self, i):\n        # This way the plot can continuously run and we just keep\n        # watching new realizations of the process\n        if i == 0:\n            self.success = 0\n            self.line.set_data([], [])\n            return self.line,\n\n        # Choose success based on exceed a threshold with a uniform pick\n        if np.random.rand() < self.prob:\n            self.success += 1\n        y = beta_pdf(self.x, self.success + 1, (i - self.success) + 1)\n        self.line.set_data(self.x, y)\n        return self.line,\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)\n\n\nfig, ax = plt.subplots()\nud = UpdateDist(ax, prob=0.7)\nanim = FuncAnimation(fig, ud, init_func=ud.start, frames=100, interval=100, blit=True)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. tags:: component: animation, plot-type: line\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}