BCompare groups · Guide test 006
One sample z test for a mean
Compares a population mean with a target using known variance or a justified large sample standard error.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- One-sample t-test (or z test) — Does the mean differ from a known or hypothesized value?
In One-sample t-test (or z test), set Known population SD (z test).
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 · One-sample z test
The population mean the data come from.
The value H₀ says the mean equals.
Alternative
The statistic's distribution if the design's true effect is real.
Your data
Drag a point or use ↑/↓ to change itIf H₀ were true
- The reference: how z varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .091).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- the sample mean
- the value H₀ claims
- the population SD, known in advance (rarely true in practice)
- the sample size
The verdict
z = 1.69, p = .091, d = 0.34
p > α = .050: compatible with H₀
Effect size: standardized difference d = 0.338
z counts how many standard errors the sample mean sits from μ0. Knowing σ is the whole point: estimate it from the data and you need the t test instead.
- Statistic
- 1.692
- p-value
- 0.0906
One-sample z test on Sample: n = 25: z = 1.69, p = .091, d = 0.34. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0: μ=μ0; H1: μ≠μ0, μ>μ0, or μ<μ0.
Assumptions
Observations are independent with a common mean. Known population variance and normal observations give exact inference; a suitable central limit theorem gives an approximation for large samples.
Formula
Parameters and notation
n is the sample size; x̄ is its mean; μ0 is the target; σ is known population standard deviation. Using an estimated standard error makes this a large sample z procedure.
Structure of the method
Compute the sample mean, subtract the specified target, and divide by the known population standard deviation over square-root sample size. A normal population gives an exact standard-normal pivot; otherwise the same calculation needs a justified central-limit approximation for the actual sampling process.
Estimated and nuisance parameters
The sample mean estimates the target population mean. In the genuinely known-variance version, sigma is supplied independently and is not re-estimated from these observations. If the standard error is estimated, that estimation uncertainty changes the claim from an exact known-sigma test to an approximation.
Settings and design choices
Specify the target mean, sigma source, one- or two-sided alternative, alpha, sampling plan, and any sequential monitoring rule. There is no optimization or neural architecture. Sample size is a design choice affecting power; it should not be increased repeatedly until significance without a valid sequential protocol.
Fitting and adaptation
Recalculate the mean and standard error for a new independent sample while preserving the prespecified target. If the reference variance drifts or is estimated from a small historical dataset, incorporate that uncertainty or use an appropriate t/model-based method rather than retaining an unjustified known-sigma claim.
Sensitivity and robustness
Heavy tails, dependence, survey weights, and unequal inclusion probabilities can defeat the simple sigma/square-root-n denominator. A sample-size rule such as n greater than thirty is not a distributional guarantee. Report the mean difference in physical units and sensitivity to the assumed sigma.
How to use and calibrate
Compute z and compare with a standard normal distribution. For a two sided test p=2Φ(−|z|). Report x̄−μ0 and a confidence interval.
Example application
With n=100, known σ=10, x̄=52, and μ0=50, z=2 and the two sided p value is about 0.0455.
Interpretation and pitfalls
Watch out
A standard deviation estimated from a small sample should usually lead to a t test. Independence and representative sampling matter more than a mechanical “n greater than 30” rule.
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
Population standard deviation sigma is known; estimating it from x changes the test.
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])
sigma=1; mu0=5
z=(x.mean()-mu0)/(sigma/np.sqrt(len(x)))
print(z,2*stats.norm.sf(abs(z)))
Output
0.9486832980505132 0.34278171114791167
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)
sigma <- 1; mu0 <- 5; z <- (mean(x)-mu0)/(sigma/sqrt(length(x)))
print(c(z=z,p=2*pnorm(-abs(z))))
Output
z p 0.9486833 0.3427817
Sources
- statsmodels z testsstatsmodels.org
- statsmodels.stats.weightstats.zteststatsmodels.org
- 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 006, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT006.