The values of data points near the start and the end of time series, when smoothed with simple moving average I am making a task for a class, where I need to code simple moving average algorithm. In my copybook, I have this written:
$$\bar{y_{i}}=\frac{y_{i-1}+y_{i}+y_{i+1}}{3}$$
for $n=3$ MA case, where near the start and end the values of smoothed time series are:
$$\bar{y_{1}}=\frac{5y_{1}+2y_{2}-y_{3}}{6}$$
$$\bar{y_{n}}=\frac{5y_{n}+2y_{n-1}-y_{n-2}}{6}$$
For $n=5$ MA case it is:
$$\bar{y_{i}}=\frac{y_{i-2}+y_{i-1}+y_{i}+y_{i+1}+y_{i+2}}{5}$$
with near-borders values of:
$$\bar{y_{1}}=\frac{3y_{1}+2y_{2}+y_{3}-y_{4}}{5}$$
$$\bar{y_{2}}=\frac{4y_{1}+3y_{2}+2y_{3}+y_{4}}{10}$$
$$\bar{y_{n}}=\frac{3y_{n}+2y_{n-1}+y_{n-2}-y_{n-3}}{5}$$
$$\bar{y_{n-1}}=\frac{4y_{n}+3y_{n-1}+2y_{n-2}+y_{n-3}}{10}$$
But what would be the formulas to recalculate the near-border values in the general case? Thank you.
 A: You normally apply the MA filter in the border by extending the time series (extrapolating), ie, creating some "phantom" values; but there are lots of ways of doing that. In our case, for $n=3$, we'd compute
$\bar{y_1} = \frac{1}{3}(y_0 + y_1+y_2)$
and equating that with the given formula, we deduce the value of $y_0$ that has been used:
$$\frac{1}{3}(y_0 + y_1+y_2) = \frac{1}{3}(\frac{5}{2}y_1 + y_2  - \frac{1}{2} y_3) \Rightarrow y_0 = y_1 - \frac{y_3 - y_1}{2}$$
which makes sense (it can be interpreted as $y_1$ minus an estimate of the derivative), but there could be other alternatives.
Analogously, for $n=5$ we have
$y_{-1} + y_0 + y_1 + y_2 + y_3 = 3 y_1 + 2 y_2 + y_3 - y_4$
and
$y_0 + y_1 + y_2 + y_3 + y_4 = 2 y_1 + \frac{3}{2} y_2 + y_3 - \frac{1}{2}y_4$
Solving this we get 
$y_0 = y_1 - \frac{y_4-y_2}{2}$ 
$y_{-1}= y_0$
From this we could guess a general formula
A: Tosh, Rather than assume the form for the weighting scheme, develop a suitable ARIMA model which will optimize the number of periods (k) to use and the best weights for each of the k weights. In this way your equation will be fully generalized.
