# Weight vector of an exponential moving average?

With weight vector I mean the vector with weights that you have to multiply the observations in the window that slides over your data with so if you add those products together it returns the value of the EMA on the right side of the window.

For a linear weighted moving average the formula for finding the weight vector is: (1:n)/sum(1:n) (in R code). This series of length n adds up to 1. For n=10 it will be

0.01818182
0.03636364
0.05454545
0.07272727
0.09090909
0.10909091
0.12727273
0.14545455
0.16363636
0.18181818

the numbers 1 to 10 / 55, with 55 the sum of the numbers 1 to 10.

How do you calculate the weight vector for an exponential moving average (EMA) of length n?

if n is the length of the window, then alpha<-2/(n+1) and i<-1:n so EmaWeightVector<-((alpha*(1-alpha)^(1-i)))

Is this correct?

Even though the EMA is not really confined to a window with a start and an end, shouldn't the weights add up to 1 just like with the LWMA?

• An exponential moving average is a single-pole IIR filter. That is, its impulse response is infinitely long, so there is no finite-length weight vector that will exactly match. However, you can approximate the EMA filter to any desired precision by approximating it with a long-enough FIR filter (i.e. the weight vector that you're talking about). – Jason R Dec 18 '12 at 21:16
• Thanks Jason, any pointers of how to approximate the EMA filter to any desired precision by approximating it with a long-enough FIR filter? There's a perl script on en.wikipedia.org/wiki/… that made the image of the EMA weight vector, but I don't understand it: if they set the number of weights to 15 why are there 20 red bars instead of 15? – MisterH Dec 19 '12 at 22:40
• There are many moving average algorithms. THe time weighting response correlates directly to the desired bandwidth or frequency response of the skirts or neighbouring trends. Cauchy, Gaussian, Hamming, Hanning, Welsh, Blackmann and Blackmann-Harris are a few different distribution functions that come to mind each with different curves designed for special functions. and yes the peak can be normalized to 1 but may not be by default since it is the intergral or area under the curve that is constant – Tony Stewart Sunnyskyguy EE75 Dec 20 '12 at 8:02
• They give a perl script to describe how they made the image on en.wikipedia.org/wiki/…, but my problem is that if I translate it to R, the weights add up to 5,.. while they should approximately add up to 1. Could someone translate this into R please? – MisterH Dec 22 '12 at 22:54

I believe the Wikipedia reference may refer to the Hunter (1986) but Wikipedia articles do morph over time. One aspect of the accepted answer that is less than satisfying is the code to divide by the sum.

The current article makes mention of this chart.

From https://en.wikipedia.org/wiki/Moving_average#/media/File:Exponential_moving_average_weights_N=15.png gives the code associated with it.

$$N = 15;$$f = 1 - 2/($N+1); $$total = 0; foreach$$i (0..20) { $$value = (1 -$$f) * ($$f **$$i); print "$$i$$value\n"; $$total +=$$value; } print "# total$total (should approach 1)\n";


Which in R is essentially:

N = 15;
a = 1 - 2/(N+1); #it is more customary to use a for the alpha rather than f
i=0:20 #increase this to a larger number to see how the sum approaches 1.0
value = (1 - a) * (a ^ i);
sum(value) #the sum eventually approaches 1.0
plot(value);grid()


The above weights do eventually sum to 1 as they should without being forced...

I hope this may be of use.

ok the solution was: (in R)

emaweights<-function(m)
{
alpha<-2/(m+1)
i<-1:m
sm<-sum((alpha*(1-alpha)^(1-i)))
return(((alpha*(1-alpha)^(1-i)))/sm)
}


Here the weights do add up to 1.

This will get you close to a closed form solution...

The weight at each vector is: $$\frac{(1-L)L^t}{1-L^T}$$

Where:

• $L$ is your smoothing constant; $(1-L)$ is often referred to as alpha
• $t$ is your time vector
• $T$ your last $t+1$

This doesn't exist anywhere else on the internet, so I had to invent it. It may therefore result in slightly different weights, but it works.

• Hey, welcome to DSP and thank you for contributing. I don't really understand this area perfectly, so I can not upvote your answer since I can't judge its quality. I improved your formatting a bit though, and if you could look at the formatting guide for your next answer, that would be great and would make your answers even better. – penelope Jan 29 '14 at 20:30