# Moving Average Formula Confusion

Hi,

why isn’t the last element in series used to compute the moving average in the method defined.

``````forecast = []

for time in range(len(series) - window_size):
forecast.append(series[time:time + window_size].mean())

return np_forecast
``````

If I take the example on a series w/ 5 data points :

series_tmp = np.array([10, 12, 8, 11, 9])
window_size = 2

Using he method defined in the course notebook I would get only 3 elements : [11.0, 10.0, 9.5].

Why don’t we use the following method :

forecast = [ ]
for time in range(len(series_tmp) - window_size + 1):
forecast.append(series_tmp[time:time + 2].mean())

The output would be : [11.0, 10.0, 9.5, 10.0]

Thanks for the explanation

Robin

We compute metrics by comparing predicted values and true values.
When we compute mean of 11 and 9 as the prediction, where’s the corresponding true value?

I don’t understand. The actual value for the prediction i.e moving average between 11 and 9 would be 9.

Here is the vector of predictions:
[MA_0 = (10 + 12)/2, MA_1 = (12 + 8)/2, MA_2 = (8 + 11)/2, MA_3 = (11 + 9)/2]
And here is the vector of true values:
[12, 8, 11, 9]

Am I missing something ?

Let’s clarify my point:

Let i be the index of my series
Let S be my series. So we have the list of index as follows : [0, 1, …, i, … n-1]
len(S) = n

Now, if ii consider the function defined in the notebook :

for i in range (len(S) - window_size (let’s call it ws))
I would get the following index for my moving_averages values MA : 0, 1, …, i, … n-ws-1
len(MA) = n-ws

Let’s take real values like the series I mentioned above :

series_tmp = np.array([10, 12, 8, 11, 9])
window_size = 2
len(MA) = 3
Yet, we could actually obtain 4 values for the moving average, namely:
[MA_0 = (10 + 12)/2, MA_1 = (12 + 8)/2, MA_2 = (8 + 11)/2, MA_3 = (11 + 9)/2]

Why is the function defined so ?