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

Sample is given: [4, 7, 2, 9, 5, 9], n=6. Accumulate total=36 over six iterations, then mean = 36/6 = 6.0.

naive.py
Replay: real traced execution (multi-file project)
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)
  1. sample ← [4, 7, 2, 9, 5, 9]

    1sample = [4, 7, 2, 9, 5, 9]2n = len(sample)
    values this step[4, 7, 2, 9, 5, 9]sample
  2. n ← 6

    1sample = [4, 7, 2, 9, 5, 9]2n = len(sample)3total = 0.0
    values this step6n
  3. total ← 0.0

    2n = len(sample)3total = 0.04for v in sample:
    values this step0.0total
  4. v ← 4

    3total = 0.04for v in sample:5    total = total + v
    values this step4v
  5. total ← 4.0

    4for v in sample:5    total = total + v6mean = total / n
    values this step0.0 4.0total
  6. v ← 7

    3total = 0.04for v in sample:5    total = total + v
    values this step4 7v
  7. total ← 11.0

    4for v in sample:5    total = total + v6mean = total / n
    values this step4.0 11.0total
  8. v ← 2

    3total = 0.04for v in sample:5    total = total + v
    values this step7 2v
  9. total ← 13.0

    4for v in sample:5    total = total + v6mean = total / n
    values this step11.0 13.0total
  10. v ← 9

    3total = 0.04for v in sample:5    total = total + v
    values this step2 9v
  11. total ← 22.0

    4for v in sample:5    total = total + v6mean = total / n
    values this step13.0 22.0total
  12. v ← 5

    3total = 0.04for v in sample:5    total = total + v
    values this step9 5v
  13. total ← 27.0

    4for v in sample:5    total = total + v6mean = total / n
    values this step22.0 27.0total
  14. v ← 9

    3total = 0.04for v in sample:5    total = total + v
    values this step5 9v
  15. total ← 36.0

    4for v in sample:5    total = total + v6mean = total / n
    values this step27.0 36.0total
  16. for v in sample:

    3total = 0.04for v in sample:5    total = total + v
  17. mean ← 6.0

    5    total = total + v6mean = total / n7print('RESULT:', mean)
    values this step6.0mean
  18. stdout ← RESULT: 6.0

    6mean = total / n7print('RESULT:', mean)
    values this stepRESULT: 6.0stdout

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.

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.