Confidence Interval and p-Value#

This document explains the relationship between p-value and confidence intervals. It goes on with the specific case of a binamial law. Assuming we want to determine whether or not two binomial laws are significantly different, how many observations we need to get the p-value under a given threshold.


The term p-value is very popular in the world of search engines. I usually prefer confidence interval 95%, I think it is easier to understand. Plus, because p-Value are real values, we could be tempted to compare them and it is usually wrong. On the other hand, it is more difficult to compare confidence intervals, especially if they are related to complete different variables. Their nature prevents you from doing that. Howerver p-Values and confidence interval are similar: they tell you whether or not a metric difference is significant.

Usually, it starts from a set of identically distributed random variables (X_i)_{1 \infegal i \infegal N}. We then estimate the average \widehat{\theta}_N = \frac{1}{N} \sum_{i=1}^{N} X_i and we ask the question is \widehat{\theta}_N null? In others terms, we want to know if the average is significantly different from zero. If the random variable X follows a random law which has a standard deviation, we can use the central limit theorem which tells us:

\sqrt{N} \widehat{\theta}_N \underset{N \rightarrow \infty}{\longrightarrow}   \loinormale{0}{\sigma}

Not all of them have a standard deviation. For example, if X follows a Cauchy law, \esp{X^2} \sim \int \frac{x^2}{1+x^2}dx which does not exist. This remark also concerns every distribution known as heavy tail distribution.

If Y \sim \loinormale{0}{\sigma}, then we have \pr{\abs{Y} \infegal 1.96} = 0.95. That is why we can say:

\widehat{\theta}_N \text{ is not null with 95\% confidence if }
\sqrt{N} \frac{|\widehat{\theta}_N|}{\sigma} > 1.96

And the confidence intervalle at 95% would be:

\cro{ %\widehat{\theta}_N
            - \frac{1.96 \sigma}{\sqrt{N}},
            %\widehat{\theta}_N +
            \frac{1.96 \sigma}{\sqrt{N}}}

When \esp{ \widehat{\theta}_N } = \theta_0 \neq 0, it becomes:

\sqrt{N} \cro{ \widehat{\theta}_N - \theta_0}
\underset{N \rightarrow \infty}{\longrightarrow}

We usually want to check if the mean is equal to a specific value using a statistical test:

H0: && \widehat{\theta}_N = \theta_0 \\
H1: && \widehat{\theta}_N \neq  \theta_0

We validate H0 if:

\widehat{\theta}_N \in \cro{ \theta_0 - \frac{1.96 \sigma}{\sqrt{N}},
\theta_0 + \frac{1.96 \sigma}{\sqrt{N}}}


With confidence intervals, you first choose a confidence level and then you get an interval. You then check if your value is inside or outside your interval. Inside, the gain is not significant, outside, it is.

With a p-value, we consider the problem the other way, given \widehat{\theta}_N, what is the probability that the difference \abs{\widehat{\theta}_N - \theta_0} is significant? Let’s consider Y following a normal law \loinormale{0}{1}. We are looking for:

\pr{ \abs{Y} >  \sqrt{N} \frac{|\widehat{\theta}_N|}{\sigma} }  = \alpha

\alpha is the p-value.

\alpha &=& 1-\int_{-\beta_N}^{\beta_N} \frac{1}{\sqrt{2\pi}}
e^{\frac{-x^2}{2}} dx =
2 \int_{\beta_N}^{\infty} \frac{1}{\sqrt{2\pi}} e^{\frac{-x^2}{2}} dx \\
\text{where } \beta_N &=& \sqrt{N} \frac{|\widehat{\theta}_N|}{\sigma}

At this point, we should not forget that we use a theorem which tells us that \sqrt{N} \frac{|\widehat{\theta}_N|}{\sigma} \sim \loinormale{0}{1} when N \rightarrow \infty, which means everything we said is true when N is great enough.

Significant difference between samples mean#

Usually, we do not want to test if an average is null but if the difference between two averages is null. We consider two random samples having the same size, each of them described by (X_i) and (Y_i). All variables are independant. (X_i) are distributed according the same law, we assume the same for (Y_i). We expect the following difference to be null.

\widehat{\eta}_N &=& \frac{1}{N} \sum_{i=1}^{N} X_i -
\frac{1}{N} \sum_{i=1}^{N} Y_i
= \frac{1}{N} \cro{ \sum_{i=1}^{N} X_i -  Y_i }

Considering expression (2), we can applying the central limit theorem on variable Z=X-Y, we get (\eta_0=0):

\sqrt{N} \widehat{\eta}_N \underset{N \rightarrow \infty}{\longrightarrow}
\loinormale{\eta_0}{\sqrt{ \frac{\var{Z} }{N  } }}

If both samples do not have the same number of observations, this expression becomes:

\sqrt{N} \widehat{\eta}_N \underset{ \begin{subarray}{c} N_1
\rightarrow \infty \\
N_2 \rightarrow \infty \\
\frac{N_1}{N_2} \rightarrow x \end{subarray}}{\longrightarrow}
\loinormale{\eta_0}{\sqrt{ \frac{\var{X}}{N_1}+\frac{\var{Y}}{N_2} }}

Application on binomial variables#

A binomial variable X \sim \loibinomialea{p} is defined by:

\pr{X=0} &=& 1-p \\
\pr{X=1} &=& p

Let’s consider two series of observations (X_i) \sim \loibinomialea{p} and (Y_i) \sim \loibinomialea{q}. We assume p \neq q and we want to determine how many observations we need to get a p-value below 5%. We know that \variance{X_i}=p(1-p) and \variance{Y_i}=q(1-q). Next table shows the values. First column contains values for p, first row contains values for q-p. We also assume we have the same number N of random observations for each variable. The statistical test cen be defined like following:

