[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: vectorized moving average?
From: |
Tim Rueth |
Subject: |
RE: vectorized moving average? |
Date: |
Tue, 11 May 2010 10:41:01 -0700 |
Thanks for showing how to use filter() to do a simple moving
average. I implemented your code, and it agrees with movavg(x,10,10,0),
which calculates a 10-day simple moving average. There's only a difference
in the first 9 numbers due to assumed values of negative time (movavg
calculates a "run-in" period). As you might recall, I'm trying to use
filter() to avoid movavg()'s for-loop.
Now, what I'm trying to do is a weighted moving average,
identical to the "alpha" parameter of movavg(). When alpha=0, it's a
simple moving average, and agrees with filter(). If I change alpha to 1,
I'm suppose to get a linear MA. Here's the code in movavg.m that does the
weighting (lead is the number of days to average, equal to 10 in the above
case):
lead = (1:lead).^alpha;
## Adjust the
weights to equal 1
lead = lead / sum
(lead);
So, for a 10-day linearly-weighted moving
average (lead = 10, alpha = 1),
the previous 9 days and current day
should be weighted as follows: 1/55, 2/55, 3/55...10/55, with the greatest weight (10/55) applied to the current day. So, I tried a simple test case with just a 2-day MA on
a 6-element vector:
ma_days = 2;
alpha = 1;
len =
6;
a = rand(1,len);
# Calculate MA using
movavg()
ma = movavg(a,ma_days,ma_days,alpha);
# Calculate MA using
filter()
sweep = (1:ma_days).^alpha;
norm_sweep = sweep/sum(sweep);
f =
filter(norm_sweep,1,a);
disp(ma);
disp(f);
The movavg() and filter() results are similar, but not
equal. I'm guessing I don't have the arguments for filter() correct, but I
can't figure out what I did wrong. Particularly, I'm not sure what the
second argument of filter() is supposed to do.
Help?
I'm not sure what you mean by weighting of 0.5, but to do a simple
10-day average, it'd be
x = rand(1, 100);
y = filter(1/10*ones(1, 10), 1, x)
This assumes that the values at negative time (x(0), x(-1), etc) are all
zero. So, for example, the first value of y would be x(1)/10.
On Fri, May 7, 2010 at 3:33 PM, Tim Rueth
<address@hidden> wrote:
I looked at both conv() and filter(), but can't figure out
how to do a
moving average with them. Perhaps I'm not understanding
the functions of
the input vars correctly.
Let's say I have an
array, a = rand(1,100). Can you tell me how I'd use
conv() and
filter() to take, say the 10-day moving average of it, with a
weighting
of 0.5?
Thanks!
- vectorized moving average?, Tim Rueth, 2010/05/06
- Re: vectorized moving average?, Andy Buckle, 2010/05/06
- RE: vectorized moving average?, Tim Rueth, 2010/05/07
- Re: vectorized moving average?, James Sherman Jr., 2010/05/07
- RE: vectorized moving average?,
Tim Rueth <=
- Re: vectorized moving average?, James Sherman Jr., 2010/05/11
- RE: vectorized moving average?, Tim Rueth, 2010/05/11
- Re: vectorized moving average?, James Sherman Jr., 2010/05/11
- RE: vectorized moving average?, Tim Rueth, 2010/05/12
- Re: vectorized moving average?, Francesco Potortì, 2010/05/13
- Re: vectorized moving average?, James Sherman Jr., 2010/05/13
- RE: vectorized moving average?, Tim Rueth, 2010/05/13
- Re: vectorized moving average?, James Sherman Jr., 2010/05/14
- RE: vectorized moving average?, Tim Rueth, 2010/05/17
Re: vectorized moving average?, Francesco Potortì, 2010/05/06