Contents

Calculate One-sample Proportions Confidence Interval in Python

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

proportions_ztest function calculates the Z Statistic for a one-sample proportion test to compare sample proportion with hypothesized population proportion.

However, the proportions_ztest function does not have built-in methods for reporting confidence intervals.

But you can calculate the confidence interval for one-sample proportion using the proportion_confint function from the statsmodels package.

The following example explains calculating the confidence interval for one-sample proportion.

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

Proportion test using proportions_ztest

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

# 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 level alpha (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%).

As you can see from the output, the proportions_ztest function does not report the confidence interval.

Calculate confidence interval

You can calculate the confidence interval one-sample proportion test using a normal approximation (Wald confidence interval) for proportions.

The proportion_confint function from statsmodels can be used for calculating the confidence interval.

We will use the normal method which considers asymptotic normal approximation (Z test) for calculating the confidence interval.

# import package
import statsmodels.api as sm

# calculate Wald confidence interval
sm.stats.proportion_confint(count=350, nobs=500, method='normal')

# output
(0.6598326910947281, 0.7401673089052718)

The 95% confidence interval for one-sample proportion is 0.65 to 0.74. It means that the true population probability of success will likely fall between 0.65 to 0.74 if you performed the survey multiple times.

If you want to calculate the confidence interval for two-sample proportions, please read our article on the confidence interval for two-sample proportions.