{ "cells": [ { "cell_type": "markdown", "id": "0213f008", "metadata": {}, "source": [ "# Histogram" ] }, { "cell_type": "code", "execution_count": null, "id": "eadf8523", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import scipy\n", "\n", "from smpl import animation, plot, stat" ] }, { "cell_type": "code", "execution_count": null, "id": "d706684b", "metadata": {}, "outputs": [], "source": [ "%matplotlib notebook \n", "# plt.ioff()\n", "# Parameter\n", "n = 13\n", "bahnhs = 13\n", "p = 1.0 / bahnhs\n", "\n", "bahnh = np.array(range(bahnhs)) * 0\n", "datax = np.array(range(1, 14))\n", "datay = datax * 0\n", "c = 0\n", "\n", "\n", "# Approximate factorial by gamma\n", "def fac(n):\n", " return scipy.special.gamma(n + 1)\n", "\n", "\n", "def update(a):\n", " global bahnh, datax, datay, c, bahnhs\n", " c += 1\n", " bahnh = bahnh * 0\n", " for i in range(n):\n", " bahnh[np.random.randint(0, bahnhs)] += 1\n", " if np.any(np.greater(bahnh, 1)):\n", " datay[i] += 1\n", " plot.data(\n", " datax,\n", " stat.poisson_dist(datay) / c,\n", " init=True,\n", " tight=False,\n", " fmt=\"hist\",\n", " ylabel=\"P(>1 Gäste bei beliebigem Bahnhof)\",\n", " xlabel=\"$n$ Gäste\",\n", " capsize=0,\n", " )\n", " plot.function(\n", " lambda ni: 1.0 - fac(bahnhs) / (fac(bahnhs - ni) * bahnhs**ni),\n", " xmin=0,\n", " xmax=13,\n", " label=\"$\\\\frac{b!}{(b-n)!b^n}$\",\n", " function_color=\"orange\",\n", " )\n", "\n", "\n", "def init():\n", " global datay, bahnh, c\n", " datay = datay * 0\n", " bahnh = bahnh * 0\n", " c = 0\n", "\n", "\n", "ani = animation.animate(\n", " update=update, frames=np.linspace(0, 20, 2000), interval=10, blit=False\n", ")\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "094ffe22", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "6168f906", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.16 64-bit", "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.9.16" }, "vscode": { "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" } }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }