Skip to content

BCompare groups · Guide test 007

Two sample z test for means

Compares independent population means using a normal reference distribution and appropriate standard errors.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

In Two-sample t-test (Welch / Student, or z test), set Known population SD, first group (z test) or set Known population SD, second group.

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · Two-sample z test

30
30
0.50
1.0
1.0
0.050

Alternative

The statistic's distribution if the design's true effect is real.

Your data

Drag a point or use ↑/↓ to change it
Group 1 · n = 30 · mean 0.62Group 2 · n = 30 · mean 0.05

If H₀ were true

z = 2.17
  1. The reference: how z varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .030).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

z=xˉ1−xˉ2σ12/n1+σ22/n2z = \dfrac{\bar x_1 - \bar x_2}{\sqrt{\sigma_1^2/n_1 + \sigma_2^2/n_2}}

With your numbers

z=0.62−0.0512/30+12/30=2.17z = \dfrac{0.62 - 0.05}{\sqrt{1^2/30 + 1^2/30}} = 2.17
xˉ1,xˉ2\bar x_1, \bar x_2
the two sample means
σ1,σ2\sigma_1, \sigma_2
the known population SDs
n1,n2n_1, n_2
the group sizes

The verdict

z = 2.17, p = .030

p ≤ α = .050: reject H₀

The difference in means divided by its exact standard error. With estimated SDs, use Welch's t test.

Statistic
2.171
p-value
0.0299
Two-sample z test on Group 1: n = 30; Group 2: n = 30: z = 2.17, p = .030. The p-value is below α = .050, so the data are hard to reconcile with H0.

Two-sample z test on Group 1: n = 30; Group 2: n = 30: z = 2.17, p = .030. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: μ1−μ2=Δ0; H1: the difference is not Δ0 or lies on a prespecified side.

Assumptions

The groups are independent, and observations within groups are independent. Population variances are known, or a sufficiently accurate large sample normal approximation is justified.

Formula

z=xˉ1−xˉ2−Δ0σ12/n1+σ22/n2z=\frac{\bar x_1-\bar x_2-\Delta_0}{\sqrt{\sigma_1^2/n_1+\sigma_2^2/n_2}}

Parameters and notation

n1,n2 are group sizes; σ1²,σ2² are population variances; Δ0 is often zero. Estimated variances give an asymptotic rather than exact z test.

Structure of the method

Form the difference of two independent sample means and use the sum of their mean variances in the denominator. The standardized contrast is normal exactly for independent normal populations with known variances, and approximately under suitable separate-group central-limit conditions when estimated standard errors are substituted.

Estimated and nuisance parameters

The two means are estimated; known population variances are external inputs. The null difference may be nonzero and represents the scientific comparison. If variances are estimated within groups, distinguish their sampling uncertainty from genuine fixed calibration constants and prefer an appropriate Welch reference when feasible.

Settings and design choices

Choose the comparison direction, null difference, variance sources, group allocation, alpha, and multiplicity family before analysis. Allocation affects power when variances differ. Independence versus pairing is a design property, not a software option to select by whichever test gives the smaller p-value.

Fitting and adaptation

Recompute both group summaries on the intended population samples and refit any justified variance model after changes in measurement scale. Matched or repeated observations require the covariance of the mean difference, usually through paired differences or a joint model, rather than this independent-groups formula.

Sensitivity and robustness

A few highly variable observations in the smaller group can dominate uncertainty. Unequal sample sizes do not invalidate the known-variance formula, but inaccurate historical variances do. Dependence, heterogeneity within a group, or outcome-dependent sampling can invalidate the nominal normal tail even with large total sample size.

How to use and calibrate

Compute the difference and its standard error, then use the normal tail matching the alternative. Account for covariance if samples are paired; the displayed denominator is for independent groups.

Example application

Compare average output from two independent production processes when their long run variances are reliably known.

Interpretation and pitfalls

Watch out

Do not pool paired observations into this formula. When variances are estimated, Welch’s t procedure is usually more appropriate and approaches this test as degrees of freedom increase.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.

Python and R

Independent groups with known population standard deviations.

Python · native

Validated — Executed successfully in the validation environment · needs numpy, scipy

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
x = np.array([4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0])
y = np.array([4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9])
s1=1; s2=1.2
z=(x.mean()-y.mean())/np.sqrt(s1*s1/len(x)+s2*s2/len(y))
print(z,2*stats.norm.sf(abs(z)))
Output
1.2146644952683714 0.22449407931846876
R

Validated — Executed successfully in the validation environment · needs Base/standard library

x <- c(4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0)
y <- c(4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9)
s1 <- 1; s2 <- 1.2; z <- (mean(x)-mean(y))/sqrt(s1^2/length(x)+s2^2/length(y))
print(c(z=z,p=2*pnorm(-abs(z))))
Output
        z         p 
1.2146645 0.2244941

Sources

Statistics guide v4 (historical):
R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 007, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT007.