{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "In this blog we're going to discuss about frequentist approach that use p-value, vs bayesian approach that use posterior." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/9w1.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/175) 01:04*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The study will help us make a comparison of frequentist vs bayesian approach. We have a population, and your task is to test whether the yellow is whether 10% or not." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/9w3.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/175) 01:52*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then from the study, you make a **decision table**. If your decision is right, you're going to get a bonus, otherwise you lose a job." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/9w5.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/175) 02:26*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then you're presented the money and the cost to gather the data. Remember, often it's pretty costly to get more data. So this example representing that condition." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using **frequentist approach**, you're going to use **hypothesis testing**. To set the hypothesis:\n", "\n", "* H0 : 10% yellow M&Ms\n", "* HA : 20% yellow M&Ms\n", "\n", "Using test statistic, because it's talking about the proportion, is the number of **yellow** observed in the sample. The p-value is calculating the probability of this many or more yellow M&M in the sample that you have buy, given the null hypothesis test. You might want to ask this, \"If I have bought 3 times and observe all the p-value, can I predict what is the p-value for the fourth time?\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So how many sample that you think you would buy? 5,10,15,20? Recall that if you fail to predict the p-value, you would lose your job. But if you buy large sample size, it will be very costly. The decision is how to get the right sample size, that's enough to make it practically significant." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's choose 5 for this state. If some of you have known bootstrapping, this is one of the most important technique to engineer a new sample. In hypothesis testing, you're collecting the data, build test statistic, p-value, and compare it to significance level. The next question then become, what is significance level for this problem? Recall that significance level is all about **type 1 error, rejecting the null hypothesis when the null hypothesis is true.**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So which level should you choose? Using higher significance level, you can have a type 2 error rate. But using smaller alpha, can get you miss any true significant p-value. For this case, we stick with 5% significance level." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With sample size this small, we can use **binomial distribution**.Because we set 10% as our null hypothesis, we set the probability of success 10%. Recall that null hypothesis is a null value for true population,hence the proportion is equal the probability of success. Suppose we have yellow (among 4 other colors) once.\n", "\n", " p-value = P(1 or more yellows | n=5, p = 10%)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since we're observing the probability of at least 1, and there are only two chances, whether it's yellow or not, we can simplify this calculation as,\n", "\n", " P(k>=1) = 1 - P(none yellows)\n", " \n", "The complement probability is 0.9, then\n", "\n", " P(k>=1) = 1 - (0.9)^5 = 0.41\n", " \n", "The result is 0.41, our p-value is greater than the significance level, we failed to reject the null hypothesis." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since the sample size is indeed small, and because of that, we want to increase our sample size to 10. Then for the 10 draws, we get two yellows. The p-value is then\n", "\n", " p-value = P(2 or more yellows | n=10,p=10%)\n", " \n", "Since this is getting too complicated(we can calculate the probability of 2,3,4..10, or 1-(k=0+k=1)), we can use R." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.2639011" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(dbinom(2:10,10,0.1))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Again, based on this p-value, we fail to reject the null hypothesis." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How about the 15 sample size? Again when doing 15 draws, we get 3 yellow. So,\n", "\n", " p-value = P(3 or more yellows | n=15, p = 10%)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using R," ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.1840611" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(dbinom(3:15,15,0.1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again we failed to reject the null hypothesis.\n", "\n", "For the sake of the argument, we use 20 sample size, and have 2 yellows in 20 draw. Again we set our binomial,\n", "\n", " p-value = P(4 or more yellows | n=20,p=10%)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.1329533" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(dbinom(4:20,20,0.1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And then, once again, we failed to reject the null hypothesis." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/9w6.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/175) 14:33*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we're looking at the possibilities earlier(1 out of 5, 2 out of 10, 3 out of 15, and 4 out of 20), we know that the proportion is actually 20%. Since we failed to reject our null hypothesis, we would lose our job.So you see, it's important to see from looking at the problem. " ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "![jpeg](../galleries/coursera-statistics/9w7.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/175) 15:30*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's use bayessian approach. Again, only two conditional probabilities.You can either have 10% or 20%. Since we don't how is the true population, we make a fair judgement 50:50. In Bayes this is our **prior probability**. As you recall in Bayes, we can be presented with the data, calculate the posterior, make that as an input of next data, calculate the posterior and keep doing that.So the p-value in bayesian is the probality that given the observed data, what are th posterior probability." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/9w8.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/175) 17:16*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So we can calculate the probability of 10% given data. This is what Bayesian can solve. We can use Bayes to calculate like the one in the examples. Recall that in Bayes, we have\n", "\n", " P(A and B) = P(B and A), if A and B are dependent.\n", " P(A|B) * P(B) = P(B|A) * P(A)\n", " \n", "We subtitute the equation like the one in the example. Since there either 10% or 20%, the probability of 20% is the complement of 10% yellow." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.4096" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbinom(1,5,0.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/9w9.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/175) 20:18*" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.32805" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbinom(1,5,0.1)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.4096" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbinom(1,5,0.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once again, since we only have two conditions in our probability space, and we're observing the exact successes for n trial, we can use `dbinom` function. Recall that we have 1 yellow in our first trial. We calculate the Bayesian as observing the probability of 10% given the data. And by calculating the probability of data, what is the probability that we have 10% yellow **or** 20% yellow given the data. Since we have an **or** in probability, we join by addition. In dbinom we can calcute the probability of k success, given n trial, knowing the probability of success. We can use P(data | 10%) with `dbinom` in R. So we're calculating P(data|10%) is 0.33 and P(data|20%) is 0.41. We incorporate the formula, and have **0.44**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/9w10.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/175) 21:40*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You may want to mark your results in the table. Since 20% yellow is the complement probability of 10%, we take it 0.56 for the 20%. And we repeat our process for 10,15,20." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/9w11.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/175) 23:05*" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.1937102" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbinom(2,10,0.1)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.3019899" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbinom(2,10,0.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "for 10 value, we can get 0.39 for P(10%|data). And the complement for 20% is 0.61" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.339383" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p_data_10 = dbinom(3,15,0.1) * 0.5\n", "p_data_20 = dbinom(3,15,0.2) * 0.5\n", "\n", "p_data_10/(p_data_10+p_data_20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So we have 0.34 for P(10%|data) and for the complement of 20%, we have 0.66. That is our posterior probability of step 3." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.2915103" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p_data_10 = dbinom(4,20,0.1) * 0.5\n", "p_data_20 = dbinom(4,20,0.2) * 0.5\n", "\n", "p_data_10/(p_data_10+p_data_20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, for 20 M&M we have 0.29 for 10% and the complement 0.71 for 20%. Let's take a look at the overall table, all 4 steps of frequentist vs bayesian approach." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![jpeg](../galleries/coursera-statistics/9w12.jpg)\n", "\n", "\n", "*Screenshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/175) 27:31*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The frequentist approach, p-value makes HT failed to reject, and keep siding with 10%. On the other hand, Bayesian consistently prefer 20%. So there's two contradicting results on two approach. Which is right? Since you know that 20% is the true population, Bayesian is the winning side. Indeed sometimes two approach could yield slightly different result." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recall that in Bayesian, you'll always update prior according to your posterior. Earlier, we don't update our prior. It keeps at constant 0.5.How about we keep updating prior based on resulted posterior? You could also using Bayesian approach like this" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.4447231 0.5552769" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p_data_10 = dbinom(1,5,0.1) * 0.5\n", "p_data_20 = dbinom(1,5,0.2) * 0.5\n", "\n", "p_10 = p_data_10/(p_data_10+p_data_20)\n", "c(p_10,1-p_10)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.3351035 0.6648965" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p_data_10 = dbinom(2,10,0.1) * 0.44\n", "p_data_20 = dbinom(2,10,0.2) * 0.56\n", "\n", "p_10 = p_data_10/(p_data_10+p_data_20)\n", "c(p_10,1-p_10)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.2092687 0.7907313" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p_data_10 = dbinom(3,15,0.1) * 0.34\n", "p_data_20 = dbinom(3,15,0.2) * 0.66\n", "\n", "p_10 = p_data_10/(p_data_10+p_data_20)\n", "c(p_10,1-p_10)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] 0.09859043 0.90140957" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p_data_10 = dbinom(4,20,0.1) * 0.21\n", "p_data_20 = dbinom(4,20,0.2) * 0.79\n", "\n", "p_10 = p_data_10/(p_data_10+p_data_20)\n", "c(p_10,1-p_10)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "So there you go, using Bayesian approach, you get 0.1, which is near to 0.13 frequentist." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> **REFERENCES**:\n", "\n", "> Dr. Mine Çetinkaya-Rundel, [Cousera](https://class.coursera.org/statistics-003/lecture)" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.1.2" } }, "nbformat": 4, "nbformat_minor": 0 }