The reason is that the density is not the appropriate weight to use -
you need some kind of estimate of the probability.
Here is one way to show the near equivalence, computationally (using
S-PLUS 8.0)
> set.seed(57) # Heinz varieties...
> xx <- rnorm(50000, 1, 5)
> m0 <- mean(xx)
> m0
[1] 1.008457
> v0 <- var(xx)
> v0
[1] 24.74718
Now to do it the hard way...
> sx <- sort(xx) # string them out on an axis
> mx <- (sx[-1] + sx[-length(x)])/2 # mid points
> px <- diff(pnorm(c(-Inf, mx, Inf), 1, 5)) # probs
> m1 <- sum(px * sx)
> m1
[1] 0.9992542
> v1 <- sum(px * sx^2) - m1^2
> v1
[1] 24.99694
Hmm, not bad. When the exact results are 1 and 25. It's a bit of
cheat, though when you put the results you want into the calculation of
the weights!
Bill Venables.
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of John Fennick
Sent: Tuesday, 23 January 2007 2:13 PM
To: S-News List
Subject: [S] var(x) vs E{x^2] - [E{x}]^2
Hi Group,
Can anyone explain the vast differences between the Variance.A and
Variance.B or Variance.C below?
From the last 4 calls, my problem appears to be in the x^2 terms. Am
I just having a bad day?
I get similar performance with rhyper and others as well as the rnorm
used here.
thanks,
john
> xx <- rnorm( 50000, 1, 5)
> pp <- dnorm( xx , 1, 5)
> pp <- pp / sum( pp )
> paste( 'Variance.A = ', round( var( xx ), 2 ) , sep='')
[1] "Variance.A = 24.97"
> paste( 'Variance.B = ', round( sum(xx^2 * pp ) - (sum( xx*pp
) )^2, 2 ) , sep='' )
[1] "Variance.B = 12.52"
> paste( 'Variance.C = ', round( sum( (xx - sum( xx * pp ) )^2
* pp), 2 ) , sep='' )
[1] "Variance.C = 12.52"
> paste( 'Mean.A = ', round( sum( xx * pp ), 2 ) , sep=''
[1] "Mean.A = 0.99"
> paste( 'Mean.B = ', round( mean( xx ), 2 ) , sep='')
[1] "Mean.B = 1"
> paste( 'E{xx^2}.A = ', round( var( xx ) + mean( xx )^2, 2 ) ,
sep='' )
[1] "E{xx^2}.A = 25.97"
> paste( 'E{xx^2}.B = ', round( sum( xx^2 * pp) , 2 ) , sep='' )
[1] "E{xx^2}.B = 13.49"
John Fennick
jhf2@bellsouth.net
alt: j.fennick@ieee.org
Tel: 770 949-9132
7045 Fletcher Drive
Winston, GA 30187
USA
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news
|