The standard error (SE) of the mean measures how much the sample mean varies across repeated samples: SE = s/√n, where s is the sample standard deviation (ddof=1). Two loops: mean, then sample std; divide by √n. Library: scipy.stats.sem(sample) — defaults to ddof=1 (sample std), matching the naive. Same fixed sample as seeded-sample-mean.

By hand

With the library

scipy.stats.sem(sample) uses ddof=1 by default — the same denominator as numpy.std(ddof=1). The snapshot shows s separately so the two-step structure is visible.

naive.py
import math
sample = [4, 7, 2, 9, 5, 9]
n = len(sample)
total = 0.0
for v in sample:
    total = total + v
mean = total / n
sq_diff = 0.0
for v in sample:
    sq_diff = sq_diff + (v - mean) ** 2
s = math.sqrt(sq_diff / (n - 1))
se = s / math.sqrt(n)
print('RESULT:', round(se, 6))
library.py
import numpy as np
from scipy.stats import sem
from dalib.display import set_display
set_display()

sample = [4, 7, 2, 9, 5, 9]
n = len(sample)
s = float(np.std(sample, ddof=1))
se = float(sem(sample))
print('n:', n)
print('s (ddof=1):', round(s, 6))
print('RESULT:', round(se, 6))
n: 6
s (ddof=1): 2.828427
RESULT: 1.154701

Honesty

This lesson shows the computation of the standard error exactly, on a tiny pinned sample. The value is arithmetically correct and reproducible, but computing SE from a handful of points demonstrates the formula, not a dependable estimate of precision — with so few observations the SE itself is unstable and rests on assumptions (independent, identically distributed draws). Read it as "how SE = s/√n is computed," not as a trustworthy margin; real precision claims need an adequate sample size and assumption checks.

Implementation notes

  • SE = s/√n: larger n → smaller SE → more precise estimate. Quadrupling the sample size halves the SE (square-root law).
  • ddof=1 trap: scipy.stats.sem defaults to ddof=1; using np.std (ddof=0 default) for s and then dividing by √n would give a slightly different (biased) result. Always match ddof on both sides.
  • Cross-reference: standard-deviation (ch02) for the s computation; seeded-sample-mean (this chapter) for the mean used here.