Sampling and Estimation
Seeded Sample Mean
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.
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)
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]samplen ← 6
1sample = [4, 7, 2, 9, 5, 9]2n = len(sample)3total = 0.0values this step6ntotal ← 0.0
2n = len(sample)3total = 0.04for v in sample:values this step0.0totalv ← 4
3total = 0.04for v in sample:5 total = total + vvalues this step4vtotal ← 4.0
4for v in sample:5 total = total + v6mean = total / nvalues this step0.0 → 4.0totalv ← 7
3total = 0.04for v in sample:5 total = total + vvalues this step4 → 7vtotal ← 11.0
4for v in sample:5 total = total + v6mean = total / nvalues this step4.0 → 11.0totalv ← 2
3total = 0.04for v in sample:5 total = total + vvalues this step7 → 2vtotal ← 13.0
4for v in sample:5 total = total + v6mean = total / nvalues this step11.0 → 13.0totalv ← 9
3total = 0.04for v in sample:5 total = total + vvalues this step2 → 9vtotal ← 22.0
4for v in sample:5 total = total + v6mean = total / nvalues this step13.0 → 22.0totalv ← 5
3total = 0.04for v in sample:5 total = total + vvalues this step9 → 5vtotal ← 27.0
4for v in sample:5 total = total + v6mean = total / nvalues this step22.0 → 27.0totalv ← 9
3total = 0.04for v in sample:5 total = total + vvalues this step5 → 9vtotal ← 36.0
4for v in sample:5 total = total + v6mean = total / nvalues this step27.0 → 36.0totalfor v in sample:
3total = 0.04for v in sample:5 total = total + vmean ← 6.0
5 total = total + v6mean = total / n7print('RESULT:', mean)values this step6.0meanstdout ← 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.
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
randomand 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.