# Statistics - Hypothesis Testing a Proportion

A population proportion is the share of a population that belongs to a particular category.

Hypothesis tests are used to check a claim about the size of that population proportion.

## Hypothesis Testing a Proportion

The following steps are used for a hypothesis test:

- Check the conditions
- Define the claims
- Decide the significance level
- Calculate the test statistic
- Conclusion

For example:

**Population**: Nobel Prize winners**Category**: Born in the United States of America

And we want to check the claim:

```
```"**More** than 20% of Nobel Prize winners were born in the US"

By taking a sample of 40 randomly selected Nobel Prize winners we could find that:

```
```10 out of 40 Nobel Prize winners in the sample were born in the US

The **sample** proportion is then: \(\displaystyle \frac{10}{40} = 0.25\), or 25%.

From this sample data we check the claim with the steps below.

## 1. Checking the Conditions

The conditions for calculating a confidence interval for a proportion are:

- The sample is randomly selected
- There is only two options:
- Being in the category
- Not being in the category

- The sample needs at least:
- 5 members in the category
- 5 members not in the category

In our example, we randomly selected 10 people that were born in the US.

The rest were not born in the US, so there are 30 in the other category.

The conditions are fulfilled in this case.

**Note:** It is possible to do a hypothesis test without having 5 of each category. But special adjustments need to be made.

## 2. Defining the Claims

We need to define a **null hypothesis** (\(H_{0}\)) and an **alternative hypothesis** (\(H_{1}\)) based on the claim we are checking.

The claim was:

```
```"**More** than 20% of Nobel Prize winners were born in the US"

In this case, the **parameter** is the proportion of Nobel Prize winners born in the US (\(p\)).

The null and alternative hypothesis are then:

```
```**Null hypothesis**: 20% of Nobel Prize winners were born in the US.

**Alternative hypothesis**: **More** than 20% of Nobel Prize winners were born in the US.

Which can be expressed with symbols as:

```
```\(H_{0}\): \(p = 0.20 \)

\(H_{1}\): \(p > 0.20 \)

This is a '**right** tailed' test, because the alternative hypothesis claims that the proportion is **more** than in the null hypothesis.

If the data supports the alternative hypothesis, we **reject** the null hypothesis and **accept** the alternative hypothesis.

## 3. Deciding the Significance Level

The significance level (\(\alpha\)) is the **uncertainty** we accept when rejecting the null hypothesis in a hypothesis test.

The significance level is a percentage probability of accidentally making the wrong conclusion.

Typical significance levels are:

- \(\alpha = 0.1\) (10%)
- \(\alpha = 0.05\) (5%)
- \(\alpha = 0.01\) (1%)

A lower significance level means that the evidence in the data needs to be stronger to reject the null hypothesis.

There is no "correct" significance level - it only states the uncertainty of the conclusion.

**Note:** A 5% significance level means that when we reject a null hypothesis:

We expect to reject a **true** null hypothesis 5 out of 100 times.

## 4. Calculating the Test Statistic

The test statistic is used to decide the outcome of the hypothesis test.

The test statistic is a standardized value calculated from the sample.

The formula for the test statistic (TS) of a population proportion is:

\(\displaystyle \frac{\hat{p} - p}{\sqrt{p(1-p)}} \cdot \sqrt{n} \)

\(\hat{p}-p\) is the **difference** between the **sample** proportion (\(\hat{p}\)) and the claimed **population** proportion (\(p\)).

\(n\) is the sample size.

In our example:

```
```The claimed (\(H_{0}\)) population proportion (\(p\)) was \( 0.20 \)

The sample proportion (\(\hat{p}\)) was 10 out of 40, or: \(\displaystyle \frac{10}{40} = 0.25\)

The sample size (\(n\)) was \(40\)

So the test statistic (TS) is then:

\(\displaystyle \frac{0.25-0.20}{\sqrt{0.2(1-0.2)}} \cdot \sqrt{40} = \frac{0.05}{\sqrt{0.2(0.8)}} \cdot \sqrt{40} = \frac{0.05}{\sqrt{0.16}} \cdot \sqrt{40} \approx \frac{0.05}{0.4} \cdot 6.325 = \underline{0.791}\)

You can also calculate the test statistic using programming language functions:

### Example

With Python use the scipy and math libraries to calculate the test statistic for a proportion.

```
import scipy.stats as stats
```

import math

# Specify the number of occurrences (x), the sample size (n), and the proportion claimed in the null-hypothesis (p)

x = 10

n = 40

p = 0.2

# Calculate the sample proportion

p_hat = x/n

# Calculate and print the test statistic

print((p_hat-p)/(math.sqrt((p*(1-p))/(n))))

Try it Yourself »
### Example

With R use the built-in `prop.test()`

function to calculate the test statistic for a proportion.

```
# Specify the sample occurrences (x), the sample size (n), and the null-hypothesis claim (p)
```

x <- 10

n <- 40

p <- 0.20

# Calculate the sample proportion

p_hat = x/n

# Calculate and print the test statistic

(p_hat-p)/(sqrt((p*(1-p))/(n)))

Try it Yourself »
## 5. Concluding

There are two main approaches for making the conclusion of a hypothesis test:

- The
**critical value**approach compares the test statistic with the critical value of the significance level. - The
**P-value**approach compares the P-value of the test statistic and with the significance level.

**Note:** The two approaches are only different in how they present the conclusion.

### The Critical Value Approach

For the critical value approach we need to find the **critical value** (CV) of the significance level (\(\alpha\)).

For a population proportion test, the critical value (CV) is a **Z-value** from a standard normal distribution.

