[BUG][stdlib][random] randn_float64()
generates values from a wrong distribution: variance
argument should be std
#3976
Labels
bug
Something isn't working
mojo
Issues that are related to mojo
mojo-repo
Tag all issues with this label
not synced
Bug description
This is related to the function
randn_float64()
in/stdlib/src/random/random.mojo
.This function should return a random double sampled from a normal distribution with
mean
andvariance
. However, It actually returns a random value from the normal distribution withmean
and squaredvariance
.Solution 1
To fix it, the second argument should be changed to
std
(sigma
), wherestd
is the standard deviation of the Normal distributionOr
mu
-sigma
expression:Solution 2
Another way to fix it is to change the
variance
in the return line tosqrt(variance)
.Steps to reproduce
See the following evaluation code:
We draw 1000,000 random values from a normal distribution with mean 0 and variance 2, using function
randn_float64(0, 2)
.We then calculate the variance of sample. It shows that the sample variance is
~3.9985
, which is2^2
. This means that the argumentvariance
is actuallystd
.mean: 0.002467043920765725, variance: 3.998468697001007
Changing the
variance
from2
to other values witness the same issue.System information
The text was updated successfully, but these errors were encountered: