Contents

One-sample Proportion Test (Z test) in Python

A one-sample proportion test is used to determine whether the observed sample proportion of successes significantly differs from a hypothesized population proportion.

In Python, you can use proportions_ztest function from statsmodels package to perform a one-sample proportion Z test.

The basic syntax for proportions_ztest for one-sample proportion Z test:

# import package
import statsmodels.api as sm
stat, pval = sm.stats.proportions_ztest(count, nobs, value)

Where, count is number of successes, nobs is a number of observations, and value is a hypothesized population proportion. The proportion should be provided as float value between 0 to 1.

The following example demonstrate how to perform a one-sample proportion Z test on a sample dataset.

Sample dataset

A company wanted to test the impact of marketing on a product sale. The current sale is 60%. A survey was completed on 500 individuals and 350 individuals purchased the product. The company wanted to check whether there is enough evidence to show that the proportion of sales of products is different than the current sales.

The number of successes (product sales) is 350 out of 500.

# number of successes
count = 350

# total number of observations
nobs = 500

# hypothesized population proportion
value = 0.6

Hypothesis

We will test the following Null and Alternative hypothesis.

Null Hypothesis (H0): Sample proportion is equal to the hypothesized population proportion of 60% (two-tailed)

Alternative Hypothesis (Ha): Sample proportion is not equal to the hypothesized population proportion of 60%

One-sample proportion Z test

Now, perform the one-sample proportion Z test using proportions_ztest function from statsmodels package.

For a one-sample proportion Z test, there are a total of 500 observations, 350 successes (product sales), and 0.6 is the hypothesized population proportion.

# import package
import statsmodels.api as sm

# perform one-sample proportion Z test
stat, pval = sm.stats.proportions_ztest(count=350, nobs=500, value=0.6)

print(stat, pval)

# output
4.879500364742665 1.0635492679527416e-06

The Z Statistic: 4.87; and p value: < 0.05

As the p value (< 0.05) is less than the significance alpha level (0.05), we reject the null hypothesis.

We conclude that the sample proportion of product sales from the market survey is significantly different from 0.6 (60%).

If you want to compare proportions between two different groups, please read our article on the two-sample proportion test using the chi-squared test.