← All simulations · Pillar 7: Patterns in time
Moving average
What it is
Day-to-day numbers are jumpy. An ice-cream stand sells a bit more on a sunny day, a bit less when it rains, so the daily line zig-zags even though the real story underneath changes slowly. A moving average calms the line down: it replaces each day with the average of a small window of nearby days. The random ups and downs cancel out, and the slow pattern shows through.
Go deeper: picture a window that slides along the line. To get the smoothed value for a day, you take that day plus a few on each side and average them. A wider window averages more days, so it cancels more noise — but it also blends together days that belong to different parts of the pattern, so it lags behind turns and flattens real bumps. Too narrow keeps the noise; too wide erases the signal. The best width sits in between.
Why care
Smoothing is everywhere you look at numbers over time. “7-day average” case counts during an outbreak, a stock’s “50-day moving average,” the trend line on your step-counter — all moving averages, used to see past the daily noise to what’s really happening. It’s also the gentlest way to reveal the trend before you try to forecast what comes next.
The idea, intuitively
Slide a little box along the jagged line. Everything inside the box gets averaged into a single, calmer point. Make the box wider and the line gets smoother and smoother — until it gets so wide it starts ignoring the real hump and drifts into a near-flat line. Watch the box, watch the point it makes, and find the width that hugs the real shape without the jitter.
Peek at the data first
Each row is one day and that day’s sales. The numbers swing widely
from day to day — exactly the kind of noisy series a moving average is built to tame, and the
kind of summary Spectra’s describe_data shows before you trust a dataset.
Try it
Slide window width and watch the orange smoothed line iron the jagged grey line flat. Slide the spotlight day to move the shaded window and see exactly which days get averaged into one smoothed point. Tick reveal the hidden real pattern and find the width that hugs it best — not so narrow it stays noisy, not so wide it flattens the hump.
Where it shows up
- Public health. “7-day average” case and hospitalisation counts smooth out weekend reporting dips to show the real direction.
- Finance. Traders watch moving averages of a price to filter daily noise and spot longer-term moves.
- Sensors & fitness. Step counters, heart-rate monitors and thermostats average recent readings so one odd blip doesn’t throw them off.
Where it came from
The moving average grew out of astronomy and actuarial work in the 1800s and early 1900s, where people averaged neighbouring measurements to cancel observation errors. It became a staple of economic statistics and quality control, and the same windowed-average idea now lives inside digital signal processing as a simple low-pass filter — letting the slow part through while blocking the fast jitter.
Try it in code
In the Studio, load a daily dataset and plot it to see the raw zig-zag
— the jumpy line a moving average is meant to smooth:
data = load "weather_town" describe_data data plot_data data, x: "day", y: "temperature"
Check your understanding
- How does averaging a window of days remove the random noise?
- What goes wrong if the window is far too wide?
- Why might a “7-day average” be clearer than the raw daily numbers?