Loading the problem
Before you start
Smooth a series with a simple moving average.
Each output value is the mean of window consecutive input values. Only full
windows count, so a series of length n gives n - window + 1 values. Output
j is the mean of x[j], x[j + 1], ..., x[j + window - 1].
A moving average removes short, random wiggles and keeps the slower trend. It is also the "MA" smoothing step behind many classical forecasting methods.
def moving_average(x, window):
Input
moving_average(np.array([1.0, 2.0, 3.0, 4.0, 5.0]), window=3)
Output
array([2., 3., 4.])
Reasoning
The three full windows are [1, 2, 3], [2, 3, 4] and [3, 4, 5]. Their means
are 2, 3 and 4.
1 <= window. If window > len(x), return an empty array.O(n) with a cumulative sum rather than O(n * window).Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell