Home » How to Perform One Sample & Two Sample Z-Tests in Python

How to Perform One Sample & Two Sample Z-Tests in Python

by Erma Khan

You can use the ztest() function from the statsmodels package to perform one sample and two sample z-tests in Python.

This function uses the following basic syntax:

statsmodels.stats.weightstats.ztest(x1x2=Nonevalue=0)

where:

  • x1: values for the first sample
  • x2: values for the second sample (if performing a two sample z-test)
  • value: mean under the null (in one sample case) or mean difference (in two sample case)

The following examples shows how to use this function in practice.

Example 1: One Sample Z-Test in Python

Suppose the IQ in a certain population is normally distributed with a mean of μ = 100 and standard deviation of σ = 15.

A researcher wants to know if a new drug affects IQ levels, so he recruits 20 patients to try it and records their IQ levels.

The following code shows how to perform a one sample z-test in Python to determine if the new drug causes a significant difference in IQ levels:

from statsmodels.stats.weightstats import ztest as ztest

#enter IQ levels for 20 patients
data = [88, 92, 94, 94, 96, 97, 97, 97, 99, 99,
        105, 109, 109, 109, 110, 112, 112, 113, 114, 115]

#perform one sample z-test
ztest(data, value=100)

(1.5976240527147705, 0.1101266701438426)

The test statistic for the one sample z-test is 1.5976 and the corresponding p-value is 0.1101.

Since this p-value is not less than .05, we do not have sufficient evidence to reject the null hypothesis. In other words, the new drug does not significantly affect IQ level.

Example 2: Two Sample Z-Test in Python

Suppose the IQ levels among individuals in two different cities are known to be normally distributed with known standard deviations.

A researcher wants to know if the mean IQ level between individuals in city A and city B are different, so she selects a simple random sample of  20 individuals from each city and records their IQ levels.

The following code shows how to perform a two sample z-test in Python to determine if the mean IQ level is different between the two cities:

from statsmodels.stats.weightstats import ztest as ztest

#enter IQ levels for 20 individuals from each city
cityA = [82, 84, 85, 89, 91, 91, 92, 94, 99, 99,
         105, 109, 109, 109, 110, 112, 112, 113, 114, 114]

cityB = [90, 91, 91, 91, 95, 95, 99, 99, 108, 109,
         109, 114, 115, 116, 117, 117, 128, 129, 130, 133]

#perform two sample z-test
ztest(cityA, cityB, value=0) 

(-1.9953236073282115, 0.046007596761332065)

The test statistic for the two sample z-test is -1.9953 and the corresponding p-value is 0.0460.

Since this p-value is less than .05, we have sufficient evidence to reject the null hypothesis. In other words, the mean IQ level is significantly different between the two cities.

Additional Resources

The following tutorials explain how to perform other common statistical tests in Python:

How to Conduct a One Sample T-Test in Python
How to Conduct a Two Sample T-Test in Python
How to Conduct a Paired Samples T-Test in Python

Related Posts