Re: [R] Is the random number generator biased?

2007-03-26 Thread Duncan Murdoch
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(100))
 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[100] 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(100))
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@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.

__
R-help@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.


Re: [R] Is the random number generator biased?

2007-03-26 Thread Ted Harding
[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(100))
 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 (P1/2)to be at
least 0.5*sqrt(n) on one side or other of the mean.
So say n=1: 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=100, 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) [EMAIL PROTECTED]
Fax-to-email: +44 (0)870 094 0861
Date: 26-Mar-07   Time: 20:27:57
-- XFMail --


E-Mail: (Ted Harding) [EMAIL PROTECTED]
Fax-to-email: +44 (0)870 094 0861
Date: 26-Mar-07   Time: 20:32:42
-- XFMail --

__
R-help@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.


Re: [R] Is the random number generator biased?

2007-03-26 Thread Peter Dalgaard
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(100))
 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[100], 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@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.


__
R-help@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.