{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Experiment with the scipy.stats.beta() function for generating a beta function. Choose different parameters a and b and find values that give a significantly asymmetric probability distribution function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from scipy.stats import beta\n", "a= # choose some different values\n", "b=\n", "x=np.linspace(0,1,101) # x values for plotting\n", "pdf= # use the method to get the pdf for your choices of a and b\n", "plt.plot(x,pdf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function that will take as arguments beta function parameters and a number of points to draw a sample from. Your function should draw a sample of the requested size and return the mean of that sample." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def betamean(a,b,n) :\n", " \"\"\" function to return the mean of n samples from a beta distribution with parameters a and b\n", " \"\"\"\n", " r = # use the method to return n samples from a beta function with input a and b\n", " return r.mean()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now write a loop that draws many samples of a given size, and plot a histogram of the mean values. Do this for several different sample sizes, and compare the resulting distributions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ndraws= # choose the number of draws for each sample size to get a well sampled distribution\n", "for nsamp in [...] : # add a list of different sample sizes here\n", " # build up an array of means\n", " means=[]\n", " for i in range(ndraws):\n", " means.append(betamean(a,b,nsamp))\n", " means=np.array(means)\n", " # plot the histogram of means for this sample size\n", " plt.hist(means,histtype='step',label='{:d}'.format(nsamp),bins=30)\n", "# label the lines\n", "plt.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Discuss how your results relate to the central limit theorem." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ANSWER HERE :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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.9.18" } }, "nbformat": 4, "nbformat_minor": 4 }