{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Normal distribution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import math\n", "import scipy.stats\n", "\n", "food = pd.read_pickle(\"../data/processed/food\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The normal distribution is central to our ability to infer about a population from a sample. The normal distribution looks like this (by Dan Kernler from Wikimedia Commons, CC BY-SA 4.0):\n", "\n", "![Normal distribution CC BY-SA 4.0 by Dan Kernler](https://upload.wikimedia.org/wikipedia/commons/a/a9/Empirical_Rule.PNG)\n", "\n", "The normal distribution was discovered by Gauss (which is why it's also sometimes called the Gaussian distribution) and described an 'ideal' situation. Lots of data follow this pattern: the height or weight of a population; The mean of the normal distribution ($\\mu$ on the diagram above) is the centre. Because the normal distribution is **symmetrical** we know that 50% of cases fall below and 50% of cases fall above the mean.\n", "\n", "Another useful property of the normal distribution is we know, or can calculate, how many cases fall with 1, 2, 3, or more standard deviations of the mean (these are shown as $\\mu \\pm \\sigma; \\mu \\pm 2\\sigma; \\mu \\pm 3\\sigma$ on the figure). These are about 68%, 95%, and 97.5% respectively.\n", "\n", "|Number of $\\sigma$ from mean | Percent of cases|\n", "|-----------------------------|-----------------|\n", "| 1 | 68.27% |\n", "| 1.96 | 95% |\n", "| 2 | 95.45% |\n", "| 2.58 | 99% |\n", "| 3 | 99.73% |\n", "\n", "Therefore if we know the mean ($\\mu$) and standard deviation ($\\sigma$) of our sample we can calculate the confidence interval of our sample mean (as we did above). Typically we calculate a 95% confidence interval (although 99% is also common), and this tells us the likely range the population mean falls within. This is why we used the figure 1.96 when calculating our confidence interval earlier, and this is the property that allows us to infer information about the population from our smaller sample.\n", "\n", "The other, related, way we can use this information is if we have a mean and standard deviation and observe a data point, we can calculate how many standard deviations from the mean this data point is. We can then see if the observed data point falls within the normal variation we expect (i.e. within 1.96 standard deviations for 95% confidence) or outside it, and is therefore the result of something not within the normal range.\n", "\n", "Remember our household weekly income example, after we removed the top--coded responses? It looked something like this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "food.P344pr.hist(bins = 100)\n", "plt.xlabel(\"Weekly income (£)\")\n", "plt.ylabel(\"Frequency\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The mean income is:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "food.P344pr.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and the standard deviation is:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "food.P344pr.std()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's imagine we find a respondent who lives in Chelsea and we ask them to complete our survey. They respond that their income is £2,000 per week. Does this fall within the variability we expect, or is it statistically significantly likely that this respondent has a higher income than most? Well, we can calculate how many standard deviations our observed data point is away from the mean. We know:\n", "\n", "$$\n", "2000 = \\mu + x.\\sigma\n", "$$\n", "\n", "where $\\mu$ is the mean, $\\sigma$ is the standard deviation, and $x$ is the number of standard deviations we want to calculate. If we rearrange the equation we get:\n", "\n", "$$\n", "\\frac{2000}{\\sigma} = \\frac{\\mu}{\\sigma} + x\n", "$$\n", "\n", "$$\n", "\\frac{(2000 - \\mu)}{\\sigma} = x\n", "$$\n", "\n", "Plugging in the mean and standard deviation we get:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(2000 - food.P344pr.mean()) / food.P344pr.std()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So our observed data point is more than five standard deviations higher than the mean, which means that if we were to interview 3.5 million people, only one would have an income that high. It's therefore highly likely that this respondent has a higher income than the average. In physics this would be known as a 'five-sigma' result: i.e. the result is more than five standard deviations ($\\sigma$) from the mean and is therefore highly unlikely to be through chance alone (in the social sciences we usually opt for the '1.96 sigma' rule).\n", "\n", "Not all observed data form a perfect normal distribution (in fact most differ at least slightly). There are two ways we need to describe a distribution if it is different from the normal: skewness and kurtosis.\n", "\n", "## Skewness\n", "\n", "A normal distribution has its mean, median, and mode at the same point (the centre). Skewness means the data points are skewed one way or the other:\n", "\n", "\"Negative\n", "\n", "Negative skew means the tail is to the left; positive skew means the tail is to the right. In a positively skewed distribution the mode and median are lower than the mean. In a negatively skewed distribution the median and mode are higher than the mean.\n", "\n", "## Kurtosis\n", "\n", "Kurtosis refers to how bunched (clustered) around the mean the data points are. Positive kurtosis (leptokurtic) means the points are clustered around the mean, making the distribution narrower and taller than a normal distribution. Negative kurtosis (platykurtic) means the data points are spread out more widely, resulting in a distribution that is flatter and broader than a normal distribution." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mu = 0\n", "variance = 1\n", "sigma = math.sqrt(variance)\n", "normal = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 100) # probability density func\n", "plt.plot(normal, scipy.stats.norm.pdf(normal, mu, sigma))\n", "\n", "variance = 2\n", "sigma = math.sqrt(variance)\n", "platykurtic = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 100)\n", "plt.plot(platykurtic, scipy.stats.norm.pdf(platykurtic, mu, sigma))\n", "\n", "variance = 0.5\n", "sigma = math.sqrt(variance)\n", "leptokurtic = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 100)\n", "plt.plot(leptokurtic, scipy.stats.norm.pdf(leptokurtic, mu, sigma))\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the figure above:\n", "\n", "- the **blue** line is a normal distribution,\n", "- the **green** line is a distribution with positive kurtosis (leptokurtic)\n", "- the **orange** line is a distribution with negative kurtosis (platykurtic)" ] } ], "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }