# Overview

A car manufacturing company releases a new car and it claims that the car has a mileage of 25 kms per liter. Should we believe it or not? And why should or shouldn't we believe it? Hypothesis testing is a method that can be used to make decisions in such situations.

Hypothesis testing generally involves four steps.

- First, we develop two claims:
**null hypothesis** (*H*_{0}) and **alternative hypothesis** (*H*_{a}). In our case ‘the car can run 20 kms per liter’ is a null hypothesis and ‘the car can’t run 20 kms per liter’ is an alternative hypothesis. - Second, we
**collect a sample** (sample of newly manufactures cars) and collect relevant data from them and summarize it using statistics. - Then, we
**calculate** how probable it is to find the result from our analysis if the hypothesis was true. - Finally, we
**reach a conclusion** based on our result in the previous step. If probability of the observed data is very low, we can safely discard our null hypothesis in favor of our alternative hypothesis. If it isn't low, we can continue to believe the null.

Thus, by using hypothesis testing we can evaluate mutually exclusive claims and choose the one which is best supported by our data.

# Hypothesis testing for population proportion

When the parameter of population in which we are interested is categorical in nature (for example, category based on weight: underweight, normal weight and overweight). For such kind of data, we require using z-test for hypothesis testing using population proportion.

## z-test for population proportion

The z-test is named so as it depends upon z-score. The hypothesis testing using z-test follow same four general steps of hypothesis testing.

- In the first step, we declare our null and alternative hypothesis (
*H*_{0} and *H*_{a}). For example, let's say a research claims that 30% of students in an a particular university own an iPhone. So our null hypothesis will be **H**_{0}: p_{0} = 0.3. - We then
**collect random sample** of size *n = 100 *students from the university. The size of sample should meet two criteria: (i) *np*_{0} >= 10 and (ii) *n(1-p*_{0}) >= 10. Since both of our criteria are met, (for (i) we get 30 and for (ii) we get 70), our choice of sample size is fine. - Now suppose, we found that out of 100 students 35 students used iPhone. Then, we can find our sample proportion
*p = 35/100 = 0.35*, which will be followed by calculation of **z-score** given as:

z = \frac{p-p_0}{\sqrt{\frac{p_0(1-p_0)}{n}}}

By using the value calculated above we can find our z-score as:

z = \frac{0.35-0.3}{\sqrt{\frac{0.3(1-0.3)}{100}}} = 1.091

What does 1.091 z-score mean? We can interpret it as our sample proportion being 1.091 standard deviation above our null value (0.3).

4. Now, we will find the corresponding probability (**p-value**) from the calculated z value. The z-value we found has a special property – it follows standard normal distribution as shown in above figure (check the resources to learn about standard normal distribution). For *z = 0*, the standard deviation of sample proportion from null value is zero and its p-value is highest. As we go left or right according to our z-value (if positive we observe p-value right from 0 and if negative left from zero), the standard deviation increases and p-value decreases. Greater the magnitude of z, smaller will be the p-value. For example, if we have z values: *1, -0.5, -2.5, *and *2* then the p-value for *z = -2.5* will be the smallest as its magnitude (2.5) is highest among all. We can use either a z-table or a software to find out corresponding p-value for given z-value. P-value basically gives the probability of getting the observed data if the null hypothesis was true. So, if we get very small p-value (usually smaller than 0.05), we can reject the null hypothesis. If the p-value is large, we can continue to believe in the claim made by research that 30% of the student in the university uses iPhone. From the z-value we calculated, we get corresponding **p-value of 0.137**. The p-value is greater than significance level 0.05, so **we can continue to believe in our null hypothesis**.

# Hypothesis testing for population mean

When the parameter of our interest in the population is quantitative in nature we use population mean for hypothesis testing. We can use either z-test or t-test for that purpose based on the availability of standard deviation of population.

## z-test for population mean

We use z-test when the standard deviation of a population (*σ*) we are interested in is known. Usually, the standard deviation of a population is unknown, but in practice if our sample size is large, then we can estimate standard deviation from the collected sample. We'll illustrate this methods using the same previously followed steps.

- We first define our null (
*H*_{0}: u_{0}) and alternative hypothesis (*H*_{a}). Let’s say the average GRE score of students willing to pursue computer science is *u*_{0} = 310 and standard deviation *σ = 10*. Lets, say the head of department of the computer science of university A claims that student enrolled in his department has higher average GRE score. We assume it to be alternative hypothesis. - We then collect random samples of 5 students and find that the mean GRE score
*x'* is 314. - From the samples we then find sample mean (
*x'*). From these value we can then calculate z-value as:

z = \frac{x'-u_0}{\frac{\sigma}{n}}

z = \frac{314-310}{\frac{10}{5}} = 2

4. We then find the corresponding p-value from the calculated z-value. The process is same as discussed in the z- test for population proportion. If the p-value is large, then it is obvious to observe the data for given null hypothesis, and if it isn’t large enough we can discard our null hypothesis. For z-value = 2, the corresponding p-value is 0.02275 which is a small value, so we reject the null hypothesis and believe that the alternative hypothesis is correct.

## t-test for population mean

Normally, the population standard deviation is not available. For example, when a noodle company produces standard 75 gram packets of noodle, it won’t be possible to find standard deviation from the all the packets of noodle it had produced. In such case, we can’t use z-test. Instead we use t-test. T-test is quite similar to z-test. The only difference is that we will calculate t-score using sample standard deviation (s). We use population mean *u*_{0} as our null hypothesis. We then take a sample of size *n* and find sample mean *x’* and sample standard deviation *s* from our data. We the calculate t-value as given below and reach to the conclusion following very similar process described in above tests.

t = \frac{x'-u_0}{\frac{s}{\sqrt{n}}}

# Trade-offs and extensions

- The size of the sample we collect in the second step of hypothesis testing has a significant role in the process. We know that larger the sample size better is its reflection of the population. However, it is not always economical to collect large sample data. Hence, we have a trade-off between statistical significance and practical feasibility.
**Confidence intervals**: Instead of having a null hypothesis of p_{0} = *x*, we can define an range in which it should lie. We can check whether a null population proportion or mean lies in the interval. If it lies in the interval we can safely accept it.

# Error types: Type I and Type II error

While drawing a conclusion, two types of errors are possible: Type I and Type II error.

**Type I error** arises when we reject null hypothesis when should have accepted it. This error type is also known as **false negative**. **Type II error** occurs when we accept null hypothesis when we should have rejected it. This error type is also known as **false positive**.

# Applications

Hypothesis testing has a variety of applications. Most prominently, it is used to verify the claims of an organization, or to choose between two options by evaluating which one is better. Some specific examples are described below

**Testing performance**: We deploy multiple statistical models and need to find out the best one. For example, we might be trying to improve our 'related content' recommendation engine. Hypothesis testing is used to decide which model is best. **AB Testing: **We deploy different versions of web products and need to find out the best one. Does the red button lead to higher conversion in sign-ups? Does the blue button increase total number of purchases? Hypothesis testing is used to decide which version is better.- In social science, it can be used to verify claims like domestic violence against women in rural area is higher than urban area.
- In healthcare industry, it is used to evaluate whether a proposed drug is effective or not, or whether it is more effective than existing drugs. Or even to decide whether hospital carpeting results in more infection or not.

These wide range of applications of hypothesis testing make it an important skill for any data science professional.

# Resources

- Standard Normal Distribution: The Standard Normal Distribution
- Find Z-value online: Quick P Value from Z Score Calculator
- Z-test: Z-test for Population Means Tutorial