Syllabus Lesson 75 of 141 · Odd limits
Odd limits

One pass only

Why it matters Sensors, logs and network taps produce data you see once. "Read it into a list first" is not an option, and the textbook formula for spread quietly breaks on the kind of numbers real systems produce.

A gateway on a factory floor watches a stream of readings and reports their count, mean, spread, minimum and maximum. It has a few kilobytes free and cannot rewind the stream. Some sensors report raw counters around one billion that only wobble by a few units.

Sample standard deviation: sqrt(sum((x - mean)^2) / (n - 1)), the usual "stdev". Written that way it needs the mean first, which means a second pass.

A famous one-pass shortcut uses the sum and the sum of squares. Try it on numbers near one billion before you trust it: subtracting two huge, nearly equal numbers leaves mostly rounding error.

Your turn

Write stats(tape). tape is a Tape of floats. Return a dict with count, mean, stdev (sample, divide by n - 1; 0 for a single reading), min and max. With no readings: count 0 and None for the rest. Limits: read the tape once (20,000 reads for 20,000 readings); use at most 16,000 bytes of working memory (measured with tracemalloc); stdev must be within 1 part in a million of the true value, including for readings around 1,000,000,000 with a spread of about 10.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.