This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.
Many frequentist methods for hypothesis testing roughly involve the following steps:
Here, we flip a coin $n$ times and we observe $h$ heads. We want to know whether the coin is fair (null hypothesis). This example is extremely simple yet quite good for pedagogical purposes. Besides, it is the basis of many more complex methods.
We denote by $\mathcal B(q)$ the Bernoulli distribution with unknown parameter $q$ (http://en.wikipedia.org/wiki/Bernoulli_distribution). A Bernoulli variable:
import numpy as np
import scipy.stats as st
import scipy.special as sp
n = 100 # number of coin flips
h = 61 # number of heads
q = .5 # null-hypothesis of fair coin
xbar
is the estimated average of the distribution). We will explain this formula in the next section How it works...xbar = float(h)/n
z = (xbar - q) * np.sqrt(n / (q*(1-q))); z
pval = 2 * (1 - st.norm.cdf(z)); pval
You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).
IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).