Sure, let’s walk through an example of a Z-test.
Imagine we’re working for a company that manufactures light bulbs. The company claims that its light bulbs have an average lifespan of 1,500 hours. We suspect this might not be accurate, so we conduct a test. We take a sample of 50 light bulbs and find that the average lifespan is 1,480 hours with a standard deviation of 100 hours.
Our null hypothesis, , is that the average lifespan of the light bulbs is 1,500 hours, as the company claims. The alternative hypothesis, , is that the average lifespan is not 1,500 hours.
Let’s conduct a Z-test to test our hypothesis. The Z-statistic is calculated as follows:
where:
- is the sample mean (1,480 hours in our case),
- is the population mean (1,500 hours according to the company),
- is the standard deviation of the sample (100 hours),
- is the sample size (50 light bulbs).
We’ll use a significance level of 0.05. If our calculated Z-score is beyond the critical Z-score (approximately ±1.96 for a two-tailed test with ), we’ll reject the null hypothesis.
Let’s calculate.
import math
# given values
sample_mean = 1480 # sample mean
pop_mean = 1500 # population mean
std_dev = 100 # standard deviation
n = 50 # sample size
# calculate z-score
z = (sample_mean - pop_mean) / (std_dev / math.sqrt(n))
z