[R] Is the random number generator biased?

(Ted Harding) ted.harding at nessie.mcc.ac.uk
Mon Mar 26 21:32:46 CEST 2007


[Sorry, I need to correct a vital typo!!]

On 26-Mar-07 18:48:50, Oliver Faulhaber wrote:
> Hi all,
> 
> in order to verify some results I did the following test in R (2.4.1., 
> windows system):
> 
> X <- cumsum(rnorm(1000000))
> for (i in 1:1000) {
>   tmp                   <- seq(1,length(X),by=i)
>   X.coarse              <- X[tmp]
>   X.return              <- diff(X.coarse)
>   X.scale.mean[i]       <- mean(X.return)
> }
> plot(X.scale.mean,type="l")
> 
> As X is a random walk with increments following a standard normal 
> distribution, the mean of X should be 0. Further more, each "piece" of 
> the random walk should also have zero mean - independent of its length.
> 
> Why is it then, that the plot of X.scale.mean shows a clear linear
> trend?
> 
> Is the generation of the random walk in some way biased or do I just 
> miss some point?
> 
> Thanks for any enlighting replies in advance
> Oliver

Try

  plot(X,type="l")

and you will see why you get that result from plot(X.scale.mean).

But that does not help to understand why plot(X) looks as it does.
However, this behaviour is standard for a random walk.

[XXXIf you think about it, var(X[n]) = 1 (in your case), so atXXX]

If you think about it, var(X[n]( = n (in your case), so at
the nth step you are (for instance) likely (P>1/2)to be at
least 0.5*sqrt(n) on one side or other of the mean.
So say n=10000: then you are likely to be at least 50 away
from the mean; and you have to get there somehow, starting
from 0. So there will be an overall trend!

Similarly, for n=1000000, you are likely to be at least 500
from the mean.

The same explanation accounts for the shorter segments of trend
you will also observe over the graph of X, since from any point
along the graph the above holds true on a smaller scale.

Hoping that helps!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 26-Mar-07                                       Time: 20:27:57
------------------------------ XFMail ------------------------------

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 26-Mar-07                                       Time: 20:32:42
------------------------------ XFMail ------------------------------



More information about the R-help mailing list