DSPRelated.com
Forums

Question about computing a "moving variance"

Started by Rick Lyons February 27, 2005
Jerry Avins wrote:

> Martin Eisenberg wrote: > >> Jerry Avins wrote: >> >> >>> 1 N >>> For those who think in math, σ² = -*SUM(xbar - x[n]), where >>> N n=1 >> >> >> >> I don't know if this works for others, but it's probably folly to >> expect an actual sigma (if that's what it was) to survive usenet >> transfer. > > > It was. Did exponent 2 come through? > > Jerry
In both Jerry's and Martin's post the exponent came through OK. Can't say the same for the rest of the formula in Martin's quote of Jerry. In Martin's post I received: Jerry Avins wrote: >> 1 N >> For those who think in math, σ² = -*SUM(xbar - x[n]), where >> N n=1 But when Jerry quoted Martin I received Martin Eisenberg wrote: > Jerry Avins wrote: >> 1 N >> For those who think in math, σ² = -*SUM(xbar - x[n]), where >> N n=1 [ left side of the equation being a lowercase sigma with exponent of 2 ] {blank lines snipped from both) Is this a "code page" issue? I thought code pages were an ANS standard. Or is it ISO or I'm just confused?
Rick Lyons wrote:
> Hi Guys, > > I've been tryin' to figure something out and > darnit, I just can't seem to find a satisfying > answer. Maybe one of you guys will help me. > > You recall the idea of a moving averager built > with a tapped delay-line FIR filter like the > 8-point moving averager shown below in Figure 1. > (All the "b" coefficients are equal to 1/8th.) > Each new y(n) output is the average of the > current x(n) input sample and the seven > previous input samples.
[snip some stuff]
> > You'll also recall that a recursive 8-point > moving averager can be built, with a "comb" > filter (8 delays and a subtract) followed > by a multiply and then a 1st-order > integrator as shown below in Figure 2.
[snip some more stuff]
> > But darn, I can't come up with such an "N-point moving > variance" network whose structure remains > (essentially) the same regardless of the value of N. > It's those darned subtractions, needed in the > variance computation, that make this process "messy". >
[snip some more stuff] You don't need "those darned subtractions". What you need is to re-write the definition of sample variance (and maybe stretch the definition of sample mean). You know that V = [1/(n-1)] * [sum (x - xbar)**2] where xbar is your estimate of sample mean (you figured that out above). What you do is square the expression under the sum: (x - xbar)**2 == (x**2 - 2 x * xbar + xbar**2) Then you notice that you can sum the three terms individually. But summing xbar**2 is just n*xbar**2 because xbar is constant over the summation. Summing 2*x*xbar is just 2*n*xbar**2 (Ok, ok! You'll have to look at that for a minute!) So, you're left with: V = [1/(n-1)]*{[sum (x**2)] - n*xbar**2} -or- V = [1/(n-1)]*{[sum(x**2)]-[sum(x)**2]/n} So, to build the FIR version, you need one delay line of n units. You can figure out the rest. Hint: for the IIR version, think about a system with two inputs, x**2 and x. Regards, -jeh
Jerry Avins wrote:

