Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 40 ยท Getting Started

Moving Averages and Rolling Windows

Smooth noisy series with simple, centered and weighted moving averages, and compute rolling statistics, expanding windows and exponentially weighted means with pandas.

A raw time series is usually jagged. Day-to-day noise hides the slower movement you care about. The simplest remedy is a moving average: replace each value by the average of the values around it. The same sliding-window idea gives you rolling standard deviations, rolling minimums and many features used later for machine learning.

pandas supports all of this through .rolling(), .expanding() and .ewm(). This lesson shows how each works, what the window, center and min_periods options really do, and when a moving average helps or misleads.

The simple moving average

A moving average of window k at time t is the mean of the k most recent values, including the current one:

MA(t) = (y(t) + y(t-1) + ... + y(t-k+1)) / k

Python Rolling mean with different windows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first window - 1 rows are NaN because there is not enough history yet. A longer window removes more noise but reacts more slowly: the 21-day average is much smoother than the 3-day one, and it also lags behind turning points.

Trailing versus centered windows

By default the window trails: it uses only the past, so it is safe in forecasting settings. Setting center=True puts the window symmetrically around each point. That gives a better estimate of the trend at time t, but it uses future values, so it is allowed only for description and decomposition, never as a feature for predicting the future.

Python Trailing vs centered

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For a straight line the centered average equals the original value, while the trailing one is shifted by one step: it lags behind. A trailing window of size k lags by about (k - 1) / 2 steps.

Using center=True for a model feature leaks future information. Backtests then look great and the live model fails.

Handling the edges: min_periods

Use min_periods to get values earlier, computed from whatever is available:

Python
y.rolling(7, min_periods=1).mean()   # first value = y[0], second = mean of two, ...

Early values are based on fewer points and are noisier, so they are less comparable with later values. Use this when losing the first rows would hurt more than a slightly uneven start.

Even windows and the 2 x m moving average

A centered window needs an odd length to sit exactly on one observation. For an even period, such as 12 months, classical decomposition uses a 2 x 12 moving average: a 12-term average followed by a 2-term average, which re-centers the result. Its weights are 1/24 at the two ends and 1/12 in the middle. Classical decomposition uses exactly this trick to extract a trend that contains no seasonal swing.

Python A moving average over one full season removes the season

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Averaging over exactly one full season cancels the seasonal wave, so the moving average recovers the straight trend almost perfectly.

Other rolling statistics

rolling() works with many aggregations, not just the mean:

Method Use
.std() rolling volatility
.min(), .max() recent range, channel breakouts
.median() smoothing that ignores outliers
.sum() totals over the last k periods
.quantile(q) rolling percentiles
.apply(func) any custom function
.corr(other) rolling correlation between two series

Time-based windows work with a datetime index: y.rolling("7D") averages whatever falls into the last 7 calendar days, even if some days are missing. That is safer than a count-based window when observations are irregular.

Weighted and exponentially weighted averages

A simple average gives all points in the window the same weight. Often recent points matter more. Exponentially weighted means give weights that decay geometrically with age and never fully drop to zero:

Python Exponentially weighted vs simple average

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The exponential average reacts immediately and then approaches the new level smoothly, while the simple average needs a full window to catch up. The parameter alpha between 0 and 1 controls the speed: larger values react faster. This is the building block of simple exponential smoothing.

Expanding windows

.expanding() uses all data from the start up to t. It gives running statistics such as the cumulative mean, running maximum or the "so far" standard deviation:

Python
y.expanding().mean()
y.expanding().max()

Moving average as a forecast, and its limits

The last trailing average can serve as a quick forecast for a flat series. Remember the limits:

  • It lags behind trends, so forecasts on trending data are biased.
  • It ignores seasonality unless the window covers whole periods.
  • The choice of window is a bias versus noise trade-off; there is no "right" size without a goal.
  • It can create fake cycles (the Slutsky-Yule effect): smoothing pure random noise produces gentle waves that look like real patterns.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a trailing rolling window of size 7 return for the first six rows by default?

    min_periods defaults to the window size, so rows with fewer than 7 observations are NaN.
  2. 2. Why is a centered moving average unsuitable as a feature in a forecasting model?

    Centering includes observations after time t, which would not be known when predicting.
  3. 3. You average a monthly series over a window of exactly 12 months. What happens to a yearly seasonal pattern?

    A window covering one full season contains every phase of the wave once, so the swings cancel.
  4. 4. Compared with a simple moving average, an exponentially weighted average:

    Weights decay geometrically with age, so the latest observations dominate.

Summary

  • A moving average replaces each value by the mean of a sliding window; longer windows smooth more and lag more.
  • rolling(k) trails; center=True is for description only; min_periods controls the start.
  • A window equal to the seasonal period removes the season, which is how classical decomposition finds the trend.
  • ewm weights recent data more; expanding uses everything so far.
  • Smoothing can invent cycles, so never mistake a smoothed wave for a real one.

Next: Trend, Seasonality and Noise