Take the 2-minute tour ×
Signal Processing Stack Exchange is a question and answer site for practitioners of the art and science of signal, image and video processing. It's 100% free, no registration required.

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?

Wikipedia tells me:

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?

share|improve this question
2  
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 –  Richman 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

2 Answers 2

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.

share|improve this answer

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.

share|improve this answer
    
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 at 20:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.