From: Oleg Komarov on
Luna Moon <lunamoonmoon(a)gmail.com> wrote in message <2b265914-45f5-4b6e-9a85-c6d07191bfaf(a)s9g2000yqd.googlegroups.com>...
> Hi all,
>
> In recursive moving or rolling average,
>
> result_t = alpha * p_t + (1-alpha)* result_{t-1},
>
> where p_t is the new arrival...
>
> How do we do this fast in Matlab, using the "filter" function format,
> i.e. it works on a whole array column-wise.
>
> Now, how to apply the same concept to rolling variance?
>
> I guess in order to do rolling variance, we need to first get the
> rolling average,
>
> and then the rolling mean square:
>
> rolling_mean_square_t = alpha*p_t^2 + (1-
> alpha)*rolling_mean_square_{t-1},
>
> then using:
>
> rolling_variance_t = rolling_mean_square_t - rolling_mean_t ^2 ...
>
> Am I right? How to do this fast?
>
> Thanks

http://www.mathworks.com/matlabcentral/fileexchange/12550-slidefun-v4-0-sep-2008

Oleg
From: Bruno Luong on
Luna Moon <lunamoonmoon(a)gmail.com> wrote in message <2b265914-45f5-4b6e-9a85-c6d07191bfaf(a)s9g2000yqd.googlegroups.com>...
> Hi all,
>
> In recursive moving or rolling average,
>
> result_t = alpha * p_t + (1-alpha)* result_{t-1},
>
> where p_t is the new arrival...
>
> How do we do this fast in Matlab, using the "filter" function format,
> i.e. it works on a whole array column-wise.
>
> Now, how to apply the same concept to rolling variance?

% Data
a=rand(1,20);

% After doing that:
N = 5; % window size
w = ones(1, N);
v = (conv(a.^2,w,'valid')-(conv(a,w,'valid')).^2/N)/(N-1)

% v will be:
% v(1) == std(a(1:N))^2
% v(2) == std(a(2:N+1))^2 ...
% v(end) == std(a(end-N+1:end))^2

If needed, replace CONV with FILTER, and you are set.

Bruno