---
# System prepended metadata

title: ICEF Elements of Statistic 2

---

---
title: ICEF Elements of Statistic 2
---

# Seminar 17-18 Goodness of Fit (Pearson's) test

Inputs : 
    - observed category sizes : $O_1, \dots, O_k$
    - expected (according to $H_0$) category sizes:  $E_1, \dots , E_k$

**Note:** if $E_i$ is too small (e.g. $\leq 3$) we have to lump it into another category, reducing the number of categories.

Test statistic :

$$\text{Pearson test statistic} = \sum_{i=1}^{k} \frac{(O_i-E_i)^2}{E_i} \sim \chi^2(k - s - 1),$$

where 
- $k$ is the number of categories, 
- $s$ is the number of parameters in the expected distribution, *that we had to estimate from the data*.


## Problem 4 (grouping)

Categories : $\{0, 1, 2, 3, 7 , 8\}$


$O_0 = 32, O_1  = 12, O_2 = 3, O_3 =1, O_7 = 1, O_8 =1$

Total number of observations $N = O_1 + \dots + O_8 = 32 + 12 + 3 + 1 + 0 + 0+ 0 + 1 + 1=50$

Theoretical categories are dictated by the sample space of the Poisson distribution.

if $X\sim Poisson(\lambda)$, $X \in \{0, 1, 2, 3, \dots \}$

$P(X = x) = \frac{\lambda^x}{x!} \exp(-\lambda),$

(PMassF, discrete variable analogue of PDF) where $\lambda$ is the mean of random variable $X$.

Set of categories registered after our research is not the same as the sample space (range of possible values) of hypothesized distribution:

$$\{0,1,2,3,7,8\} \quad \text{vs} \quad \{0, 1,2,3,4,5,6,7,8,9, 10\dots\}$$

To make them the same we *change category labels*:

- we add categories $\{4,5,6\}$ in the observed set with $O_4=O_5=O_6 = 0$, we get

$$\{0,1,2,3,4,5,6,7,8\} \quad \text{vs} \quad \{0, 1,2,3,4,5,6,7,8,9, 10\dots\}$$

- to handle the tail of the theoretical distribution $\{8, 9, 10, \dots\}$, we replace the last category $8$ with category "$\geq 8$".

$$\{0,1,2,3,4,5,6,7,\geq 8\} \quad \text{vs} \quad \{0, 1,2,3,4,5,6,7,\geq 8\}$$

Accordingly $O_8$ must be relabeled $O_{\geq 8}$.

$$E_i = N \times P(X = i), \text{ for $i =0, 1,\dots 7$}\\E_{\geq 8} = N\times P(X \geq 8)$$

From the statement of the problem $\lambda = 0.7$, and $P(X = x) = \frac{\lambda^x}{x!} \exp(-\lambda)$. Then 

- $E_0 = 50 \cdot \frac{0.7^0}{0!}\cdot\exp(-0.7)=50 \cdot \exp(-0.7) =24.8$ 
- $E_1 = 50 \cdot \frac{0.7^1}{1!}\cdot\exp(-0.7)=50 \cdot 0.7\cdot \exp(-0.7) =17.4$
- $E_2 = 50 \cdot \frac{0.7^2}{2!}\cdot\exp(-0.7)=6.1$
- $E_3 = 50 \cdot \frac{0.7^3}{3!}\cdot\exp(-0.7)=50 \cdot 0.7\cdot \exp(-0.7) =1.4$
- $E_4 = 50 \cdot \frac{0.7^4}{4!}\cdot\exp(-0.7)=50 \cdot 0.7\cdot \exp(-0.7) =0.2$
- ...

We observe that the theoretical sizes of categories $3,4,5,\dots,\geq 8$ are too small, smaller than $2$. For the test to be valid we should lump them together.

![](https://i.imgur.com/ml32hNb.png)

We make another grouping:

- replace categories $\{3,4,5,6,7,\geq 8\}$ with a single category $\geq 3$:


$$E_{\geq 3} = N \cdot P(X \geq 3) = N \cdot (1 - P(X <3))\\
=N\cdot (1 - P(X=0) - P(X=1) - P(X=2))\\
=N - N\cdot P(X=0) - N\cdot P(X=1) - N\cdot P(X=2)\\
=N - E_0-E_1-E_2 \\
=50 - 24.8 - 17.4 - 6.1 = 1.7 
$$
The last category $\geq 3$ still turns out to be far too small, so we group it together with $2$ to get category $\geq 2$.

$$E_{\geq 2} = E_2 + E_{\geq 3} = 6.1 + 1.7 = 7.8$$

Finally we get 3 categories $\{0,1,\geq 2\}$ with frequencies (category sizes):

- theoretical : $E_0 = 24.8, E_1 = 17.4, E_{\geq 2} = 7.8$
- observed : $O_0 =32,O_1=12, O_{\geq 2} =6$.

The value of Pearson test statistic is $\frac{(32 - 24.8)^2}{24.8} + \frac{(12 - 17.4)^2}{17.4} + \frac{(6 - 7.8)^2}{7.8} =4.18$

We reject the null hypothesis of the data being distributed as Poisson(0.7) if 4.18 is larger than the 95% quantile of the $\chi^2(3-1)$. The quantile (critical value) is 5.99, above the observed value of the test statistic therefore we do not reject $H_0$.

![](https://i.imgur.com/SKSCKm5.png)



## Problem 5 GoF (conditioning and grouping)

$$H_0 : \text{Basketball shots are independent Bernoulli trials with probability of success $p = .6$}$$

We start with determining the thoretical categories (sample space) and their sizes $E_i$.

$O_3 = 80, O_4 = 50, \dots O_{15} = 1$.

The number of observations is $N=200$.

$X$ - length of a series of hits (successful shots). Under the $H_0$ it must be distributed as a geometric random variable.

$$P(X = x) = p^x (1-p)$$


$P(X = 2) = P(hit \cap hit \cap miss) = 0.6 \cdot 0.6 \cdot (1 - 0.6)$

As the researcher dropped the observations with $X\leq 2$, we have to use the conditional distribution:

> $$P(A \mid B) =\frac{P(A \cap B)}{P(B)}$$

$$E_i = N \times P(\underbrace{X = i}_{A} \mid \underbrace{X > 2}_{B}) = \begin{cases}
0, &\quad \text{if $x \leq 2$}\\
N \times \frac{P(X = x)}{P(X < 2)}, &\quad \text{if $x \geq 3$}  
\end{cases}$$

> $$E_1 = N \times P(\underbrace{X = 1}_{A} \mid \underbrace{X > 2}_{B}) = N \times \frac{P(X=1 \cap X > 2)}{P(X > 2)} = N \cdot \frac{0}{P(X > 2)} = 0$$


$$P(X > 2) = 1 - P(X =0) - P(X = 1) - P(X = 2) \\
= 1 -  0.4 - 0.6\cdot 0.4 - 0.6^2 \cdot 0.4 = 1 - (1 + 0.6 + 0.36)\cdot .4 = 0.216$$

\begin{align}
E_3 &= N \times P(\underbrace{X = 3}_{A} \mid \underbrace{X > 2}_{B}) = N \times \frac{P(X = 3 \cap X > 2)}{P(X > 2)} \\
&= N \cdot \frac{P(X=3)}{P(X > 2)} \\
&= 200\cdot \frac{.6^3 \cdot .4}{0.216}  = 80\\
E_4 &= N \times P(\underbrace{X = 4}_{A} \mid \underbrace{X > 2}_{B}) = N \times \frac{P(X = 4 \text{ AND } X > 2)}{P(X > 2)} \\
&= N \cdot \frac{P(X = 4)}{P(X > 2)} \\
&= 200 \cdot \frac{.6^4\cdot .4}{.216} = 48\\
E_5 &= 200\cdot \frac{.6^5 \cdot .4}{.216} = 28.8\\
E_6 &= 200\cdot \frac{.6^6 \cdot .4}{.216} = 17.28\\
E_7 &= 200\cdot \frac{.6^7 \cdot .4}{.216} = 10.4\\
E_8 &= 200\cdot \frac{.6^8 \cdot .4}{.216} = 6.22\\
E_{\geq 9} &= N \times P(X \geq 9 \mid X > 2) =  N\frac{P(X \geq 9)}{P(X > 2)} \\
&= N - \sum_{i \in \{3,4,5,6,7,8\}} E_i = 9.3
\end{align}

We need to group the existing categories to obtain classes $\{3,4,5,6,7,8,\geq 9\}$.

The observed frequencies for grouped classes are $O_3 = 80, O_4 = 50, \dots , O_{\geq 9} = 8$

The test statistic is

$$\sum_{i \in \{3,4,5,6,7,8,\geq 9\}}\frac{(O_i - E_i)^2}{E_i} =\\
= (48 - 50)^2 / 48 + (28.8 - 32)^2 / 28.8 + (16 - 17.28)^2 / 17.28 \\+ (8 - 10.4)^2 / 10.4 + (6.22 - 6)^2 / 6.22 + (8 - 9.3)^2 / 9.3 \\
= 1.27$$

The test statistic is distributed as $\chi^2(7 - 1)$, the 5% critical value is 12.59, much larger than the observed test statistic value, 1.27, therefore we do not reject $H_0$.


## Problem 6

$$E_{\leq 30} = N \cdot \Phi(\frac{30 - \mu}{\sigma})$$

## Problem 7

$$E_i = N \cdot P(X = i) = (1 - \hat p)^{i-1} \hat p$$


# Seminar 18

## Problem 6

The suggested null is that the shots are independent, which is equivalent to the following hypothesis on $X$:


[Geometric distribution wiki](https://en.wikipedia.org/wiki/Geometric_distribution)

$$H_0 : \text{shot are independent, with prob $p$} \iff H_0 : X \sim \text{Geometric}(1-p)$$

$H_0$ implies the following probability mass function for $X$:

$$P(X = x) = p^x (1-p), \quad x= 0, 1,2,3,\dots$$

We only have records on series of postitive length $1,2,3, \dots$. Series of length $0$ are not recorded, therefore to compute the estimated frequency of series of length $x = 1, 2, 3 , \dots$  we must use conditional probability mass function:

$$P(X=  x \mid X > 0) = \frac{P(X = x)}{1 - P(X = 0)}$$

$$P(X = 0) = 1 - p = 0.503, \Rightarrow P(X = x\mid X> 0 ) = p^x$$

> The null could also have been formulated as 
>$$H_0 : P(X = x) = p^x (1-p), \quad x= 1,2,3,\dots$$

We start by computing the estimated class sizes:

\begin{align}
E_1 &= N \cdot P(X = 1\mid X > 0) = 400 \cdot .497  = 198.8\\
E_2 &= N \cdot P(X = 2\mid X > 0) = 400 \cdot .497^2  = 98.8\\
E_3 &= N \cdot P(X = 3\mid X > 0) = 400 \cdot .497^3  = 49.1\\
E_4 &= N \cdot P(X = 4\mid X > 0) = 400 \cdot .497^4  = 24.4\\
E_5 &= N \cdot P(X = 5\mid X > 0) = 400 \cdot .497^5  = 12.12\\
E_{\geq 6} &= 400 \cdot (1 - P(X <6))\\
&= 400 - E_1 - E_2 - E_3 - E_4 - E_5 = 10.7
\end{align}


The Pearson test statistic takes value

$$(173 - 198.8)^2/198.8 + (110 - 98.8)^2/49.7 \\+ (61 - 49.1)^2/49.1 + (28 -24.4)^2/24.4 \\+ (15 - 12.12)^2/12.12 + (13 - 10.7)^2/10.7 = 10.46$$

It is distributed according to $\chi^2(5)$ ($df = 6 - 1$, as the probability of a successful shot is given in the problem statement). The critical value corresponding to the 10% significance level is $9.24$. The obtained test-statistic is above the critical value therefore the null is rejected, i.e. the shots of Michael Jordan were not independent.


## Problem 7


$$H_0 : X \sim \text{Geometric(p)}$$

$p$ is unknown, let's find it via Likelihood method.
We have 120 observations of $X$, denote them $x_1, \dots , x_{120}$. Each one is distributed geometrically:

\begin{align}
P(X_i = x) &=\underbrace{(1-p)^{x-1}}_\text{$x-1$ misses} \cdot \underbrace{p}_\text{hit} \quad x= 1, 2, 3\\
P(X \geq 4) &= 1 - P(X < 4)\\ 
&=1 - \left(p + (1-p)p + (1 - p)^2 p\right)  \\
&= 1 -  \frac{1 - (1-p)^3}{1 - (1-p)}p\\
&= (1-p)^3
\end{align}


$$L( x_1, \dots, x_{120}) = \Pi_{i = 1}^{120} P(X_i = x_i)\\
= \Pi_{x_i = 1} P(X_i = 1) \cdot \Pi_{x_i = 2} P(X_i = 2)\cdot \Pi_{x_i = 3} P(X_i = 3) \cdot \Pi_{x_i \geq 4} P(X_i = x_i)\\
=\underbrace{\Pi_{x_i=1} p}_{p^{50}} \cdot \underbrace{\Pi_{x_i = 2} (1-p)p}_{(1-p)^{40} p^{40}}\cdot \underbrace{\Pi_{x_i = 3} (1-p)^2 p}_{(1-p)^{40} p^{20}} \cdot \underbrace{\left(P(X \geq 4)\right)^{10}}_{(1 - p)^{30}}$$

Maximize the logarithm of the likelihood with respect to $p$:

$$\frac{\partial }{\partial p} \left( 110 \ln p +   110 \ln (1-p) \right)\mid_{p = \hat p_{ML}} = 0$$

$$\frac{110}{\hat p_{ML}} - \frac{110}{1-\hat p_{ML}} = 0\\
\Rightarrow \hat p_{ML} = 1-\hat p_{ML} = 1/2$$


1. Formulate the null

$$H_0 : \text{shots are independent}$$

2. Write down the $E_i$

Using the estimate $\hat p$ of $p$ we can write down the expected frequencies:

\begin{align}
E_1 &= N \cdot \hat P(X = 1)= N \cdot \hat p = 120 \cdot 1/2 = 60\\
E_2 &= N \cdot \hat P(X = 2) = 120 \cdot 1/2\cdot 1/2 = 30\\
E_3 &= N \cdot \hat P(X = 3) = 120 \cdot 1/2^2\cdot 1/2 = 15\\
E_{\geq 4} &= N - E_1 - E_2 - E_3 = 120 - 60 - 30 - 15 = 15.
\end{align}

3. Compute the Pearson test statistic (chi-squared):

$$(50 - 60)^2/60 + (40 - 30)^2/30 + (15 - 20)^2/20 + (10- 15)^2/15 \approx 7.92$$

The test-statistic is distributed as $\chi^2(2)$ ($df = 4 - 1 - 1$, because we used an estimate of the parameter based on the same data), the 5% significance level critical value is $5.99$, below the observed value of the test statistic, therefore the null is rejected at 5% significance level. At 1% signinficance level the null passes, although barely, the 1% significance level critical value being $9.21$.

## Problem 8

$X$ is the number of wins in a match

+ $X=0$ - the opponent won two games in a row
$P(X=0) = P(loss \cap loss) = (1-p)^2$

+ $X = 1$ - 
    -  we won the first game but lost the next two OR
    -  we lost the first game, won the second, but lost the third
    $P(X = 1) = P(win \cap loss \cap loss) + P(loss \cap win \cap loss) =2 p(1-p)^2$

+ $X=2$
    $P(X = 2) = P(win \cap win) +P(loss \cap win \cap win) +P(win\cap loss \cap win) = p^2 + 2p^2 (1-p)$
    
Our null 
$$H_0 : \text{wins in diffferent games are independent and happen with probability $p=.6$}$$

is equivalent to 

$$H_0 : P(X = x) = \begin{cases}
(1 - p)^2 \quad \text{if $x = 0$}\\
2p (1 - p)^2 \quad \text{if $x = 1$}\\
p^2 (3 - 2p) \quad \text{if $x = 2$}
\end{cases}$$

Under the null the estimated frequencies are:

\begin{align}
E_0 &= 200 \cdot P(X = 0) = 200 \cdot .4^2 = 32\\
E_1 &=200 \cdot P(X = 1) =200 \cdot 2\cdot.6\cdot .16 =  38.4\\
E_2 &=200 \cdot P(X = 2) = 200 \cdot .36 \cdot 1.8 = 129.6
\end{align}

The test statistic takes value

$$(27 - 32)^2/32 + (47 - 38.4)^2/38.4 + (127 - 129.6)^2/129.6 = 2.76$$

The test statistic is distributed according to $\chi^2(3 - 1)$, the 5% critical value is $5.99$ above the observed value, therefore we do not reject the null.