[R] Is the random number generator biased?

Duncan Murdoch murdoch at stats.uwo.ca
Mon Mar 26 21:20:49 CEST 2007


On 3/26/2007 2:48 PM, 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?

The points in your plot are all based on a single realization of a 
Brownian motion.  What you are seeing is just that X[1000000] is 
non-zero.  If you repeat the simulation you'll get a different linear trend.

> Is the generation of the random walk in some way biased or do I just 
> miss some point?

If it is biased, your plots don't show it.  Try this plot instead (and 
wait a lot longer; it does a lot of memory allocations!):

X.scale.mean <- numeric(1000)

for (i in 1:1000) {
    X <- cumsum(rnorm(1000000))
    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")

Duncan Murdoch

> 
> Thanks for any enlighting
> replies in advance
> Oliver
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list