# Frequentist Inference Case Study - Part A 

## 1. Learning objectives

Welcome to part A of the Frequentist inference case study! The purpose of this case study is to help you apply the concepts associated with Frequentist inference in Python. Frequentist inference is the process of deriving conclusions about an underlying distribution via the observation of data. In particular, you'll practice writing Python code to apply the following statistical concepts: 
* the _z_-statistic
* the _t_-statistic
* the difference and relationship between the two
* the Central Limit Theorem, including its assumptions and consequences
* how to estimate the population mean and standard deviation from a sample
* the concept of a sampling distribution of a test statistic, particularly for the mean
* how to combine these concepts to calculate a confidence interval

## Prerequisites

To be able to complete this notebook, you are expected to have a basic understanding of:
* what a random variable is (p.400 of Professor Spiegelhalter's *The Art of Statistics, hereinafter AoS*)
* what a population, and a population distribution, are (p. 397 of *AoS*)
* a high-level sense of what the normal distribution is (p. 394 of *AoS*)
* what the t-statistic is (p. 275 of *AoS*)

Happily, these should all be concepts with which you are reasonably familiar after having read ten chapters of Professor Spiegelhalter's book, *The Art of Statistics*.

We'll try to relate the concepts in this case study back to page numbers in *The Art of Statistics* so that you can focus on the Python aspects of this case study. The second part (part B) of this case study will involve another, more real-world application of these tools. 

For this notebook, we will use data sampled from a known normal distribution. This allows us to compare our results with theoretical expectations.

## 2. An introduction to sampling from the normal distribution

First, let's explore the ways we can generate the normal distribution. While there's a fair amount of interest in [sklearn](https://scikit-learn.org/stable/) within the machine learning community, you're likely to have heard of [scipy](https://docs.scipy.org/doc/scipy-0.15.1/reference/index.html) if you're coming from the sciences. For this assignment, you'll use [scipy.stats](https://docs.scipy.org/doc/scipy-0.15.1/reference/tutorial/stats.html) to complete your work. 

This assignment will require some digging around and getting your hands dirty (your learning is maximized that way)! You should have the research skills and the tenacity to do these tasks independently, but if you struggle, reach out to your immediate community and your mentor for help. 


```python
import math
from scipy.stats import norm
from scipy.stats import t
import numpy as np
import pandas as pd
from numpy.random import seed
import matplotlib.pyplot as plt
```

__Q1:__ Call up the documentation for the `norm` function imported above. (Hint: that documentation is [here](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html)). What is the second listed method?

**Probability density function** - pdf(x, loc=0, scale=1)

__Q2:__ Use the method that generates random variates to draw five samples from the standard normal distribution. 

__A:__See below


```python
seed(47)
# draw five samples here

# Initialize random numbers: random_numbers
random_numbers = np.empty(5)

for i in range(5):
    random_numbers[i] = np.random.normal()
```

__Q3:__ What is the mean of this sample? Is it exactly equal to the value you expected? Hint: the sample was drawn from the standard normal distribution. If you want a reminder of the properties of this distribution, check out p. 85 of *AoS*. 

See below__A:__See below


```python
# Calculate and print the mean here, hint: use np.mean()
print("The mean of this sample is: {0:f}".format(np.mean(random_numbers)))
```

    The mean of this sample is: 0.193556


__Q4:__ What is the standard deviation of these numbers? Calculate this manually here as $\sqrt{\frac{\sum_i(x_i - \bar{x})^2}{n}}$ (This is just the definition of **standard deviation** given by Professor Spiegelhalter on p.403 of *AoS*). Hint: np.sqrt() and np.sum() will be useful here and remember that numPy supports [broadcasting](https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html).

__A:__ Using random.normal()


```python
# Array of differences to mean: differences
differences = random_numbers - np.mean(random_numbers)

# Square the differences: diff_sq
diff_sq = differences*differences

# Compute the mean square difference: variance_explicit
variance_explicit = np.mean(diff_sq)

# Print the standard deviation as sqrt of variance_explicit
print("The sample standard deviation of these numbers is: {0:f}".format(np.sqrt(variance_explicit)))
```

    The sample standard deviation of these numbers is: 0.960620


Here we have calculated the actual standard deviation of a small data set (of size 5). But in this case, this small data set is actually a sample from our larger (infinite) population. In this case, the population is infinite because we could keep drawing our normal random variates until our computers die! 

In general, the sample mean we calculate will not be equal to the population mean (as we saw above). A consequence of this is that the sum of squares of the deviations from the _population_ mean will be bigger than the sum of squares of the deviations from the _sample_ mean. In other words, the sum of squares of the deviations from the _sample_ mean is too small to give an unbiased estimate of the _population_ variance. An example of this effect is given [here](https://en.wikipedia.org/wiki/Bessel%27s_correction#Source_of_bias). Scaling our estimate of the variance by the factor $n/(n-1)$ gives an unbiased estimator of the population variance. This factor is known as [Bessel's correction](https://en.wikipedia.org/wiki/Bessel%27s_correction). The consequence of this is that the $n$ in the denominator is replaced by $n-1$.

You can see Bessel's correction reflected in Professor Spiegelhalter's definition of **variance** on p. 405 of *AoS*.

__Q5:__ If all we had to go on was our five samples, what would be our best estimate of the population standard deviation? Use Bessel's correction ($n-1$ in the denominator), thus $\sqrt{\frac{\sum_i(x_i - \bar{x})^2}{n-1}}$.

__A:__


```python
def pop_variance(data):
    n = len(data)
    mean = sum(data) / n
    return sum((x - mean) ** 2 for x in data) / (n - 1)

def pop_stdev(data):
    pop_var = pop_variance(data)
    pop_std_dev = math.sqrt(pop_var)
    return pop_std_dev
```

__Q6:__ Now use numpy's std function to calculate the standard deviation of our random samples. Which of the above standard deviations did it return?

__A:__ See below


```python
print("The population standard deviation of these numbers is: {0:f}".format(pop_stdev(random_numbers)))
```

    The population standard deviation of these numbers is: 1.074005


**Observation** :The sum of squares of the distance from samples to the population mean (**1.074**) will always be bigger than the sum of squares of the distance to the sample mean (**0.96**)

__Q7:__ Consult the documentation for np.std() to see how to apply the correction for estimating the population parameter and verify this produces the expected result.

__A:__See below


```python
# if N-1 is the denominator, divisor N-ddof is used instead, so ddof = 1 i.e. Degree of freedom for estimating population
# parameter is supposed to be 1

print("The population standard deviation of these numbers as calculated using np.std() is: {0:f}".format(np.std(random_numbers, ddof = 1)))
```

    The population standard deviation of these numbers as calculated using np.std() is: 1.074005


**Conclusion**: Both calcualtions of standard deviation estimation for the population match

### Summary of section

In this section, you've been introduced to the scipy.stats package and used it to draw a small sample from the standard normal distribution. You've calculated the average (the mean) of this sample and seen that this is not exactly equal to the expected population parameter (which we know because we're generating the random variates from a specific, known distribution). You've been introduced to two ways of calculating the standard deviation; one uses $n$ in the denominator and the other uses $n-1$ (Bessel's correction). You've also seen which of these calculations np.std() performs by default and how to get it to generate the other.

You use $n$ as the denominator if you want to calculate the standard deviation of a sequence of numbers. You use $n-1$ if you are using this sequence of numbers to estimate the population parameter. This brings us to some terminology that can be a little confusing.

The population parameter is traditionally written as $\sigma$ and the sample statistic as $s$. Rather unhelpfully, $s$ is also called the sample standard deviation (using $n-1$) whereas the standard deviation of the sample uses $n$. That's right, we have the sample standard deviation and the standard deviation of the sample and they're not the same thing!

The sample standard deviation
\begin{equation}
s = \sqrt{\frac{\sum_i(x_i - \bar{x})^2}{n-1}} \approx \sigma,
\end{equation}
is our best (unbiased) estimate of the population parameter ($\sigma$).

If your dataset _is_ your entire population, you simply want to calculate the population parameter, $\sigma$, via
\begin{equation}
\sigma = \sqrt{\frac{\sum_i(x_i - \bar{x})^2}{n}}
\end{equation}
as you have complete, full knowledge of your population. In other words, your sample _is_ your population. It's worth noting that we're dealing with what Professor Spiegehalter describes on p. 92 of *AoS* as a **metaphorical population**: we have all the data, and we act as if the data-point is taken from a population at random. We can think of this population as an imaginary space of possibilities. 

If, however, you have sampled _from_ your population, you only have partial knowledge of the state of your population. In this case, the standard deviation of your sample is not an unbiased estimate of the standard deviation of the population, in which case you seek to estimate that population parameter via the sample standard deviation, which uses the $n-1$ denominator.

Great work so far! Now let's dive deeper.

## 3. Sampling distributions

So far we've been dealing with the concept of taking a sample from a population to infer the population parameters. One statistic we calculated for a sample was the mean. As our samples will be expected to vary from one draw to another, so will our sample statistics. If we were to perform repeat draws of size $n$ and calculate the mean of each, we would expect to obtain a distribution of values. This is the sampling distribution of the mean. **The Central Limit Theorem (CLT)** tells us that such a distribution will approach a normal distribution as $n$ increases (the intuitions behind the CLT are covered in full on p. 236 of *AoS*). For the sampling distribution of the mean, the standard deviation of this distribution is given by

\begin{equation}
\sigma_{mean} = \frac{\sigma}{\sqrt n}
\end{equation}

where $\sigma_{mean}$ is the standard deviation of the sampling distribution of the mean and $\sigma$ is the standard deviation of the population (the population parameter).

This is important because typically we are dealing with samples from populations and all we know about the population is what we see in the sample. From this sample, we want to make inferences about the population. We may do this, for example, by looking at the histogram of the values and by calculating the mean and standard deviation (as estimates of the population parameters), and so we are intrinsically interested in how these quantities vary across samples. 

In other words, now that we've taken one sample of size $n$ and made some claims about the general population, what if we were to take another sample of size $n$? Would we get the same result? Would we make the same claims about the general population? This brings us to a fundamental question: _when we make some inference about a population based on our sample, how confident can we be that we've got it 'right'?_

We need to think about **estimates and confidence intervals**: those concepts covered in Chapter 7, p. 189, of *AoS*.

Now, the standard normal distribution (with its variance equal to its standard deviation of one) would not be a great illustration of a key point. Instead, let's imagine we live in a town of 50,000 people and we know the height of everyone in this town. We will have 50,000 numbers that tell us everything about our population. We'll simulate these numbers now and put ourselves in one particular town, called 'town 47', where the population mean height is 172 cm and population standard deviation is 5 cm.


```python
seed(47)
pop_heights = norm.rvs(172, 5, size=50000)
```


```python
_ = plt.hist(pop_heights, bins=30)
_ = plt.xlabel('height (cm)')
_ = plt.ylabel('number of people')
_ = plt.title('Distribution of heights in entire town population')
_ = plt.axvline(172, color='r')
_ = plt.axvline(172+5, color='r', linestyle='--')
_ = plt.axvline(172-5, color='r', linestyle='--')
_ = plt.axvline(172+10, color='r', linestyle='-.')
_ = plt.axvline(172-10, color='r', linestyle='-.')
```

Now, 50,000 people is rather a lot to chase after with a tape measure. If all you want to know is the average height of the townsfolk, then can you just go out and measure a sample to get a pretty good estimate of the average height?


```python
def townsfolk_sampler(n):
    return np.random.choice(pop_heights, n)
```

Let's say you go out one day and randomly sample 10 people to measure.


```python
seed(47)
daily_sample1 = townsfolk_sampler(10)
```


```python
_ = plt.hist(daily_sample1, bins=10)
_ = plt.xlabel('height (cm)')
_ = plt.ylabel('number of people')
_ = plt.title('Distribution of heights in sample size 10')
```

The sample distribution doesn't resemble what we take the population distribution to be. What do we get for the mean?


```python
np.mean(daily_sample1)
```




    173.47911444163503



And if we went out and repeated this experiment?


```python
daily_sample2 = townsfolk_sampler(10)
```


```python
np.mean(daily_sample2)
```




    173.7317666636263



__Q8:__ Simulate performing this random trial every day for a year, calculating the mean of each daily sample of 10, and plot the resultant sampling distribution of the mean.

__A:__See below


```python
n = 365
avg = []

for i in range(1,n):
    a = np.random.choice(townsfolk_sampler(10))
    avg.append(np.mean(a))
```


```python
plt.hist(avg[0:], 20)
plt.axvline(pop_heights.mean(), color = 'C1')
```

The above is the distribution of the means of samples of size 10 taken from our population. The Central Limit Theorem tells us the expected mean of this distribution will be equal to the population mean, and standard deviation will be $\sigma / \sqrt n$, which, in this case, should be approximately 1.58.

__Q9:__ Verify the above results from the CLT.

__A:__Verify the above histogram results with the population mean and standard deviation of the population (computed below)


```python
print("The population standard deviation of these numbers is: {0:f}".format(np.mean(pop_heights)))
```

    The population standard deviation of these numbers is: 172.019260



```python
print("The population standard deviation of these numbers is: {0:f}".format(pop_stdev(pop_heights)/np.sqrt(10)))
```

    The population standard deviation of these numbers is: 1.578258


Remember, in this instance, we knew our population parameters, that the average height really is 172 cm and the standard deviation is 5 cm, and we see some of our daily estimates of the population mean were as low as around 168 and some as high as 176.

__Q10:__ Repeat the above year's worth of samples but for a sample size of 50 (perhaps you had a bigger budget for conducting surveys that year)! Would you expect your distribution of sample means to be wider (more variable) or narrower (more consistent)? Compare your resultant summary statistics to those predicted by the CLT.

__A:__ AS you can see below, as the sample size increases, the sample mean tends to get closer to the population mean


```python
seed(47)
# calculate daily means from the larger sample size here
daily_sample50 = townsfolk_sampler(50)

_ = plt.hist(daily_sample1, bins=10)
_ = plt.xlabel('height (cm)')
_ = plt.ylabel('number of people')
_ = plt.title('Distribution of heights in sample size 50')
```


```python
np.mean(daily_sample50)
```




    172.7815108576788




```python
daily_sample200 = townsfolk_sampler(200)
np.mean(daily_sample200)
```




    172.5599929095019



What we've seen so far, then, is that we can estimate population parameters from a sample from the population, and that samples have their own distributions. Furthermore, the larger the sample size, the narrower are those sampling distributions.

### Normally testing time!

All of the above is well and good. We've been sampling from a population we know is normally distributed, we've come to understand when to use $n$ and when to use $n-1$ in the denominator to calculate the spread of a distribution, and we've  seen the Central Limit Theorem in action for a sampling distribution. All seems very well behaved in Frequentist land. But, well, why should we really care?

Remember, we rarely (if ever) actually know our population parameters but we still have to estimate them somehow. If we want to make inferences to conclusions like "this observation is unusual" or "my population mean has changed" then we need to have some idea of what the underlying distribution is so we can calculate relevant probabilities. In frequentist inference, we use the formulae above to deduce these population parameters. Take a moment in the next part of this assignment to refresh your understanding of how these probabilities work.

Recall some basic properties of the standard normal distribution, such as that about 68% of observations are within plus or minus 1 standard deviation of the mean. Check out the precise definition of a normal distribution on p. 394 of *AoS*. 

__Q11:__ Using this fact, calculate the probability of observing the value 1 or less in a single observation from the standard normal distribution. Hint: you may find it helpful to sketch the standard normal distribution (the familiar bell shape) and mark the number of standard deviations from the mean on the x-axis and shade the regions of the curve that contain certain percentages of the population.

__A:__See below

Calculating this probability involved calculating the area under the curve from the value of 1 and below. To put it in mathematical terms, we need to *integrate* the probability density function. We could just add together the known areas of chunks (from -Inf to 0 and then 0 to $+\sigma$ in the example above). One way to do this is to look up tables (literally). Fortunately, scipy has this functionality built in with the cdf() function.


```python
def ecdf(data):
    """Compute ECDF for a one-dimensional array of measurements."""
    # Number of data points: n
    n = len(data)

    # x-data for the ECDF: x
    x = np.sort(data)

    # y-data for the ECDF: y
    y = np.arange(1, n+1) / n

    return x, y
```


```python
mu = np.mean(pop_heights)
sigma = np.std(pop_heights)

samples_std1 = np.random.normal(mu, 1*sigma, size = n)
samples_std2 = np.random.normal(mu, 2*sigma, size = n)
samples_std3 = np.random.normal(mu, 3*sigma, size = n)

# Generate CDFs
x_std1, y_std1 = ecdf(samples_std1)
x_std2, y_std2 = ecdf(samples_std2)
x_std3, y_std3 = ecdf(samples_std3)

# Plot CDFs
_ = plt.plot(x_std1, y_std1, marker = '.', color='r', linestyle = '-')
_ = plt.plot(x_std2, y_std2, marker = '.', color='b', linestyle = '-')
_ = plt.plot(x_std3, y_std3, marker = '.', color='g', linestyle = '-')
_ = plt.axhline(norm.cdf(1, loc=0,scale=1), color='r', linewidth=0.5)

# Make a legend and show the plot
_ = plt.legend(('std = 1', 'std = 2', 'std = 3'), loc='lower right')
plt.show()
```

__Q12:__ Use the cdf() function to answer the question above again and verify you get the same answer.

__A:__

??

__Q13:__ Using our knowledge of the population parameters for our townsfolks' heights, what is the probability of selecting one person at random and their height being 177 cm or less? Calculate this using both of the approaches given above.

__A:__


```python
mu = np.mean(pop_heights)
sigma = np.std(pop_heights)
norm.cdf(177, mu, sigma)
```




    0.8408546025640735



__Q14:__ Turning this question around — suppose we randomly pick one person and measure their height and find they are 2.00 m tall. How surprised should we be at this result, given what we know about the population distribution? In other words, how likely would it be to obtain a value at least as extreme as this? Express this as a probability. 

__A:__


```python
1 - norm.cdf(200, mu, sigma)
```




    1.0327726340797483e-08



What we've just done is calculate the ***p-value*** of the observation of someone 2.00m tall (review *p*-values if you need to on p. 399 of *AoS*). We could calculate this probability by virtue of knowing the population parameters. We were then able to use the known properties of the relevant normal distribution to calculate the probability of observing a value at least as extreme as our test value.

We're about to come to a pinch, though. We've said a couple of times that we rarely, if ever, know the true population parameters; we have to estimate them from our sample and we cannot even begin to estimate the standard deviation from a single observation. 

This is very true and usually we have sample sizes larger than one. This means we can calculate the mean of the sample as our best estimate of the population mean and the standard deviation as our best estimate of the population standard deviation. 

In other words, we are now coming to deal with the sampling distributions we mentioned above as we are generally concerned with the properties of the sample means we obtain. 

Above, we highlighted one result from the CLT, whereby the sampling distribution (of the mean) becomes narrower and narrower with the square root of the sample size. We remind ourselves that another result from the CLT is that _even if the underlying population distribution is not normal, the sampling distribution will tend to become normal with sufficiently large sample size_. (**Check out p. 199 of AoS if you need to revise this**). This is the key driver for us 'requiring' a certain sample size, for example you may frequently see a minimum sample size of 30 stated in many places. In reality this is simply a rule of thumb; if the underlying distribution is approximately normal then your sampling distribution will already be pretty normal, but if the underlying distribution is heavily skewed then you'd want to increase your sample size.

__Q15:__ Let's now start from the position of knowing nothing about the heights of people in our town.
* Use the random seed of 47, to randomly sample the heights of 50 townsfolk
* Estimate the population mean using np.mean
* Estimate the population standard deviation using np.std (remember which denominator to use!)
* Calculate the (95%) [margin of error](https://www.statisticshowto.datasciencecentral.com/probability-and-statistics/hypothesis-testing/margin-of-error/#WhatMofE) (use the exact critial z value to 2 decimal places - [look this up](https://www.statisticshowto.datasciencecentral.com/probability-and-statistics/find-critical-values/) or use norm.ppf()) Recall that the ***margin of error*** is mentioned on p. 189 of the *AoS* and discussed in depth in that chapter). 
* Calculate the 95% Confidence Interval of the mean (***confidence intervals*** are defined on p. 385 of *AoS*) 
* Does this interval include the true population mean?

__A:__


```python
seed(47)
# take your sample now
sample_50 = townsfolk_sampler(50)
```


```python
# Calculate the mean
pop_mean = np.mean(sample_50)
pop_mean
print("The population mean is: {0:f}".format(pop_mean))
```

    The population mean is: 172.781511



```python
pop_sigma = np.std(sample_50, ddof = 1)
pop_sigma
print("The population standard deviation of these numbers as calculated using np.std() is: {0:f}".format(pop_sigma))
```

    The population standard deviation of these numbers as calculated using np.std() is: 4.195424



```python
z_score = norm.ppf((1 + 0.95) / 2)
z_score
```




    1.959963984540054




```python
# Calculate the standard error
std_error = pop_sigma / np.sqrt(len(sample_50))
std_error
```




    0.5933226036092445




```python
# Margin of Error = z-score * standard error
moe = z_score * std_error
moe
```




    1.1628909342876539




```python
# Calculate 95% confidence interval for the mean
[pop_mean - moe, pop_mean + moe]
```




    [171.61861992339115, 173.94440179196644]



__Q16:__ Above, we calculated the confidence interval using the critical z value. What is the problem with this? What requirement, or requirements, are we (strictly) failing?

__A:__ 
If we want to calculate confidence interval for a sample size that is smaller or equal to 30, we use the t-distribution, but only if we feel it is appropriate to assume that the population distribution itself is normal (or close to normal). In above case for sample size 50, we know it is close to normal, so we can use t distribution for calculating Confidence interval

**Reason**: Small samples are less precise and hence confidence interval range is larger, in which case, t-distribution provides a better estimation of Confidence interval by providing a broader margin of error whereas z-score calculation underestimates the error and tends to give incorrect results

__Q17:__ Calculate the 95% confidence interval for the mean using the _t_ distribution. Is this wider or narrower than that based on the normal distribution above? If you're unsure, you may find this [resource](https://www.statisticshowto.datasciencecentral.com/probability-and-statistics/confidence-interval/) useful. For calculating the critical value, remember how you could calculate this for the normal distribution using norm.ppf().

__A:__see below


```python
#Reference: https://stackoverflow.com/questions/15033511/compute-a-confidence-interval-from-sample-data

import statsmodels.stats.api as sms

a = 1.0 * np.array(sample_50)
sms.DescrStatsW(a).tconfint_mean()
```




    (171.589184447403, 173.9738372679546)



This is slightly wider than the previous confidence interval. This reflects the greater uncertainty given that we are estimating population parameters from a sample.

## 4. Learning outcomes

Having completed this project notebook, you now have hands-on experience:
* sampling and calculating probabilities from a normal distribution
* identifying the correct way to estimate the standard deviation of a population (the population parameter) from a sample
* with sampling distribution and now know how the Central Limit Theorem applies
* with how to calculate critical values and confidence intervals

bc76e29ed1e9fc0fc4363cc3c622305b2878ac51
ipynb
Jupyter Notebook
Frequentist Case Study/Frequentist Inference Case Study - Part A.ipynb
shalin4788/Springboard-Do-not-refer-
e7627e6f4b09456e08c6f10baeb66b0a22422b7a
MIT
2020-10-23T06:24:18.000Z
2020-10-23T06:24:25.000Z
Frequentist Case Study/Frequentist Inference Case Study - Part A.ipynb
shalin4788/Springboard-Do-not-refer-
e7627e6f4b09456e08c6f10baeb66b0a22422b7a
MIT
2021-06-08T22:56:21.000Z
2022-01-13T03:35:07.000Z
Frequentist Case Study/Frequentist Inference Case Study - Part A.ipynb
shalin4788/Springboard
e7627e6f4b09456e08c6f10baeb66b0a22422b7a
MIT
Qwen/Qwen-72B
1. YES
2. YES