{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Normal Distribution" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Normal Distribution will is the distribution which calculates popularity of the population. This will get discussed on including standard deviation to determine Z score of particular value." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w40.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/49) video, 01:57*\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This distribution shows the distribution for both male and female, with height repored on the U.S survey and OkCupid. The height distribution of OkCupid is slightly to the right, which can be told as people often over-exaggerated their heights/tends to round up the height to the top." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many data is not exactly shape like a bell-curve, as its represent the real data, it's natural that it's nearly but not exactly normal. But when you see that the distribution tends to like bell-curve, we can assume the distribution is normal distribution, an unimodal. A normal distribution will make all data distributed around center, the mean." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w42.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/49) video, 05:29*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The fact that the questions include the data to have normal distribution, is our critical information. A normal distribution should have all data that are in 3 standard deviation, 99.7%. With that in mind, because we are given 3 statistics value, we can predict the standard deviation that closely match the minimum and maximum. Turns out the answer (b) has matched the criteria perfectly." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w43.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/49) video, 06:29*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is one of the example of standarized test. Instead of comparing raw score, which would give Pam absolute winning, we can calculate Z-score, which tells us the both scores in their respective standard deviation away from the mean." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w44.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/49) video, 07:20*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotting all of the value in standarized distributin, we see clearly that Pam has higher value from Jim." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w45.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/49) video, 09:03*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Z score comes from letter z in 'standardizing', s has taken for 'standard deviation'. Z score of the mean is simply 0, because it only subtracting for the mean value iself.\n", "\n", "When the distribution is converted to the standardized Z score, the mean will be 0, and the median will roughly 0 as well, because it's normal distribution.When you're standardizing, it's not take the original distribution, rather standardized distribution, which is exact normal distribution centered at 0. Therefore positive skewed will give you negative median, negative skewed will give you positive median." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Z-scores can also be used to calculate percentiles, which is the percentage of population below the mean. Other's distribution can take percentile as well, as long it's unimodal. But those will require us to using extra calculus.Note that this means **regardless the shape or skew of the distribution, z can always be defined, provided mean and standard deviation. But this doesn't mean we can compute percentiles, percentiles distribution need normalty**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can easily using R to specify the what value that we want to observe, the mean, and the value of standard deviation using R" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load_ext rmagic" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.15865525])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R pnorm(-1, mean=0, sd=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w46.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/49) video, 14:37*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This question can be arrived at solving it in R or by using percentage. Note that we can also counting the percentiles manually but it's not recommended. We already know 68%-95%-99.7%, and we know that below standard deviation of 1 is roughly 84%.Using R:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.84134475])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R pnorm(1800, mean=1500, sd=300)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This will answer that Pam's score is better than 84% of her class. We always calculate the percentiles **below** the value. To calculate who's better than her, we just calculate the complementary, which resulting in 0.1587" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.72574688])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R pnorm(24,mean=21,sd=5)dd" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2160.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0.9*2400" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w47.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/49) video, 17:17*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using this we can determine the X value, in R, we're using different method. Given quantile/percentile that is cut, we're trying to predict the value that resides there. Note that because she's in top 10% we cut 10% from the top." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1884.46546966])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R qnorm(0.90,1500,300)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Evaluating Normal Distribution" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w48.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/51) video, 00:15*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the usual plotting histogram and scatter plot to validate if the population indeed has normal distribution." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w48.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/51) video, 01:23*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w50.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/51) video, 02:44*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also checking the spread of the distribution by plotting standard deviation. If the spread of the distribution in 1 standard deviation is higher than 68% (normal distribution in 1 standard deviation), it's expected that the distribution has more variable, more spread." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Normal Distribution Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w52.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/53) video, 00:41*" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.059085122932667544" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "revans = %R pnorm(50,mean=45,sd=3.2)\n", "1 - revans[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5.9% passengers are expected of excess the bagage." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w51.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/53) video, 04:06*" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 72.79189383])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R qnorm(0.2, 77, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w53.jpg)\n", "\n", "*Screenshot taken from [Udacity](https://www.udacity.com/course/viewer#!/c-ud827/l-1478678538/m-102088771) video, 00:36*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember that sum of normal distribution, because it all based on relative frequency add up to 1? That's called Probability Density Function (PDF)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this way, because all of the relative frequency add up to 1, we can treat it the same as the probability" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w54.jpg)\n", "\n", "*Screenshot taken from [Udacity](https://www.udacity.com/course/viewer#!/c-ud827/l-1478678538/e-103339228/m-102088774) video, 00:12*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In normal distribution, the two-tail is never touch the x-axis(horizontal asymptote). That's because we can never be sure at 100%. If the distribution touch at x-axis, then it equals 1, however the PDF just close to 1, even it extend to 5 standard deviations away.\n", "\n", "The green area is the probability of randomly selecting less than the threshold, which in turn proportion in sample/population with score less than the threshold. So you see, the two-tail actually goes to infinity." ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.815" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0.95-0.135" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w55.jpg)\n", "\n", "*Screenshot taken from [Udacity](https://www.udacity.com/course/viewer#!/c-ud827/l-1478678538/e-102833897/m-102088794) video, 00:12*" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.91756673])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R pnorm(240,mean=190,sd=36)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w56.jpg)\n", "\n", "*Screenshot taken from [Udacity](https://www.udacity.com/course/viewer#!/c-ud827/l-1478678538/e-102833890/m-102088819) video, 02:09*" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.46802894190259892" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = %R pnorm(16,mean=13,sd=4.8)\n", "a = 1-a[0]\n", "b = %R pnorm(10,mean=13,sd=4.8)\n", "1.0 - a - b[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w58.jpg)\n", "\n", "*Screenshot taken from [Udacity](https://www.udacity.com/course/viewer#!/c-ud827/l-1478678538/m-102088823) video, 02:09*" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 20.89529741])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R qnorm(0.95,13,4.8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sampling Distributions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We know how we can compare between two normal distribution. But how about sample distributions? How can we compare of one sample with the other sample within that population? We can:\n", "\n", "* find the mean of the sample\n", "* find other means of other samples\n", "* And we can compare all the means from samples." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we're using thethral die, and roll 2 times, it would mean we're sampling size 2 out of our population, resulting in 2^4 = 16 total possibilities.We can take means of all sampling possibilities.After that, if we take average of all means possibilities, we get Means that equals to the means of the population.This all sample means that we get from the means of sample size 2 possibilities.\n", "\n", "Mean of each sample: \n", "1, 1.5, 2, 2.5, 1.5, 2, 2.5, 3, 2, 2.5, 3, 3.5, 2.5, 3, 3.5, 4\n", "\n", "When we average this, we get 2.5, equals to means of the population. So the mean of each sample is the mean of the population. This will taking an advantage of sampling, because it's often the case where population is too large and we're short on computing because the data is too big.\n", "\n", "If we copy this to web http://www.wolframalpha.com/:\n", "\n", "We get a histogram plot for this data. The data, a sample means, is actually a distribution itself which often called **Sampling Distributions**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/3w3.jpg)\n", "\n", "\n", "*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/61) 02:57*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's take another look. Earlier we're take inifinite number of sample possibilities. But that's just extreme condition. The everytime we take sample from the population we have sample distributions. And for example we're taking sample statistic, in this case, the mean. Plotting mean into the distribution would be called **sampling distribution**. So there's different between sample distributions and sampling distribution." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/3w4.jpg)\n", "\n", "\n", "*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/61) 04:07*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But we're missing some important point. What do we need to compare mean in a single sample with other sample's mean in the distribution? We don't know whether the mean of the sample that we took will be located in which location of Sampling Distributions. In this case, we need **Standard Deviation**.\n", "\n", "Note that because we're using sample means as our data, doesn't mean we need to use Bessel's Correction(n-1). We already have all the possible outcomes, and that's the entire data of our population(means sample).What we're calculating is not actually standard deviation, the standard deviation of all possible sample means is called **Standard Error**.\n", "\n", "In the case above we're taking 1000 sample for each of the state. This problem arise to know the population height of all women. Suppose we know the sample size, the mean, and all height of women population in the US. Then we do that sampling distribution, collecting mean for each of sample state.The mean of sample distribution should approximately equal to the mean of population.The variability should be less than variability of the population, since the population outliers are outliers combining for all states." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w70.jpg)\n", "\n", "*Screenshot taken from [Udacity](https://www.udacity.com/course/viewer#!/c-ud827/l-1455178654/e-117708709/m-117640039) video, 00:23*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There's actually a relationship between standard deviation of population and standard error,\n", "\n", "$$SE = \\frac{\\sigma}{\\sqrt{n}}$$\n", "\n", "* $\\sigma$ = population standard deviation\n", "* SE = Standard Deviation of distribution sample means\n", "* n = sample size" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This solve the original problem, which require us to find where the sample lie on distribution of sample means.This called **Central Limit Theorem**,that if you keep fetching fix sample size(n) all possible sample outcome, you're getting normal distribution that has standard deviation that equals **Standard Error**. Turns out the Standard Error is not easy to get, because that's mean we have to have all population data. And for that there's mathematical formula which stated above." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w71.jpg)\n", "\n", "*Screenshot taken from [Udacity](https://www.udacity.com/course/viewer#!/c-ud827/l-1455178654/e-117708715/m-117640058) video, 00:23*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that as we grow the sample size, the sampling distribution will get skinnier. This can be proved by mathematical formula earlier, sample size bigger will make bigger value in the denominator, that makes smaller standard error, hence skinnier sampling distribution.\n", "\n", "Intuitively, as the sample size bigger, the variety about where the mean of entire population get smaller, we get better prediction. To achieve half the mean error of the sampling distribution, we have to quarduple the sample size, 4xn, which we can replace it to the formula earlier to prove it.To get the error 1/3 from before we need:\n", "\n", "$$\\frac{1}{3} . \\frac{\\sigma}{\\sqrt{n}} = \\frac{\\sigma}{\\sqrt{9n}}$$\n", "\n", "The important thing to take away, is that when you're sampling to any distribution and distributed all the possible means, you will get aprroximately a normal distribution. As you're sample size bigger, the error will get smaller and converge to actual mean. This can get very intuitive. In the extreme condition, when your sample size match the size of the population, you will get the exact mean of the population.Keep in mind, that if you're averaging all possible means no matter what sample size, you're should get approximately the mean of the population." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/3w5.jpg)\n", "\n", "\n", "*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/65) 12:55*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is more official definition of CLT. Here we saw that the standard deviation is being corrected. Earlier I told you that it's not often the case that standard deviation population can be defined. So instead we can use standard deviation of our sample. In this case, we're using Bessel's correction. The observation must be independent, random sample if observational, or random assignment ifon experiment. To get better intuition for CLT, there's an applet for that http:/bit.ly/clt_mean\n", "\n", "\n", "The sample size doesn't to be large enough, less than 10 percent if without replacement. We also often don't know the shape of population distribution. If you do sample and turns out the shape is normal, it's more likely that the population which you take sample on is also normal. But to make sure for the distribution we set the minimum of sample size 30.But if you have skew population, high skew for example, then you would need larger sample size to make the shape normal so CLT can works" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Why 10% if without replacement? Because too large of sample size would make a larger probability that we grab sample that are dependent one another. For case above, if we're taking 50% sample from women population, chances are there's sibling or related family, and perhaps make similar height because of the genetic, and thus make it dependant. This will disrupt our sample which we want to be perfectly randomised. So not too large, below 10%, but also not too small, above 30." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/3w6.jpg)\n", "\n", "\n", "*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/65) 17:28*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So there's additional reason why the sample size has to be large enough. Consider the population distribution that has right skewed in the case above. When're take sample means and make sampling distribution, it will be like the parent, right skewed. But if take the sample large enough, it will approximate normal distribution. Why we so obsessed about normal shape? Because we need that for Central Limit Theorem to work. CLT will allow many statistical inference to use, including null hypothesis and hypothesis testing to use, which require normal probability distribution. There's also confidence interval that we can use." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Doing z-score that we discussed earlier, we can pretty much calculate the probability in certain distribution.Lower probability would means that certain sampling distribution doesn't affected the whole population.The result of lower probability then, means lower error, and thus *not likely to happen by chance.*\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's take an example. There's app Bieber Twitter that may affected Klout Score user. We have the mean, the standard deviation, and number of people, and average score of who uses the Bieber Twitter. From this examples we can extract some information related to Central Limit Theorem.\n", "\n", "* Sampling : Bieber Twitter users\n", "* population : Klout Score users\n", "* mean population : 37.72\n", "* sd population 16.04\n", "* avg Klout Score of users that use Bieber Twitter = 40\n", "\n", "Suppose that there are 35 Bieber Twitter user.What's the probability that using this app will affected the Klout Score users?" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mu = 37.72\n", "sigma = 16.04\n", "sample_size = 35" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.7112548491690811" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "SE = sigma/np.sqrt(sample_size)\n", "SE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is our standard error.Average score of Klout population is 37.72. The average score Klout users who use Bieber Twitter is 40. Given this, what's the probability of Bieber Twitter affected Klout Score?" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.2001689])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R mu = 37.72\n", "%R sigma = 2.711\n", "%R pnorm(40, mean=mu,sd=sigma,lower.tail=F)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So doing pnorm tells us that 0.2 probability of using Bieber Tweeter affecting the Kloud Score. There's too much possible reason to mention, but what's important is that our sample is lacking.\n", "\n", "Suppose that we increase the sample size to 250,the standard error would be" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.014458673382016" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_size =250\n", "SE = sigma/np.sqrt(sample_size)\n", "SE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see that we have much smaller standard error from the previous one. Would this increase the probability? What's the Z-score of 40? (mean of sample size 250) Remember that the mean of sampling distribution is exactly the same of mean at entire population." ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.2475040726882511" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(40 - mu)/SE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That value means that it's more than 2 standard deviations away, which is very small probability. What's the probability? The probability of **randomly selecting a sample** with sample size 250 with **a mean greater than 110**?" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.01199074])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R mu = 37.72\n", "%R sigma = 1.01\n", "%R pnorm(40, mean=mu,sd=sigma,lower.tail=F)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now please keep in mind, that we plot the mean of this particular sample (of size 250) in the sample means distribution. The probability randomly selecting average at least 40(more than 40, because less will be more common) is very rare, 0.01 if plot it into sampling distributions .This is very unlikely that is **random**. Things can be due to chance if the probability is still 0.2 like earlier. This explain us that there's maybe some correlation between the app and the Kloud score.Based on this low probability, it's unlikely due to chance.You may not notice this yet, but what we actually do is **using CLT for Hypothesis Testing**.\n", "\n", "So you see, increasing our sample size making our standard error smaller, thus tend to make less error. From our sampling distribution, the mean took from larger sample has lower probability, too low and so rare that is thought not due to chance.The closer the Z-score of a sample to 0 in sampling distribution, the higher the probability that the sample is due to chance." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sampling distribution is also the gate to open *Statistical Inference*. Consider following case," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/3w1.jpg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/3w2.jpg)\n", "\n", "\n", "*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/61) 02:57*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This data take very expensive resources. Imagine that they have to collect entire data US population. For this, rather than taking *population parameters*, we're taking *sample statistics*. As earlier that we've learned so far, we're gonna take all kind of sample in the case, and try to make distribution out of it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This kind of sample will " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In summary, CLT let us know:\n", "\n", "* the predicted shape of the distribution (Normal Distribution)\n", "* Expected value of the mean of the distribution (Mean of population)\n", "* Standard Deviation/Standard Error (known from standard deviation and sampling size)\n", "* The bigger the sample size, the smaller standard error, resulted in bigger z-score for that sample mean\n", "* The bigger the z-core,makes skinner distribution, makes less the proportion of sample means greater than that sample mean.\n", "* To minimize the error exactly, we can calculate how much sample size do we need.\n", "* The skewer the shape of the population, the higher sample size that we would need.\n", "* Standard deviation of population is almost always unknown. We can use the sample standard deviation with bessel's correction.\n", "* Point estimates like mean is expected to be equal to mean of the population\n", "* Standard error measures the error variability when we plot sample means into sampling distribution. This to measure the probability of error of our sample. If the probability is low, then it's likely not error and not happend due to chance.This is different than the definition of standard deviation, that measures variability of our data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### CLT example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/3w7.jpg)\n", "\n", "\n", "*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/67) 03:31*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This one of the example that later we're going to calculate by CLT. Eyeballing educated guess this problem, we see that by the height of the bar of histogram, we can guess the frequency. Using simple probability calculation, we get approximately 0.17. Using R" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load_ext rmagic" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\n", "[0.170822]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R pnorm(5,mean=3.45,sd=1.63,lower.tail=F)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/3w8.jpg)\n", "\n", "\n", "*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/67) 07:41*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now for this question, we actually taking the average of total 100 songs in ipod that average last **at least** 3.6 mins.This is not the same as taking probability that each of them last at least 3.6 mins, total the length will be perhaps more than 360 mins.\n", "\n", "So we're going to calculate the probability of the skewed distribution. How? by transforming it to normal distribution using CLT. Given mean and standard population earlier, we can make sampling distribution, and calculate the Z-score. Using that, we calculate the probability and have 0.179 using upper tail.Thats the probability that if you randomly select 100 songs, it will last for 60 mins." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/3w9.jpg)\n", "\n", "\n", "*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/67) 10:51*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next we're going to match the plot from the probability. Distribution that transform to normal the most is the distribution of sample means that have larger size, in this case right most is 3. The distribution of sample observation, sample distribution but not sampling distribution is in the middle is 2. It also because it resemble right skewed and variability of the original population. The rest is the sampling distribution that have fewer size, left at 1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Binomial Distribution" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w60.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/55) video, 01:15*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For explaining more about Binomail, here we will take Milgram Experiment. In this experiment, the Teacher is the subject, which would tell whether the teacher wants to do thing against his conscious. The Experiment will order the teachers to shock the Learner, which actually just sound recorded each time the teacher send a shock. The teacher has 'blinding' about the learner and found that consistently overtime probability of teacher giving shock is 0.65" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each subject of Milgram's experiment is considered as **trial**.And she/he labeled **success** if he administer a 'shock' and **failure** otherwise. In this case the **probability of sucess**, p = 0.35\n", "\n", "When a trial only has two possible outcomes, we call this **Bernoulli random variable**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w61.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/55) video, 05:29*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So when doing the experiment on 4 people, we have possible outcomes of 2^4. But only regarding scenario where exactly 1 person that we concern. This turns out to be 4 scenario that each of the person try.\n", "\n", "With all of the scenario simulated, we see that exactly all the probability is the same. Since all the person is **independent**, we can multiply them directly. Since this is **disjoint events** of 4 scenario, we can quickly do:\n", "\n", " res = P(scenario) * n_scenario\n", " \n", "This eventually make a perfect setting for **Binomial Distribution**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w62.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/55) video, 07:12*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So doing earlier steps by hand would be quiet tedious, as we have to jot down all possible conditions. What we can do is using binomial distribution for our problem. \n", "\n", "This is the main formula for Binomial Distribution, and as long we have three of the required variables, we can do the calculation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w63.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/55) video, 09:23*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the first K '4 choose 1' can simply be calculated doing the formula above. For the second one, as we can see at the first three simulated, things can get complicated easily, so we're doing '9 choose 2'.\n", "\n", "This can get easily computed in R" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 36.])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R choose(9,2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w64.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/55) video, 07:12. Only last one is false*" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 5.])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R choose(5,4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So to put it concretely," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w65.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/55) video, 09:53*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The conditions for binomial are:\n", "\n", "1. The trials must be independent\n", "2. The number of trials, n, must be fixed\n", "3. each trial outcome must be classified as a success or a failure.\n", "4. The probability of sucess,p,must be the same for each trial(which is to be expected, agree with number 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w66.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/55) video, 13:43*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pay attention that **random sample** means we know that the employees, the trials, are **independent**. And the reason that we're getting very small probability because the power magnitude, we would expect that find probability of success from 10 people is so much lower than the probability of 8 people.\n", "\n", "Alternative way of thinking is that, prob sucess is really low, finding 8 in 10 people that succeed, is magnitudely low. Things could go different if we only find 2 success in 10 people, which goes to probability 0.2~0.3.\n", "\n", "To do this in R," ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 2.77841962e-06])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#k,n,p\n", "%R dbinom(8,size=10,p=0.13)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w67.jpg)\n", "\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/55) video, 17:13*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So doing some simple probabilistic given n samples and probability, we can guess the exact value. And guess the exact value is the **expected value** in binomial. Standard deviation can also be counted as the following, which in this case give 3.36. So give or take 68% of 100 employees falls in plus minus 3.36. Say, why binomial? Is it the shape because there's only two possible conditions?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w80.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/57) video, 02:14*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The binomial will looks closer to the normal distribution as the number of trials increases. This is get by the probability of success, p = 0.25. In the top left distribution, the complement probability, 0.75 is raise to the power of 10(because success case =0). This will give right skewed distribution to be more normal." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w81.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/57) video, 03:51*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Study found that we actually get more than what we give. The power users is the users in facebook who give more than they get. So we're actually calculating the probability of success, of finding the power users.P(K>= 70)?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w82.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/57) video, 07:40*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As larger n shape more to normal distribution. We can use the benefit of normal distribution. With binomial, the probability of exact value is the only must condition. So more than probability of success, should be calculated independently like shown in the sample until the number of trials n.\n", "\n", "We can calculate this using normal distribution and its properties. Like the formula given earlier, mean can be get from the directly product the number trials and probability of success. Standard deviation from square root of number of trials times probability of success times probability of failure. We can get Z-value and using it to predict the probability.Remember that we want to get the probability of at least 70, so we have to take the complementary probability, since z probability only compute below Z." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In R, this can get by" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\n", "[0.098428]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R pnorm(70,mean=61.25,sd=6.78,lower.tail =F)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This also can be done using another command of R" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w83.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/57) video, 09:53*" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\n", "[0.112763]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R sum(dbinom(70:245, size = 245, p=0.25))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So it turns out to be so different than earlier calculations. This can happen in two ways. First it's because the binomial doesn't exactly shape like normal-density function, you can see the bar chart. Second, the 70 of the value is actually cut off and out of the distribution. We can reduce the k by 0.5, and recalculating using R:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\n", "[0.111837]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R pnorm(69.5,mean=61.25,sd=6.78,lower.tail =F)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can also be done by applet : http://bit.ly/dist_calc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So we know that by taking certain condition that binomial shape like normal distribution, we can take advantage of normal distribution. But if we can't see the distribution? What is the requirement?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w84.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/57) video, 12:07*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We also have 0.5 point adjustment for k. But what's important here is the sample size, how you're going to be big enough that will somehow shape into normal distribution. Later in statistical inference, there's only two oucome in categorical variable, so it's shape binomial. We can predict the variable as long as binomial shape into normal.\n", "\n", "For the formula above we know that we need at least 10 succeses and 10 failures to shape into normal distributions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w85.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/57) video, 14:04*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Binomal Examples\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w86.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/59) video, 02:58*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can be calculated using R" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\n", "[0.242749]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R dbinom(6,size=10,p=0.56)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This will get probability that closely approach to the normal distribution. What we expect is actually is(expected number of successes,k):\n", "\n", " Expected Value = mean = nxp = 5.6\n", " \n", "We expect 5.6 from our calculation, but the histogram prove that 5-6, and 5.6 is not too far behind." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\n", "[0.019825]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R dbinom(2,size=10,p=0.56)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using it in n = 1000 and k = 600, because the Law of the Larger Numbers, makes the probability will be conceptually smaller." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\n", "[0.000977]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R dbinom(600,size=1000,p=0.56)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w87.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/59) video, 06:29*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To describe normal, we need to be exactly sure of the minimum required from the binomial case. When shape like normal, we also need to know mean and standard deviation to plot." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/2w88.jpg)\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/59) video, 09:59*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can be calculated by applet or dbinom R command. Using z-score, the k value once again has to be decrement by 0.5 because no exact value is 60." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\n", "[0.240204]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R pnorm (60-0.5,mean=56,sd=4.96,lower.tail=F)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\n", "[0.241106]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R sum(dbinom(60:100,size=100,p=0.56))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> **REFERENCES:**\n", "\n", "> * https://class.coursera.org/statistics-003/lecture\n", "> * https://www.udacity.com/course/viewer#!/c-ud827" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }