# How to Perform a One Proportion Z-Test in Python

### Introduction

In the realm of statistical analysis, a one proportion Z-test is a popular method for determining whether a sample proportion differs significantly from a population proportion or a hypothesized proportion. This kind of test is often used in cases where the sample size is large enough to assume that the sampling distribution of the proportion is approximately normally distributed by virtue of the Central Limit Theorem.

The following guide will walk you through how to conduct a one proportion Z-test in Python using the ‘statsmodels’ library. The ‘statsmodels’ library is a powerful Python module built specifically for statistics that can handle a wide variety of statistical tests.

Firstly, it is important to ensure that ‘statsmodels’ and other necessary packages are installed. If you haven’t done so already, you can install these libraries using pip:

pip install statsmodels pandas numpy

### Setting up the Problem

For the purposes of this article, let’s assume a hypothetical scenario. A company claims that 80% of their products are made from recycled materials. As a sustainability analyst, you have collected a random sample of 150 products and found that 105 of them are made from recycled materials. You would like to verify the company’s claim.

### Performing a One Proportion Z-Test

First, we need to import the necessary libraries:

import numpy as np
from statsmodels.stats.proportion import proportions_ztest

In our hypothetical scenario, we have:

• The number of successes (x): 105 (products made from recycled materials)
• The number of observations (n): 150 (total products)
• The null hypothesis proportion (p0): 0.80 (the company’s claim)

The Null Hypothesis (H0) in this case is that the proportion of products made from recycled materials in the population is 0.80, while the Alternative Hypothesis (H1) is that the proportion is not 0.80.

We can now perform the Z-test:

x = 105
n = 150
p0 = 0.80

z_stat, p_value = proportions_ztest(count=x, nobs=n, value=p0, alternative='two-sided')

In the ‘proportions_ztest’ function, we input the count of successes, number of observations, the null hypothesis proportion, and specify that we want a two-sided test (since we’re testing for inequality). This function will return two values: the z-statistic (‘z_stat’) and the p-value (‘p_value’).

### Interpreting Results

Finally, we can print and interpret our results:

print('Z-statistic:', z_stat)
print('P-value:', p_value)

The Z-statistic is a measure of how many standard deviations an element is from the mean. The p-value is the probability that you would have found the current result if the null hypothesis were true.

Generally, in the statistical community, a p-value of less than 0.05 is considered to be statistically significant. So, if the calculated p-value is less than 0.05, we reject the null hypothesis and conclude that the proportion of products made from recycled materials is significantly different from 80%. If the p-value is greater than 0.05, we fail to reject the null hypothesis and conclude that we do not have enough evidence to say the proportion is different from 80%.

### Conclusion

This is a simplified demonstration of performing a one proportion Z-test in Python. In a real-world scenario, you would likely have to account for more complex factors and considerations. However, this example illustrates the core process and the power of Python in statistical testing. Remember, the choice of statistical test depends heavily on the nature of your data and the specific question you are seeking to answer. As always, careful consideration and good data practices are key to deriving valid and useful insights from statistical testing.