[R] Is the random number generator biased?
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Mon Mar 26 22:58:03 CEST 2007
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?
>
>
The latter. If you think a little closer, you'll realize that
sum(X.return) is going to be pretty close to X[1000000], except for the
sum of at most 999 terms (at most 961, actually). You then proceed to
calculate the mean by dividing by the number of terms which is
essentially 1/i.
> 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