4
$\begingroup$

I am generating a sequence of normal random variables (using the routines from boost C++ library). How fast would you expect the mean and the variance of the sequence converge to the actual variance?

In my case the true values are:

mean=-0.01208333333333333
variance=0.0075

For 10000000 generated variables I get:

Mean=-0.0120757, 
var=0.00750125

which intuitively seem quite far (especially variance) for this ridiculously large number of samples. Should I expect faster convergence (i.e. there is some issue with my code), or is it what you would expect?

$\endgroup$

2 Answers 2

2
$\begingroup$

Your results are well within expectations. The random variable defined by $(n-1)s^2 \over \sigma^2$ is a $\chi^2$ random variable with variance of $2(n-1)$. Using this I find the standard deviation of $s$ to be 0.000003354, which when compared to your observed difference of 0.00000125 means this is not unusual. A similar argument shows your $\bar x$ result is also in line.

$\endgroup$
1
$\begingroup$

Doing this in Stata (actually Mata) I got the following results for 10 such large samples (first column is the mean, the second is the variance):

: res = J(10,2,.)

: for(i=1; i <= 10; i++) {
>         x = rnormal(10000000,1, -0.01208333333333333, sqrt(0.0075))
>         res[i,.] = meanvariance(x)'
> }

: res
                   1              2
     +-------------------------------+
   1 |  -.0120843169    .0075019019  |
   2 |  -.0121035264    .0074968712  |
   3 |  -.0120866703    .0074955339  |
   4 |  -.0120646992     .007499407  |
   5 |  -.0120959452    .0074969704  |
   6 |  -.0120737802    .0075110002  |
   7 |  -.0120411955    .0074920638  |
   8 |  -.0120973025    .0075073199  |
   9 |  -.0120785954     .007501123  |
  10 |   -.012116408    .0075042688  |
     +-------------------------------+

So your results seem to be in line with what happens inside Stata. That is no guarantee that there is no error, but it is still somewhat reassuring.


Edit:

I checked the variances from Stata's random normal number generators against the results mentioned in soakly's answer and not surprisingly it performs very well:

clear all

local n = 1000
local reps = 10000
local m = -0.01208333333333333
local v = .0075

set obs `reps'

mata
res = J(`reps',2,.)

for(i=1; i <= `reps'; i++) {
    x = rnormal(`n',1, `m', sqrt(`v'))
    res[i,.] = meanvariance(x)'
}

idx =st_addvar("double","v")
st_store(.,idx,res[.,2])

end

gen chi = (`n'-1)*v/`v'
qchi chi, df(`=`n'-1')

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.