2.4 C
London
Friday, December 20, 2024
HomePythonHypothesis Tests in PythonHow to Perform a One Proportion Z-Test in Python

How to Perform a One Proportion Z-Test in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

A one proportion z-test is used to compare an observed proportion to a theoretical one.

This test uses the following null hypotheses:

  • H0p = p0 (population proportion is equal to hypothesized proportion p0)

The alternative hypothesis can be either two-tailed, left-tailed, or right-tailed:

  • H1 (two-tailed): p ≠ p0 (population proportion is not equal to some hypothesized value p0)
  • H1 (left-tailed): p 0 (population proportion is less than some hypothesized value p0)
  • H1 (right-tailed): p > p0 (population proportion is greater than some hypothesized value p0)

The test statistic is calculated as:

z = (p-p0) / √p0(1-p0)/n

where:

  • p: observed sample proportion
  • p0: hypothesized population proportion
  • n: sample size

If the p-value that corresponds to the test statistic z is less than your chosen significance level (common choices are 0.10, 0.05, and 0.01) then you can reject the null hypothesis.

One Proportion Z-Test in Python

To perform a one proportion z-test in Python, we can use the proportions_ztest() function from the statsmodels library, which uses the following syntax:

proportions_ztest(count, nobs, value=None, alternative=’two-sided’) 

where:

  • count: The number of successes
  • nobs: The number of trials
  • value: The hypothesized population proportion
  • alternative: The alternative hypothesis

This function returns a z test-statistic and a corresponding p-value.

The following example shows how to use this function to perform a one proportion z-test in Python.

Example: One Proportion Z-Test in Python

Suppose we want to know whether or not the proportion of residents in a certain county who support a certain law is equal to 60%. To test this, we collect the following data on a random sample:

  • p0: hypothesized population proportion = 0.60
  • x: residents who support law: 64
  • n: sample size = 100

The following code shows how to use the proportions_ztest function to perform a one sample z-test:

#import proportions_ztest function
from statsmodels.stats.proportion import proportions_ztest

#perform one proportion z-test
proportions_ztest(count=60, nobs=100, value=0.64)
(-0.8164965809277268, 0.41421617824252466)

From the output we can see that the z test-statistic is -0.8165 and the corresponding p-value is 0.4142. Since this value is not less than α = 0.05, we fail to reject the null hypothesis. We do not have sufficient evidence to say that the proportion of residents who support the law is different from 0.60.

Additional Resources

An Introduction to the One Proportion Z-Test
One Proportion Z-Test Calculator
How to Perform a One Proportion Z-Test in Excel
How to Perform a One Proportion Z-Test in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories