PythiloLesson 8 of 8 · progress kept in this browser

Update a mean without storing all samples

A running average can be updated from its previous value and a count. When the next sample arrives, move the mean by one count-th of the difference between the sample and the old mean. The formula avoids retaining observations and is useful for streams. An empty stream has no average, so its result needs a separate contract.

mean, count = 10.0, 2
count += 1
mean += (16 - mean) / count
print(mean)
Your task

Write running_means(values). Consume an iterable once and return the average after each sample. An empty iterable gives []. Use the incremental update rather than repeatedly summing earlier samples.

All lessons