Estimate a population mean from a fixed sample. The sample is given as a literal list — not generated at runtime — so results are deterministic without relying on RNG agreement between Python and numpy. Loop to accumulate total, divide by n. Library: np.mean(sample) on the same literal.

By hand

With the library

np.mean(sample) computes the same sum-then-divide in one call. Both sides operate on the identical fixed list, so results are bit-for-bit equal.

naive.py
sample = [4, 7, 2, 9, 5, 9]
n = len(sample)
total = 0.0
for v in sample:
    total = total + v
mean = total / n
print('RESULT:', mean)
library.py
import numpy as np
from dalib.display import set_display
set_display()

sample = [4, 7, 2, 9, 5, 9]
n = len(sample)
mean = float(np.mean(sample))
print('n:', n)
print('RESULT:', mean)
n: 6
RESULT: 6.0

Honesty

This lesson shows the computation exactly, on a tiny pinned (seeded) sample. The sample mean is arithmetically correct and reproducible, but a single small draw demonstrates the mechanism, not the population — one sample mean is not a reliable estimate of the true mean and carries real sampling error. Read it as "how a seeded draw and its mean are computed," not as a conclusion about the population; a trustworthy estimate needs an adequate sample size and a sense of the sampling distribution.

Implementation notes

  • The sample mean is a point estimate of the population mean μ. Different samples would give different estimates; the spread of those estimates is the standard error (see standard-error, this chapter).
  • RNG parity rule for this chapter: both naive and library use the same fixed literal list, framed as "a reproducible draw with a fixed seed." Never let the two halves generate their own random draws — Python's random and numpy's RNG produce different sequences even with the same seed.
  • Cross-reference: arithmetic-mean (ch01) for the basic loop that this lesson reuses in a sampling context.