Z-Test

A z-test is a hypothesis test whose statistic is compared with the standard normal distribution. The statistic is an estimate minus its value under the null hypothesis, divided by a standard error that is treated as known. For a mean, with \(\bar{Y}\) the sample mean of \(N\) values, \(\mu_0\) the hypothesized mean, and \(\sigma\) the population standard deviation, it is \(z = (\bar{Y} - \mu_0)/(\sigma/\sqrt{N})\): the number of standard errors between what was observed and what the null hypothesis said. The statistic has exactly that distribution when the observations are normally distributed and \(\sigma\) is known; for other distributions it is an approximation that improves with sample size through the central limit theorem, and the same form is also used for large-sample tests whose standard error is estimated but consistent, which is why the name covers more than the known-variance case. The NIST handbook gives the known-variance form for the case where the standard deviation is known and adds a caution that is easy to skip: “If the standard deviation is assumed known for the purpose of this test, this assumption should be checked by a test of hypothesis for the standard deviation”. A historical \(\sigma\) is an assumption to be justified, and a single test that fails to reject it is support for it rather than proof. When the standard deviation has to be estimated from the same sample, which the handbook calls the more typical case, the ratio is a t-statistic and the reference distribution is Student’s t; that is the t-test, of which the z-test is the known-variance special case.

Where a z-test is actually used

A population standard deviation known in advance is rare outside process control with a long, stable history. In practice the z-test is mainly met in a few situations. One is a proportion. If \(N\) items include a fraction \(\hat{p}\) with some property and the null hypothesis says the true fraction is \(p_0\), the standard error under the null is fixed by \(p_0\) itself, so nothing has to be estimated: \(z = (\hat{p} - p_0)\big/\sqrt{p_0(1-p_0)/N}\). The handbook notes that this rests on a normal approximation to the binomial distribution that needs \(N\) to be large, and gives one criterion for large: both \(N p_0\) and \(N(1 - p_0)\) should be at least 5, so a null rate of 0.1 needs at least 50 observations and a null rate of 0.01 at least 500. The comparison of two proportions, the usual form of an A/B test on a conversion rate, applies the same idea with a pooled estimate and has its own entry, the two-proportion z-test.

Another is a large sample of anything. With hundreds of observations the sample standard deviation is usually close to the population value and the t and z answers usually agree, but not always: the two-sided 5 percent critical value is 1.984 for a t distribution with 100 degrees of freedom and 1.960 for the normal, and a statistic that falls between them is rejected by one rule and not the other. Software computes a t-test in that case, which is the exact answer under normality, and the z-test is then an approximation to it rather than a separate procedure. The old rule that a sample above 30 calls for a z-test has it backwards: what decides between z and t is whether the standard deviation is known, and a large sample only makes the choice matter less.

A worked example on a proportion

The handbook’s own example is a one-sided test. After a change to a wafer process, 26 of 200 wafers showed a defect, a rate of 0.13, where the process had historically run at 0.10 or better. The question is whether quality has degraded, so the alternative is one-sided and the statistic is compared with the upper 5 percent point of the normal distribution, 1.645. It comes to \(z = (0.13 - 0.10)\big/\sqrt{0.10 \times 0.90/200} = 1.414\), below 1.645, so the null hypothesis of no degradation is not rejected. The handbook is careful about what that means: “The new process may, indeed, be worse, but more evidence would be needed to reach that conclusion” at that level. Not rejecting is a statement about the evidence, not a finding that the rate is unchanged; the one-sided p-value here is about 0.079.

from statsmodels.stats.proportion import proportions_ztest
# 26 defects in 200; null rate 0.10; alternative: rate is larger
# prop_var=0.10 uses the null rate in the standard error, as in the handbook
stat, p = proportions_ztest(count=26, nobs=200, value=0.10,
                            alternative='larger', prop_var=0.10)
# stat = 1.414..., p = 0.0786...

Without prop_var, statsmodels computes the standard error from the observed proportion instead of the null one, which gives \(z = 1.26\) for the same data. Both are z-tests; they differ in which variance stands in the denominator, and the handbook’s form uses the null rate. The documentation describes the function as equivalent to a mean z-test on data coded 1 for an event and 0 otherwise, which is the link back to the mean formula at the top.

A worked example on a mean, with a known standard deviation

For a mean with a known \(\sigma\) the arithmetic is the same shape. Suppose a measurement process has a long-established standard deviation of 15 and is supposed to average 100, and a sample of 50 readings averages 105. These are illustrative numbers. Then \(z = (105 - 100)/(15/\sqrt{50}) = 2.36\), beyond the two-sided 5 percent critical value of 1.96, and the two-sided p-value is about 0.018. The sample mean is 2.36 standard errors from the hypothesized value, which is further than a true mean of 100 would produce more than about 2 percent of the time.

Reading the result

The decision can be stated either as the statistic against a critical value or as a p-value against the significance level; the two are the same rule. For a two-sided test at 5 percent the critical value is 1.960, for a one-sided test 1.645, and at 1 percent two-sided 2.576, all from the normal table. Which of the two the question calls for is fixed before the data are seen. An upper one-sided test at 5 percent rejects for \(z > 1.645\), a lower one for \(z < -1.645\), and a two-sided test at 10 percent for \(|z| > 1.645\); they share a boundary value but not a rejection region, so a one-sided test is not a two-sided test at twice the level. Beyond the yes or no, the z-value itself is an interpretable number, the distance in standard errors, and the estimate with its confidence interval says how large the departure is, which the test alone does not. The hypothesis testing entry covers the logic and the two kinds of error; statistical power covers how likely the test is to detect a departure of a given size.

SituationTestWhy
Standard deviation known from long historyz-testThe standard error is known; the statistic is exactly normal for normal data and approximately so otherwise
Standard deviation estimated from the samplet-testThe estimated standard error adds variability; t with \(N - 1\) degrees of freedom accounts for it, and approaches z as \(N\) grows
One proportion against a fixed ratez-testThe null rate fixes the standard error; needs \(N p_0\) and \(N(1 - p_0)\) of at least about 5
Two proportions, two groupsTwo-proportion z-testPooled rate under the null; the common A/B test
Small counts of a proportionExact binomial test, or Fisher’s exact test for two groupsThe normal approximation is not good enough

The assumptions are those of any test on a mean or a rate: a random sample from one process, and observations that are independent. Independence is the one that fails most visibly in product data, when several rows come from the same user or the same day. Positive correlation within such groups makes the computed standard error smaller than the data justify; negative correlation would make it larger. The remedy is an analysis that reflects the structure, such as aggregating to the unit the question is about or a cluster-aware standard error, rather than a different test.

References


Discover more from Insightful Data Lab

Subscribe to get the latest posts sent to your email.

Similar Posts

Questions, corrections, or additional insights?

This site uses Akismet to reduce spam. Learn how your comment data is processed.