This critical Z-value (CV) defines the **rejection region** for the test.

The rejection region is an area of probability in the tails of the standard normal distribution.

Because the claim is that the population proportion is **more** than 20%, the rejection region is in the right tail:

The size of the rejection region is decided by the significance level (\(\alpha\)).

Choosing a significance level (\(\alpha\)) of 0.05, or 5%, we can find the critical Z-value from a Z-table, or with a programming language function:

**Note:** The functions find the Z-value for an area from the left side.

To find the Z-value for a right tail we need to use the function on the area to the left of the tail (1-0.05 = 0.95).

### Example

With Python use the Scipy Stats library `norm.ppf()`

function find the Z-value for an \(\alpha\) = 0.05 in the right tail.

```
import scipy.stats as stats
```

print(stats.norm.ppf(1-0.05))

Try it Yourself »
### Example

With R use the built-in `qnorm()`

function to find the Z-value for an \(\alpha\) = 0.05 in the right tail.

```
qnorm(1-0.05)
```

Try it Yourself »
Using either method we can find that the critical Z-value is \(\approx \underline{1.6449}\)

For a **right** tailed test we need to check if the test statistic (TS) is **bigger** than the critical value (CV).

If the test statistic is bigger than the critical value, the test statistic is in the **rejection region**.

When the test statistic is in the rejection region, we **reject** the null hypothesis (\(H_{0}\)).

Here, the test statistic (TS) was \(\approx \underline{0.791}\) and the critical value was \(\approx \underline{1.6449}\)

Here is an illustration of this test in a graph:

Since the test statistic was **smaller** than the critical value we do **not** reject the null hypothesis.

This means that the sample data does not support the alternative hypothesis.

And we can summarize the conclusion stating:

```
```The sample data does **not** support the claim that "more than 20% of Nobel Prize winners were born in the US" at a **5% significance level**.

### The P-Value Approach

For the P-value approach we need to find the **P-value** of the test statistic (TS).

If the P-value is **smaller** than the significance level (\(\alpha\)), we **reject** the null hypothesis (\(H_{0}\)).

The test statistic was found to be \( \approx \underline{0.791} \)

For a population proportion test, the test statistic is a Z-Value from a standard normal distribution.

Because this is a **right** tailed test, we need to find the P-value of a Z-value **bigger** than 0.791.

We can find the P-value using a Z-table, or with a programming language function:

**Note:** The functions find the P-value (area) to the left side of Z-value.

To find the P-value for a right tail we need to subtract the left area from the total area: 1 - the output of the function.

### Example

With Python use the Scipy Stats library `norm.cdf()`

function find the P-value of a Z-value bigger than 0.791:

```
import scipy.stats as stats
```

print(1-stats.norm.cdf(0.791))

Try it Yourself »
### Example

With R use the built-in `pnorm()`

function find the P-value of a Z-value bigger than 0.791:

```
1-pnorm(0.791)
```

Try it Yourself »
Using either method we can find that the P-value is \(\approx \underline{0.2145}\)

This tells us that the significance level (\(\alpha\)) would need to be bigger than 0.2145, or 21.45%, to **reject** the null hypothesis.

Here is an illustration of this test in a graph:

This P-value is **bigger** than any of the common significance levels (10%, 5%, 1%).

So the null hypothesis is **kept** at all of these significance levels.

And we can summarize the conclusion stating:

```
```The sample data does **not** support the claim that "more than 20% of Nobel Prize winners were born in the US" at a **10%, 5%, or 1% significance level**.

**Note:** It may still be true that the real population proportion is more than 20%.

But there was not strong enough evidence to support it with this sample.

## Calculating a P-Value for a Hypothesis Test with Programming

Many programming languages can calculate the P-value to decide outcome of a hypothesis test.

Using software and programming to calculate statistics is more common for bigger sets of data, as calculating manually becomes difficult.

The P-value calculated here will tell us the **lowest possible significance level** where the null-hypothesis can be rejected.

### Example

With Python use the scipy and math libraries to calculate the P-value for a right tailed hypothesis test for a proportion.

Here, the sample size is 40, the occurrences are 10, and the test is for a proportion bigger than 0.20.

```
import scipy.stats as stats
```

import math

# Specify the number of occurrences (x), the sample size (n), and the proportion claimed in the null-hypothesis (p)

x = 10

n = 40

p = 0.2

# Calculate the sample proportion

p_hat = x/n

# Calculate the test statistic

test_stat = (p_hat-p)/(math.sqrt((p*(1-p))/(n)))

# Output the p-value of the test statistic (right tailed test)

print(1-stats.norm.cdf(test_stat))

Try it Yourself »
### Example

With R use the built-in `prop.test()`

function find the P-value for a right tailed hypothesis test for a proportion.

Here, the sample size is 40, the occurrences are 10, and the test is for a proportion bigger than 0.20.

```
# Specify the sample occurrences (x), the sample size (n), and the null-hypothesis claim (p)
```

x <- 10

n <- 40

p <- 0.20

# P-value from right-tail proportion test at 0.05 significance level

prop.test(x, n, p, alternative = c("greater"), conf.level = 0.95, correct = FALSE)$p.value

Try it Yourself »
**Note:** The `conf.level`

in the R code is the reverse of the significance level.

Here, the significance level is 0.05, or 5%, so the conf.level is 1-0.05 = 0.95, or 95%.

## Left-Tailed and Two-Tailed Tests

This was an example of a **right** tailed test, where the alternative hypothesis claimed that parameter is **bigger** than the null hypothesis claim.

You can check out an equivalent step-by-step guide for other types here: