Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Recursive mean plot

Subject: Recursive mean plot

From: Jhonatan

Date: 3 Dec, 2011 17:55:09

Message: 1 of 7

well I have Z= randn(100,1)
and i do the recursive like this:

for i=1:100
mean(Z(1:i))
end

which give me the recursive
the problem is that i have to plot the recursive estimate of the mean. any hlep?

Subject: Recursive mean plot

From: Roger Stafford

Date: 5 Dec, 2011 01:24:08

Message: 2 of 7

"Jhonatan " <jhon902@hotmail.com> wrote in message <jbdnpt$25m$1@newscl01ah.mathworks.com>...
> well I have Z= randn(100,1)
> and i do the recursive like this:
>
> for i=1:100
> mean(Z(1:i))
> end
>
> which give me the recursive
> the problem is that i have to plot the recursive estimate of the mean. any hlep?
- - - - - - - - -
  I can show you how to obtain the results of that for-loop, but it doesn't accord with my notion of a recursive procedure.

 E = cumsum(Z)./(1:length(Z)).';

Roger Stafford

Subject: Recursive mean plot

From: Greg Heath

Date: 5 Dec, 2011 04:31:37

Message: 3 of 7

On Dec 4, 8:24 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> "Jhonatan " <jhon...@hotmail.com> wrote in message <jbdnpt$25...@newscl01ah.mathworks.com>...
> > well I have Z= randn(100,1)
> > and i do the recursive like this:
>
> > for i=1:100
> > mean(Z(1:i))
> > end
>
> > which give me the recursive
> > the problem is that i have to plot the recursive estimate of the mean. any hlep?
>
> - - - - - - - - -
>   I can show you how to obtain the results of that for-loop, but it doesn't accord with my notion of a recursive procedure.
>
>  E = cumsum(Z)./(1:length(Z)).';

NICE!

All these years I have been using

N = length(Z)
E(N) = 0;
E(1) = Z(1);

for i = 1:N-1

E(i+1) = E(i) + ( Z(i+1) - E(i) ) / ( i + 1 ) ;

end

Is there a similar vectorization for the variance?

Greg

Subject: Recursive mean plot

From: Roger Stafford

Date: 5 Dec, 2011 06:12:08

Message: 4 of 7

Greg Heath <heath@alumni.brown.edu> wrote in message <b62829cd-63c9-4815-94a0-f9d9b09e41aa@z1g2000yqa.googlegroups.com>...
> Is there a similar vectorization for the variance?
- - - - - - - - - - -
  Hi Greg. I don't really know how a recursive variance is defined, but if it is analogous to this recursive mean, you could use:

 n = length(Z);
 V = (cumsum(Z.^2)-cumsum(Z).^2./(1:n).')./[1;(1:n-1).'];

(For large n this form suffers somewhat from greater round off error.)

Roger Stafford

Subject: Recursive mean plot

From: Greg Heath

Date: 6 Dec, 2011 04:11:31

Message: 5 of 7

On Dec 5, 1:12 am, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> Greg Heath <he...@alumni.brown.edu> wrote in message <b62829cd-63c9-4815-94a0-f9d9b09e4...@z1g2000yqa.googlegroups.com>...
> > Is there a similar vectorization for the variance?
>
> - - - - - - - - - - -
>   Hi Greg.  I don't really know how a recursive variance is defined, but if it is analogous to this recursive mean, you could use:
>
>  n = length(Z);
>  V = (cumsum(Z.^2)-cumsum(Z).^2./(1:n).')./[1;(1:n-1).'];
>
> (For large n this form suffers somewhat from greater round off error.)

Although a recursive form for the mean is

 M(n+1) = [n*M(n) + x(n+1)] / (n+1)

the following perturbational form is much more accurate for large n:

 M(n+1) = M(n) + [x(n+1) - M(n)] / (n+1).

The corresponding perturbational form for the unbiased variance is

V(n+1) = V(n) + [(x(n+1)-M(n))*(x(n+1)-M(n+1))-V(n)]/n

which can be derived from the equations

n*V(n) = (x(n+1)-M(n+1))^2 + sum(i=1:n)
{(x(i)-M(n+1))^2}
sum(i=1:n){ x(i)-M(n) } = 0
sum(i=1:n){(x(i)-M(n))^2} = (n-1)*V(n)
M(n+1) - M(n) = [x(n+1) - M(n+1)] / n
x(n+1) - M(n+1) = (n/(n+1))*[x(n+1)-M(n)]

Since vectorization does all of the calculations in one fell swoop,
it does not appear to be compatible with recursion.

Hope this helps.

Greg

Subject: Recursive mean plot

From: Roger Stafford

Date: 6 Dec, 2011 07:56:08

Message: 6 of 7

Greg Heath <heath@alumni.brown.edu> wrote in message <54d437d0-1cda-4b90-864e-196c8a4eb134@v5g2000yqn.googlegroups.com>...
> Since vectorization does all of the calculations in one fell swoop,
> it does not appear to be compatible with recursion.
- - - - - - - - -
  I'm puzzled by your statement, Greg. The code

 n = length(Z);
 V = (cumsum(Z.^2)-cumsum(Z).^2./(1:n).')./[1;(1:n-1).'];

generates the same values as does your perturbational recursive formula on Z. It looks like a valid vectorization to me; it's all in "one fell swoop".

  By the way, the first of your five equations ought to read:

 n*V(n+1) = (x(n+1)-M(n+1))^2 + sum(i=1:n){(x(i)-M(n+1))^2}

Roger Stafford

Subject: Recursive mean plot

From: Greg Heath

Date: 6 Dec, 2011 22:21:28

Message: 7 of 7

On Dec 6, 2:56 am, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> Greg Heath <he...@alumni.brown.edu> wrote in message <54d437d0-1cda-4b90-864e-196c8a4eb...@v5g2000yqn.googlegroups.com>...
> > Since vectorization does all of the calculations in one fell swoop,
> > it does not appear to be compatible with recursion.
>
> - - - - - - - - -
>   I'm puzzled by your statement, Greg.  The code
>
>  n = length(Z);
>  V = (cumsum(Z.^2)-cumsum(Z).^2./(1:n).')./[1;(1:n-1).'];
>
> generates the same values as does your perturbational recursive formula on Z.  It looks like a valid vectorization to me; it's all in "one fell swoop".

Agree. My point is the incompatibility of recursion and vectorization.

1. Recursion usualy implies that there is a significant time lapse
between
the availability of x(n+1) and x(n+2) and you wish to have M(n+1) and/
or
V(n+1) before x(n+2) is available.
2. Vectorization calculates all of the M(n) and/or V(n) using all of
the data at once.
>
>   By the way, the first of your five equations ought to read:
>
>  n*V(n+1) = (x(n+1)-M(n+1))^2 + sum(i=1:n){(x(i)-M(n+1))^2}

Right. Thanks.

Hope this helps.

Greg

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us