From averages to updates

A compact math-and-code example

Quarto Talks example

2026-01-15

The mean is a balancing point

Every observation pulls; the residuals sum to zero.

One definition

\bar{x}_n = \frac{1}{n}\sum_{i=1}^{n} x_i

The update form

\bar{x}_n = \bar{x}_{n-1} + \frac{x_n - \bar{x}_{n-1}}{n}

The new observation changes the old mean by a shrinking fraction of its surprise.

From notation to implementation

Translate the equation directly

def running_mean(values):
    """Yield the mean after each observed value."""
    mean = 0.0
    for n, value in enumerate(values, start=1):
        mean += (value - mean) / n
        yield mean

Readability is part of correctness

If the code only fits when microscopic, show less code.

The two forms answer different needs

Form Makes visible Useful when
Sum All observations contribute equally Explaining the estimand
Update One state changes at a time Streaming or teaching

Preserve the invariant

After each update, what quantity should equal the mean of all values seen so far?

End at the idea

Good code can reveal the structure already present in the mathematics.