# Weights of simple moving average are not adding up to one

This is the definition of linear filter from a book I am reading:

A second procedure for dealing with a trend is to use a linear filter, which converts one time series, $$\{x_t\}$$, into another, $$\{y_t\}$$, by the linear operation $$y_t = \sum_{r = -q}^{+s} a_r x_{t+r}$$ where $$\{a_r\}$$ is a set of weights. In order to smooth out local fluctuations and estimate the local mean, we should clearly choose the weights so that $$\sum a_r = 1$$, and then the operation is often referred to as a moving average. Moving averages are discussed in detail by Kendall et al. (1983, Chapter 46), and we will only provide a brief introduction. Moving averages are often symmetric with $$s = q$$ and $$a_j = a_{-j}$$. The simplest example of a symmetric smoothing filter is the simple moving average, for which $$a_r = 1/(2q + 1)$$ for $$r = -q, \ldots, +q$$, and the smoothed value of $$x_t$$ is given by $$\textrm{Sm}(x_t) = \frac{1}{2q + 1}\sum_{r=-q}^{+q} x_{t+r}$$

It is said there that $$\{a_r\}$$ is a set of weights and in order to call the operation a moving average we should clearly choose the weights so sum of $$a_r$$ is equal to 1.

Then it is described that the moving averages are often symmetric with $$s = q$$ and $$a_j = a_{-j}$$. So the simple moving average for which $$a_r = 1/(2q + 1)$$ for $$r = -q, \ldots, +q$$ is $$\textrm{Sm}(x_t)$$

But when I tried to confirm that the sum of $$a_r$$ for simple moving average equal 1 I got this:

Is there something I misunderstood?

## 2 Answers

In your code, you have computed [1/(2 * ele + 1) for ele in r], which (in your example) is $$\left[ \frac{-1}{9}, \frac{-1}{7}, \frac{-1}{5}, \frac{-1}{3}, -1, 1, \frac{1}{3}, \frac{1}{5}, \frac{1}{7}, \frac{1}{9}, \frac{1}{11} \right] \text{.}$$ When you sum those up, you get $$\frac{1}{11} = 0.\overline{09}$$, as you observed.

However, the book specifies that you use $$\frac{1}{2q+1}$$, so [1/(2 * q + 1) for ele in r], which will give $$\left[ \frac{1}{11}, \frac{1}{11}, \frac{1}{11}, \frac{1}{11}, \frac{1}{11}, \frac{1}{11}, \frac{1}{11}, \frac{1}{11}, \frac{1}{11}, \frac{1}{11}, \frac{1}{11} \right] \text{.}$$ Summing those gives $$1$$, as expected. Also, the simplest moving average should weight all of its inputs equally, which is what happens here.

Note that the book defines $$a_r = \frac{1}{2q + 1}$$ where $$a_r$$ does NOT depend on $$r$$, but only on $$q$$. Since $$q$$ is fixed, all of the $$a_r$$ should be the same. In your code, you calculate $$a_r = \frac{1}{2r + 1}$$ instead.