Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 57 ยท Hypothesis Testing

The z-Test for Means and Proportions

Run z-tests for a mean with known sigma, for one proportion and for two proportions: test statistic, p-value, critical regions, one-tailed versus two-tailed, with Python.

The z-test is the simplest hypothesis test. It compares a sample statistic with a claimed value by measuring the gap in standard-error units, then reads the p-value from the standard normal curve. It is used when the sampling distribution is (approximately) normal and the standard error is known or well approximated: a mean with known sigma, or large-sample proportions.

The z statistic

For every z-test the shape is the same:

TEXT
z = (estimate - value claimed by H0) / standard error under H0

A z near 0 means the data agree with H0; a large |z| means they do not. Using the standard normal, the p-value depends on the alternative:

  • right-tailed (H1: parameter > value): p = P(Z >= z)
  • left-tailed (H1: parameter < value): p = P(Z <= z)
  • two-tailed (H1: parameter != value): p = 2 x P(Z >= |z|)

The critical value approach is equivalent: for a two-tailed test at alpha = 0.05 reject when |z| > 1.96 (one-tailed: z > 1.645 or z < -1.645).

One-sample z-test for a mean

Use when the population sigma is known (for example, from long production history) and the data are normal or n is large.

TEXT
z = (x-bar - mu0) / (sigma / sqrt(n))

Example: a filling machine is set to 500 ml with a known sigma = 6. A sample of 36 bottles averages 498.2 ml. Is the machine off target?

Python One-sample z-test

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Since z = -1.8, the two-tailed p-value (0.072) exceeds 0.05, so the data do not provide enough evidence at the 5% level; the one-tailed p-value (0.036) would, which shows why the choice of alternative must be made before looking at the data.

One-sample z-test for a proportion

To test H0: p = p0, compute the standard error from the claimed value p0 (not from p-hat), because the test assumes H0 is true:

TEXT
z = (p-hat - p0) / sqrt(p0 (1 - p0) / n)

Conditions: n p0 >= 10 and n (1 - p0) >= 10.

Python Is the defect rate above 4%?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both tests point the same way, but the z approximation (0.011) is noticeably smaller than the exact p-value (0.020) because the count is discrete and the expected number of defects is only 20. For small or borderline cases prefer the exact test.

Two-sample z-test for proportions

To compare two groups, H0: p1 = p2. Under H0 both groups share one common proportion, estimated by pooling:

TEXT
p-pooled = (x1 + x2) / (n1 + n2)
z = (p-hat1 - p-hat2) / sqrt(p-pooled (1 - p-pooled) (1/n1 + 1/n2))
Python Comparing two conversion rates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the test uses the pooled standard error (because H0 says the groups are the same), while the confidence interval for the difference uses unpooled errors.

Two-sample z-test for means

With known standard deviations sigma1, sigma2 (or very large samples where s1, s2 serve as good estimates):

TEXT
z = (x-bar1 - x-bar2) / sqrt(sigma1^2 / n1 + sigma2^2 / n2)
Python Two-sample z for means with large samples

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When the sample is small and sigma unknown, switch to the t-test.

When to use which test

flowchart TD
    A["What are you testing?"] --> B{"Means or proportions?"}
    B -- "Proportions" --> C["z-test for proportions (large n)"]
    B -- "Means" --> D{"Is sigma known or n very large?"}
    D -- "Yes" --> E["z-test"]
    D -- "No" --> F["t-test"]

A quick guide to choosing between z and t.

In real work sigma is almost never known, so for means the t-test is the usual choice (with big samples it matches the z-test). The z-test for proportions, however, is used all the time.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A two-tailed z-test gives z = 2.5. What is the approximate p-value?

    P(Z >= 2.5) is about 0.0062; doubling for two tails gives about 0.0124.
  2. 2. In a one-sample proportion test, the standard error uses:

    The test assumes H0 is true, so the standard error is computed with p0.
  3. 3. Why do we pool in a two-proportion z-test?

    Under H0 the groups share one proportion, best estimated from all the data.
  4. 4. At alpha = 0.05, the two-tailed critical z values are:

    2.5% lies in each tail beyond +/-1.96.

Summary

  • z = (estimate - claimed value) / standard error under H0; p-values come from the standard normal.
  • Choose left, right or two-tailed alternatives before seeing the data.
  • Proportion tests use p0 for the one-sample standard error and a pooled value for two samples.
  • For means with unknown sigma, use a t-test instead.

Next: t-Tests