{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Central Limit Demo\n", "John Horel\n", "ATMOS 5340\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sides = input('enter the number of sides on the die (6 or 10 or...): ')\n", "dice = input('enter the sample size (number of dice to roll at the same time): ')\n", "rolls = input('enter the number of rolls: ')\n", "sides = int(sides)\n", "dice = int(dice)\n", "rolls = int(rolls)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rd = np.random.random_sample((rolls,dice))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rdd = np.ceil(sides*rd)\n", "#print(rdd)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#define the population as all the possible rolls\n", "pop = rdd.flatten();\n", "print(len(rd))\n", "#compute std of population\n", "pstd = np.std(pop,ddof=1);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#compute the sum and mean of the samples\n", "if dice>1:\n", " ss = np.sum(rdd,axis=1)\n", " sm = np.mean(rdd,axis=1)\n", "else:\n", " ss = rdd\n", " sm = rdd \n", " \n", "mean_sum = np.mean(ss)\n", "mean_mean = np.mean(sm)\n", "print(mean_sum, mean_mean)\n", "\n", "#compute the std of the sample means \n", "sstd = np.std(sm,ddof=0);\n", "#does the central limit theorem work? is the following close to sstd?\n", "check = pstd/np.sqrt(dice);\n", "print(\"population standrd deviation= %.1f\" % pstd)\n", "print(\"standard deviation of sample means= %.1f\" % sstd)\n", "print(\"check for central limit theorem= %.1f\" % check)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig,(ax1,ax2,ax3) = plt.subplots(3,1,figsize=(10,10))\n", "x = np.arange(0,rolls*dice)\n", "ax1.bar(x,pop)\n", "ax1.set(xlabel=\"\",ylabel='Roll Value')\n", "ax1.set(title='Population- each die is rolled independently ')\n", "x2 = np.arange(1,sides*dice+2,1)\n", "\n", "hist_val,bins = np.histogram(ss,bins=x2)\n", "#print(hist_val)\n", "#print(bins)\n", "width = 0.9 * (bins[1] - bins[0])\n", "center = (bins[:-1] + bins[1:]) / 2\n", "ax2.bar(center,hist_val,align='center',width=width)\n", "ax2.set(xlim=(dice,sides*dice+1))\n", "ax2.set(xlabel=\"\",ylabel='# of Occurrences')\n", "ax2.set(title='Sample Sums')\n", "\n", "hist_val,bins = np.histogram(sm,bins=x2)\n", "#print(bins)\n", "width = 0.9 * (bins[1] - bins[0])\n", "center = (bins[:-1] + bins[1:]) / 2\n", "ax3.bar(center,hist_val,align='center',width=width)\n", "ax3.set(xlim=(1,sides+1))\n", "ax3.set(xlabel=\"\",ylabel='# of Occurrences')\n", "ax3.set(title='Sample Means')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.4" } }, "nbformat": 4, "nbformat_minor": 4 }