>>>> 1 N >>>> For those who think in math, σ² = -*SUM(xbar - x[n]), >>>> N n=1 >>> >>> I don't know if this works for others, but it's probably folly >>> to expect an actual sigma (if that's what it was) to survive >>> usenet transfer. >> >> It was. Did exponent 2 come through?
Yes, and I also see the ^3 and fractions you like to use. But look at how Richard's quote of my post appears to me -- some more back-and- forth and I'll have the line filled ;)
> >> 1 N > >> For those who think in math, σ² = -*SUM(xbar - x[n]), > >> N n=1
-- Quidquid latine dictum sit, altum viditur.
Martin Eisenberg wrote:
> Jerry Avins wrote: > > >>>>> 1 N >>>>> For those who think in math, σ² = -*SUM(xbar - x[n]), >>>>> N n=1 >>>> >>>>I don't know if this works for others, but it's probably folly >>>>to expect an actual sigma (if that's what it was) to survive >>>>usenet transfer. >>> >>>It was. Did exponent 2 come through? > > > Yes, and I also see the ^3 and fractions you like to use. But look at > how Richard's quote of my post appears to me -- some more back-and- > forth and I'll have the line filled ;) > > >>>> 1 N >>>> For those who think in math, σ² = -*SUM(xbar - x[n]), >>>> N n=1
Martin, I'm doubly sorry. I swore of such typography last year, and I broke my word. For what it's worth, the sigma-squared in the equation at the top of this that you cited as unreadable shows up perfectly in my browser even with >>>>> in front of it. On the other hand, it is transmuted in what came back from Richard, even though he (I think) could read it. My character set is Western ISO 8859-1, the default in Mozilla and Netscape on Windows. Jerry -- Engineering is the art of making what you want from things you can get. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Jerry Avins wrote:

> Martin Eisenberg wrote: > >> Jerry Avins wrote: >> >> >>>>>> 1 N >>>>>> For those who think in math, σ² = -*SUM(xbar - x[n]), >>>>>> N n=1 >>>>> >>>>> >>>>> I don't know if this works for others, but it's probably folly >>>>> to expect an actual sigma (if that's what it was) to survive >>>>> usenet transfer. >>>> >>>> >>>> It was. Did exponent 2 come through? >> >> >> >> Yes, and I also see the ^3 and fractions you like to use.
I see nothing involving a "3" ;/ In the above quoted line I see a carat followed by 3. >> But look at
>> how Richard's quote of my post appears to me -- some more back-and- >> forth and I'll have the line filled ;) >> >> >>>>> 1 N >>>>> For those who think in math, σ² = -*SUM(xbar - x[n]), >>>>> N n=1 > > > Martin, I'm doubly sorry. I swore of such typography last year, and I > broke my word. For what it's worth, the sigma-squared in the equation at > the top of this that you cited as unreadable shows up perfectly in my > browser even with >>>>> in front of it. On the other hand, it is > transmuted in what came back from Richard, even though he (I think) > could read it.
I seem [ NB wishy-washy verbiage ;] to be able to read Jerry's posts. I'm using Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.3) Gecko/20040910 . IIRC all settings are default and I'm running under WinXP Pro SP1 .
> > My character set is Western ISO 8859-1, the default in Mozilla and > Netscape on Windows. > > Jerry
And when Martin posted what I quoted Jerry as quoting Martin, it got even stranger than the preceding clause ;} I 'know' there is a way in Windows(tm) to capture screen to BMP/JPG file. If someone can remind me of key sequence I'll forward Jerry and Martin image of what I see.
Richard Owlett wrote:

>> Martin Eisenberg wrote:
>>> Yes, and I also see the ^3 and fractions you like to use. > > I see nothing involving a "3" ;/ > In the above quoted line I see a carat followed by 3.
That's exactly what I wrote to indicate superscript 3. While we're at it, do y'all see that here: � ?
> I'm using Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; > rv:1.7.3) Gecko/20040910 . > > IIRC all settings are default and I'm running under WinXP Pro > SP1 .
> Jerry Avins wrote:
>> My character set is Western ISO 8859-1, the default in Mozilla >> and Netscape on Windows.
I'm running XNews 5.04.25 on German Win98SE and it doesn't seem to have a charset option, though I may have overlooked it.
> I 'know' there is a way in Windows(tm) to capture screen to > BMP/JPG file. If someone can remind me of key sequence I'll > forward Jerry and Martin image of what I see.
Hit the Print key (for me, 3rd from right in topmost row) and paste the clipboard contents into a graphics program; Windows' Paint will do. Here's what I see in the viewer and editor with MS Sans Serif and Courier New, respectively: http://martin.dg-internetdienste.de/sigma.gif I'm actually slightly disappointed that it didn't change with further quoting after all ;) -- Quidquid latine dictum sit, altum viditur.
Martin Eisenberg wrote:

   ...

