rolling_mean

transforms.rolling_mean(values, window)

Compute a rolling (moving) average over a sequence of values.

Parameters

Name Type Description Default
values Iterable[float] Input sequence to smooth. required
window int Number of points to average over. Must be positive. required

Returns

Name Type Description
list[float] Smoothed sequence with the same length as input.

Notes

This function uses mode="same" convolution, which means:

  • Output has the same length as input
  • Edge values (first and last window//2 points) are averaged over fewer points than the window size, using only available data
  • This prevents shrinkage but means edges are less smoothed

For example, with window=5, the first point averages over just itself plus the next 2 points, while interior points average over 5 points centered on the current position.

Examples

>>> scores = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> smoothed = rolling_mean(scores, window=3)
>>> # First value: avg of [1, 2] (only 2 points available)
>>> # Fifth value: avg of [4, 5, 6] (full 3-point window)