Skip to content

rnorm() converted to daily

6 messages · Ken-JP, Karl Ove Hufthammer, Meyners, Michael, LAUSANNE, AppliedMathematics

#
yearly 8% drift, 25% sd

why are x and y so different (x and z look ok)?

Does this have something to do with biases due to the relationship between
population and sample-estimates as a function of n samples and sd?  Or am I
doing something wrong?

-----------------------------------------------------------------------

set.seed( 1 );
x <- mean( rnorm( 100000, mean=0.08, sd=0.25 ));
set.seed( 1 );
y <- mean( rnorm( 100000, mean=(0.08/252), sd=(0.25/sqrt(252)) )) * 252;
set.seed( 1 );
z <- mean( rnorm( 100000, mean=(0.08/252), sd=0.001 )) * 252;
# -----------------------------------------------------------------------
x    # 0.07943898
y    # 0.07109407
z    # 0.07943449
#
Ken-JP:
Yes, you?re doing something wrong.

While it is true? that rnorm(n, a, b) will give the same results as
rnorm(n, 0, 1) * b + a, it is *not* true that the above calculations
would return the same numbers. That?s the reason the the means 
differ.

? True in practice, but not documented as far as I can see, so you
  should not depend on it.
#
I think I got it...

I assumed that n=100000 would be big enough to give me a sample mean which
converges on the population mean, but this was a bad assumption.

Using sigma / sqrt(n) for the standard error of the sample mean, I get...

-----------------------------------------------------

se.x.mean.as.perc.of.pop.mean <- (0.25 / sqrt(100000)) /0.08;  
se.y.mean.as.perc.of.pop.mean <- (0.25/sqrt(252)) / sqrt(100000)
/(0.08/252);

se.x.mean.as.perc.of.pop.mean   # 0.0098821
se.y.mean.as.perc.of.pop.mean   # 0.1568738
SD in y is more than 15 times (!!!) larger than in x and z,
respectively, and hence SD of the mean y is also larger. 100,000 values
are not enough to stabilize this. You could have obtained 0.09 or even
larger as well. Try y with different seeds, y varies much more than x
and z do. Or check the variances: 

var.x <- var(replicate(100, mean( rnorm( 100000, mean=0.08, sd=0.25 ))))
var.y <- var(replicate(100, mean( rnorm( 100000, mean=(0.08/252),
sd=(0.25/sqrt(252)) )) * 252))
var.z <- var(replicate(100, mean( rnorm( 100000, mean=(0.08/252),
sd=0.001 )) * 252))

I guess what you are doing wrong is assuming that 0.25/sqrt(252) is
similar to 0.25/252, the latter being close so 0.001, while the former
is obviously not. Try to replace 0.25 in y by 0.0158, than you should be
close enough.

HTH, Michael

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Ken-JP
Sent: Freitag, 17. April 2009 09:26
To: r-help at r-project.org
Subject: [R] rnorm() converted to daily



yearly 8% drift, 25% sd

why are x and y so different (x and z look ok)?

Does this have something to do with biases due to the relationship
between population and sample-estimates as a function of n samples and
sd?  Or am I doing something wrong?

-----------------------------------------------------------------------

set.seed( 1 );
x <- mean( rnorm( 100000, mean=0.08, sd=0.25 )); set.seed( 1 ); y <-
mean( rnorm( 100000, mean=(0.08/252), sd=(0.25/sqrt(252)) )) * 252;
set.seed( 1 ); z <- mean( rnorm( 100000, mean=(0.08/252), sd=0.001 )) *
252; #
-----------------------------------------------------------------------
x    # 0.07943898
y    # 0.07109407
z    # 0.07943449



--
View this message in context:
http://www.nabble.com/rnorm%28%29-converted-to-daily-tp23092444p23092444
.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org 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.
#
Hi Karl,

Your comment is very interesting.  

Do you mean I should favor:

  rnorm(n, 0, 1) * b + a

over

  rnorm(n, a, b)

?  What is the proper/safe way to use rnorm()?

--------------------------------------------------------

FWIW,

set.seed( 1 );
yy <- mean( rnorm( 100000 ) * (0.25/sqrt(252)) + (0.08/252)) * 252;
yy   # 0.07109407

still gives me what I had before (but I shouldn't depend on it being the
same, right)?
#
Ken-JP:
No, as I said, they give exactly the same results.