>>Jerry Avins wrote: > > >>>My character set is Western ISO 8859-1, the default in Mozilla >>>and Netscape on Windows. > > > I'm running XNews 5.04.25 on German Win98SE and it doesn't seem > to have a charset option, though I may have overlooked it.
In Mozilla and its descendants Netscape and Thunderbird, the encoding scheme is selected in the View pull-down menu (<alt> V). Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
On 1 Mar 2005 14:00:14 -0800, "John Hadstate" <jh113355@hotmail.com>
wrote:

>Rick Lyons wrote:
> > [snipped] > >> >> But darn, I can't come up with such an "N-point moving >> variance" network whose structure remains >> (essentially) the same regardless of the value of N. >> It's those darned subtractions, needed in the >> variance computation, that make this process "messy". >> >[snip some more stuff]
Hi John, I just now saw your post.
>You don't need "those darned subtractions". What you need is to >re-write the definition of sample variance (and maybe stretch the >definition of sample mean). > >You know that V = [1/(n-1)] * [sum (x - xbar)**2] > >where xbar is your estimate of sample mean (you figured that out >above). > >What you do is square the expression under the sum: > >(x - xbar)**2 == (x**2 - 2 x * xbar + xbar**2) > >Then you notice that you can sum the three terms individually. But >summing xbar**2 is just n*xbar**2 because xbar is constant over the >summation. Summing 2*x*xbar is just 2*n*xbar**2 (Ok, ok! You'll have >to look at that for a minute!) >So, you're left with: > > V = [1/(n-1)]*{[sum (x**2)] - n*xbar**2} > > -or- > > V = [1/(n-1)]*{[sum(x**2)]-[sum(x)**2]/n}
Ah. John, your expression is equal to that on the webpage: http://www.mnstate.edu/wasson/ed602calcvarraws.htm (URL supplied by Clay Turner) but you've supplied the "missing step" that justifies the expression. [I didn't see that step until you stuck my nose in it. :-) ] Thanks! John, your name has been officially entered on the list of guys to whom I owe a beer! (Unfortunately, to collect you have to fly to Sacremento CA, rent a car, and drive to my local bar to collect that beer.)
>So, to build the FIR version, you need one delay line of n units.
Oops. Don't we need two delay lines? One delay line to compute [sum (x**2)], and one delay line to compute xbar. Or am I missing something here? Thanks again John, [-Rick-]
Rick Lyons wrote:

(snip)

>>>But darn, I can't come up with such an "N-point moving >>>variance" network whose structure remains >>>(essentially) the same regardless of the value of N. >>>It's those darned subtractions, needed in the >>>variance computation, that make this process "messy".
(snip)
>>So, to build the FIR version, you need one delay line of n units.
> Oops. Don't we need two delay lines? One delay line > to compute [sum (x**2)], and one delay line to compute > xbar. Or am I missing something here?
I thought that at first, too. I think, though, that it is two delay lines and one multiplier, or one delay line and two multipliers. Also, the delay line for x**2 could be twice as wide as for x. I would say it depends on N and the width. You could also build a pipelined multiplier into the delay line. -- glen
Nice topic!  I have always just used the recursive mean and variance
estimators given in "Optimal Estimation" by Frank Lewis, since they
made sense to me...

For sample mean using sample input x[n]:

m[n+1] = m[n] + 1/(N+1)*(x[n+1] - m[n])
       = N/(N+1)*m[n] + 1/(N+1)*x[n+1] = a nice first-order IIR filter

For zero-mean sample variance (biased):

s[n+1] = s[n] + 1/(N+1)*(x[n+1]^2 - s[n]), which is equal to the sample
variance and another nice IIR filter (both look like averagers but dont
have constant coefficients since N is changing).

For non-zero mean you just use your estimate of m[n] above in the
equation for sample variance.  You can also derive the same filter for
unbiased estimator.

These probably reduce to what you are talking about, the derivation is
a bit different.  You start with the block equation(s) for sample mean
and variance, then do sort of an induction step where you add one
sample to the block equation and then rewrite the whole deal.  You end
up with a nice recursion...

Jim