Thursday, December 26, 2024
Google search engine
HomeLanguagesHow to Perform a One Proportion Z-Test in Python

How to Perform a One Proportion Z-Test in Python

In this article, we will be looking at the approach to perform a one-proportion Z-test in the Python programming language. 

Z-test is a statistical test to determine whether two population means are different when the variances are known and the sample size is large.

One-proportion Z-test formula:

z = \frac{(P-Po)}{\sqrt{Po(1-Po)/n}}

Where: 

  • P: Observed sample proportion
  • Po: Hypothesized Population Proportion
  • n: Sample size 

The one-proportional Z-test uses the following null hypotheses:

  • H0: p = 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 (two-tailed population proportion is not equal to some hypothesized value p0)
  • H1 (left-tailed): p < p0 (left-tailed population proportion is less than some hypothesized value p0)
  • H1 (right-tailed): p > p0 (right-tailed population proportion is greater than some hypothesized value p0)

Method 1: Calculating  one-proportional Z-test using formula

In this approach, we will be calculating the one-proportional Z-test using the given formula and by simply putting the given value in the formula and getting the result.

Formula:

z=(P-Po)/sqrt(Po(1-Po)/n

In this example, we are using the P-value to 0.86, Po to 0.80, and n to 100, and by using this we will be calculating the z-test one proportional in the python programming language.

Python




import math
  
P = 0.86
Po = 0.80
n = 100
a = (P-Po)
b = Po*(1-Po)/n
z = a/math.sqrt(b)
print(z)


Output:

1.4999999999999984

Method 2: Calculating  one-proportional Z-test using  proportions_ztest() function

In this approach, we need to first import the statsmodels.stats.proportion library to the python compiler and then call the proportions_ztest() function to simpling get the one proportional Z-test by adding the parameters to the function.

proportions_ztest() function: This function is used to test for proportions based on the normal (z) test.

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

Parameters:

  • count: the number of successes in nobs trials.
  • nobs: the number of trials or observations, with the same length as count.
  • value: the hypothesized population proportion.
  • alternative: The alternative hypothesis.  

In this example, we will be using the same values as used in the previous example, and bypassing these values to the proportions_ztest() function we will be calculating the one-proportional z-test in the python programming language.

Python




# import library
from statsmodels.stats.proportion import proportions_ztest
  
# perform one proportion z-test
proportions_ztest(count=80, nobs=100, value=0.86)


Output:

(-1.4999999999999984, 0.1336144025377165)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments