A z-score puts a value on a familiar shared scale: its distance from the mean, measured in standard deviations. Positive scores sit above the mean, negative scores below it, and zero lands exactly on the mean. This makes values from a normal distribution easier to describe and compare than raw units alone. section 4.10
To calculate one, subtract the mean from the observation and divide by the standard deviation. For coffee recommendations with a mean of 27.6 grams and standard deviation of 3.2 grams, a 40-gram recommendation has a z-score of 3.875. Since roughly 99.7% of normally distributed data lie within three standard deviations of the mean, that recommendation is unusually high. section 4.10
Standardize a whole dataset
Apply the same conversion to every observation to transform data from a normal distribution into the standard normal distribution, whose mean is 0 and standard deviation is 1. A z-score can also be converted back into the original value when the mean and standard deviation are known. section 4.10
Z-values also support inference. A chosen confidence level corresponds to central area in the standard normal distribution; its Z-value helps calculate a confidence interval’s margin of error. Higher confidence uses a larger Z-value and produces a wider interval when the sample conditions stay fixed. section 5.4
In a Z-test, a test Z-value standardizes how far a sample mean lies from the population mean after accounting for sample size. Compare it with critical Z-values or use its tail area to decide whether the sample supplies enough evidence to reject a null hypothesis. Z-tests have assumptions: population variance is strictly required, sample variance may be used as an approximation, and t-tests are often preferred when uncertainty is greater. section 6.6 section 6.3
from math import sqrt #1
from scipy.stats import norm #1
mu = 142.0 #2
n = 41 #3
x_bar = 140.2 #4
s = 4.79 #5
alpha = 0.05 #6
z_stat = (x_bar - mu) / (s / sqrt(n)) #7
critical_z = norm.ppf(1 - (alpha / 2)) #8
print("Population mean: ", mu) #9
print("Sample mean: ", x_bar) #9
print("Sample standard deviation: ", s) #9
print("Sample size: ", n) #9
print("Level of confidence: ", 1 - alpha) #9
print("Level of significance: ", alpha) #9
print("z-statistic: ", z_stat) #9
print("Critical value: ", critical_z) #9
if -critical_z <= z_stat <= critical_z: #10
print("Decision: Fail to reject the null hypothesis. "
"The sample does not provide enough evidence the mean is different.") #11
else:
print("Decision: Reject the null hypothesis. "
"The sample provides enough evidence the mean is different.") #11
"""
Population mean: 142.0
Sample mean: 140.2
Sample standard deviation: 4.79
Sample size: 41
Level of confidence: 0.95
Level of significance: 0.05
z-statistic: -2.4061844733568267
Critical value: 1.959963984540054
Decision: Reject the null hypothesis. The sample provides enough evidence the
mean is different
"""
Inspect a two-tailed Python Z-test that calculates a test statistic, finds a critical value with SciPy, and reports a reject-or-fail-to-reject decision. section 6.6