H0: && p = q = p_0 \\
H1: && p \neq q

If H0 is true, then:

\sqrt{N} \widehat{\theta}_N \underset{N \rightarrow \infty}{\longrightarrow}   \loinormale{0}{\sqrt{p_0(1-p_0)}
\sqrt{ \frac{1}{N_1}+\frac{1}{N_2} }}


Given a binomial law with parameter p and a difference d, this table gives the number of observations needed on both sides to get a significant difference assuming p is the expected pourcentage

Estimate a p-value by using the distribution function#

Expression (1) gives a way to estimate the p-value. Computing the integral is not always possible but there is a way to do it using Monte Carlo method. Let’s assume X \sim \loinormale{0}{1}. We denote f_X as the density function of X. We also consider an interval I=\cro{-a,a}. Then we have f(a)=f(-a) and:

\pr{X \in I} = \pr{ \abs{X} \infegal a } = \pr{ f(X) \supegal f(a)}

This is true because f is decreasing for x>0. The p-value \alpha for a estimator \beta using Monte Carlo method is:

\frac{1}{N}\sum_{i=1}^{N} \indicatrice{ f(X_i) < f(\beta)} \longrightarrow \alpha

Assuming every (X_i)_i follows a normal law \loinormale{0}{1}.

Correlated variables#

Let’s assume we now have a vector a correlated variables X=(X_1,...X_d) drawn following a law \loinormale{\theta_0}{\Sigma}.

The central limit theorem is still valid:

\sqrt{N} \widehat{\theta}_N \underset{N \rightarrow \infty}{\longrightarrow}   \loinormale{\theta_0}{\Sigma}

We know estimators for the average and the covariance matrix defined as follows:

\widehat{\theta_N} &=& \frac{1}{n} \sum_{i=1}^{N} X_i \\
\widehat{\Sigma_N} &=& \frac{1}{n} \sum_{i=1}^{N} (X_i - \widehat{\theta_N})(X_i - \widehat{\theta_N})'

We usually want to check if:

H0: && \widehat{\theta}_N = \theta_0 \\
H1: && \widehat{\theta}_N \neq  \theta_0

If \Lambda is diagonal matrix of \Sigma (diagonal matrix with eigen values). All eigen values are real and positive, we then define:

\Sigma &=& P \Lambda P' \text { and } \Sigma^{\frac{1}{2}} =  P \Lambda^{\frac{1}{2}} P'

We consider Z_i = (X_i - \widehat{\theta_N}) \Sigma^{-\frac{1}{2}}. We then have \esp{Z_i} = 0 and \var{Z_i} = I_2 where I_2 is the identity matrix. We could now consider each dimension of Z_i independently as illustrated in next figure: it shows the difference on an example if we consider the correlation of two variables correlated such as \Sigma=\pa{\begin{array}{cc} 0.1 & 0.05 \\ 0.05 &  0.2 \end{array}}.


We assume we observe two Bernouilli variables correlated. Red points represents the area for which we would accept hypothesis H0 in case both variables are independant. Blue area represents the same but with the correlation. But that would not be the best way to do it. The confidence interval for a couple of indenpendant gaussian (N_1,N_2) variables is an ellipse. Two independent normal variables N_1^2+N_2^2 with a null mean and standard deviation equal to one follows a \chi_2 law. Based on that, we can deduce a boundary for the confidence zone at 95%. Next figure shows this zone for a non-correlated couple and a correlated couple (\Sigma=\pa{\begin{array}{cc} 0.1 & 0.05 \\ 0.05 &  0.2 \end{array}}).


We assume we observe two Bernouilli variables correlated. Red points represents the area for which we would accept hypothesis H0 in case both variables are independant. Blue area represents the same but with the correlation.

Multiple comparisons problem#

The problem of Multiple comparisons happens when dealing with many metrics measyring a change. That’s allways the case when two version of the sam websire are compared in a test A/B. The metrics are correlated but it is unlikely that all metrics differences will be significant or not. The Holm–Bonferroni method proposes a way to define an hypthesis on the top of the existing ones.

Algorithm Expectation-Maximization#

We here assume there are two populations mixed defined by random variable C. Let’s X be a mixture of two binomial laws of parameters p and q. It is for example the case for a series draws coming from two different coins.

\pr{X} &=&  \pr{ X | C = a} \pr{C=a} + \pr{X | X =b} \pr{C = b}

The likelihood of a random sample (X_1,...X_n), the class we do not observe are (C_1,...C_n):

L(\theta) = \prod_i \cro{ p^{X_i}(1-p)^{(1-X_i)} \pi }^{1-C_i} \cro{q^{X_i}(1-q)^{(1-X_i)}  (1-\pi)  }^{C_i}

The parameters are \theta=(\pi,p,q). We use an algorithm Expectation-Maximization (EM) to determine the parameters. We define at iteration t:

w_i &=& \espf{C_i | X_i, \theta_t }{X_i}  \\
    &=& \frac{ p_t^{X_i} (1-p_t)^{1-X_i} \pi_t }
               { p_t^{X_i}(1-p_t)^{1-X_i} \pi_t + q_t^{X_i} (1-q_t)^{1-X_i} (1-\pi_t) }

We then update the parameters:

\widehat{\pi} &=&  \frac{1}{n} \sum_{i = 1}^n w_i \\
\widehat{p}         &=&  \frac{  \sum_{i = 1}^n w_i X_i }{  \sum_{i = 1}^n w_i} \\
\widehat{q}         &=&  \frac{  \sum_{i = 1}^n (1-w_i) X_i }{  \sum_{i = 1}^n (1-w_i)}

See also Applying the EM Algorithm: Binomial Mixtures.


The following notebook produces the figures displayed in this document.