{ "cells": [ { "cell_type": "markdown", "id": "ba322e0b-5b8f-473f-90c3-dbfb10578b34", "metadata": {}, "source": [ "## Physics 310\n", "\n", "### Class 2\n", "\n", "### Tuesday January 23, 2024\n", "\n", "\n", "Goals:\n", "* Introduce tools and functions for plotting\n", "* Introduce tools for generating random numbers and histograms\n", "* Experiment with PDFs (probability distribution functions)" ] }, { "cell_type": "code", "execution_count": 3, "id": "61460d81-be45-42ae-95b9-b8383d2643bb", "metadata": {}, "outputs": [], "source": [ "## Import relevant packages\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "from scipy import stats\n" ] }, { "cell_type": "markdown", "id": "7581bf99-2e1e-4838-8002-43502dc64640", "metadata": {}, "source": [ "## The linspace function\n", "\n", "Often, we will fine that we need to generate a regularly spaced array of numbers, such as when creating an x-axis, or a set of independent variables\n", "\n", "The ```np.linspace``` is really valuable in this regard. What does it do? Look up its syntax by running the following command\n", "\n", "```?np.linspace```\n", "\n", "Run the following commands\n", "```np.linspace(0,30,10)``` and note what each varible does" ] }, { "cell_type": "code", "execution_count": null, "id": "432b4c1e-8077-43f2-af83-cec2d69f6805", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "227b61c8-95ce-467f-9140-9f29bf77a54b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "5da10776-8829-45e2-9df7-b285be704bb5", "metadata": {}, "source": [ "Two possible functions that we can use for geneerating random numbers are\n", "\n", "```\n", "np.random.random.rand_int\n", "```\n", "and\n", "```\n", "np.random.random_sample\n", "```\n", "\n", "Look up the help file to determine the syntax of these two functions\n", "* What do the functions take as input variables\n", "* Do the functions have any optional variables? How do you know\n", "* Use the function to generate an array of 10 random numbers\n", "\n" ] }, { "cell_type": "code", "execution_count": 18, "id": "5e02d387-04af-43f8-8e6f-0b4684308a6f", "metadata": {}, "outputs": [], "source": [ "#?np.random.random_sample" ] }, { "cell_type": "markdown", "id": "f11da288-e657-4883-9951-97fdab20b7ee", "metadata": {}, "source": [ "### Gaussian Distribution\n", "\n", "This is given by \n", "\n", "$p(x) = \\frac{1}{\\sigma \\sqrt{2\\pi}}exp\\left[-\\frac{(x-\\bar{x})^2}{2\\sigma^2}\\right]$\n", "\n", "The snippet of code below generates 10 samples from a normal distribution\n", "```\n", "# Sampling from normal distribution\n", "n = 10\n", "mean = 10.\n", "sigma = 2.\n", "stats.norm.rvs(mean, sigma, size=n) \n", "```" ] }, { "cell_type": "code", "execution_count": 27, "id": "e4064506-c753-4306-b078-9317b6611e24", "metadata": {}, "outputs": [], "source": [ "# Sampling from normal distribution\n", "n = 1000\n", "mean = 10.\n", "sigma = 2.\n", "ynorm = stats.norm.rvs(mean, sigma, size=n) " ] }, { "cell_type": "code", "execution_count": null, "id": "6083e6e1-b675-4f95-979a-4af96d13d290", "metadata": {}, "outputs": [], "source": [ "x = np.linspace(0,20,200) # make an array of 200 evenly spaced values from 0 to 20\n", "y = stats.norm.pdf(x, mean, sigma) # determine the value of the pdf at each of the points in 'x'\n", "plt.title(\"pdf of normal distribution\")\n", "plt.xlabel(\"$x$\")\n", "plt.ylabel(\"$p(x)$\")\n", "plt.grid()\n", "plt.plot(x, y);" ] }, { "cell_type": "markdown", "id": "4d756685-2053-4558-a0ee-6804b7ed12a9", "metadata": {}, "source": [ "Consider the following snippet of code\n", "```\n", "n = 1000\n", "mean = 10.\n", "sigma = 2.\n", "ynorm = stats.norm.rvs(mean, sigma, size=n) \n", "```\n", "\n", "Before runnint it, describe what it will do.\n", "\n", "Now generate a histogram of using:\n", "\n", "```\n", "plt.hist(ynorm)\n", "plt.xlabel(\"value\")\n", "plt.ylabel(\"occurences\")\n", "plt.title(\"Histogram; equal sized bins\")\n", "```" ] }, { "cell_type": "markdown", "id": "c98073ac-0ad0-49ba-9902-8f8022465a6f", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b3f0171a-20de-4e8c-89ed-0c00b87971bb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "dffb4027-bc7b-427e-ab5c-ed20469421ca", "metadata": {}, "source": [ "Now the plt.hist function has a number of optional variables. For example, you can normalize it\n", "using density = True, and you can change the relative width of the cells by using rwidth = ...\n", "\n", "Try this snippet of code, and think about what each line an each optional variable does\n", "\n", "```\n", "plt.hist(ynorm, density = True,rwidth=0.8,label = 'samples')\n", "plt.plot(x, y, label = 'pdf');\n", "plt.legend()\n", "```\n", "\n", "How would you add labels on the x and y axes?" ] }, { "cell_type": "code", "execution_count": null, "id": "661cefba-0737-4e22-a0e6-cf821808c7e7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "9797febf-2e56-4d99-bc9a-f2ac2bc4a54a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "9c959bd3-89c9-4d24-8a9a-7496ac645c7e", "metadata": {}, "source": [ "## Using other distributions\n", "\n", "The stats module implements dozens of other distributions. For example, to generate random values from a binomial distribution, you may use\n", "\n", "```stats.binom.rvs(n,p,size = 100)``` where ```n``` and ```p``` are the two required variables for the binomial distribution. \n", "\n", "```stats.poisson.rvs(mean, size=100)``` generates a sample of 100 random variables form the poisson distribution. Note that the poisson distribution is a function of a single variable. Namely the mean number of counts.\n", "\n", "Experiment with generating random numbers and histograms of these distributions." ] }, { "cell_type": "code", "execution_count": null, "id": "18013a25-2dd6-456a-b29a-31259ddca19d", "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.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }