Random.nextgaussian()可以从均值和标准差不同的分布中采样值吗? [英] Can Random.nextgaussian() sample values from a distribution with different mean and standard deviation?
问题描述
这是Java和基本数学问题的组合. Random.nextGaussian()的文档指出,它是从均值0和标准偏差为1的正态分布中抽样的.如果我想从均值和方差不同的正态分布中抽样,该怎么办?
This is a combined Java and basic math question. The documentation from Random.nextGaussian() states that it samples from a normal distribution with mean 0 and standard deviation 1. What if I wanted to sample from a normal distribution with a different mean and variance?
推荐答案
简短的答案是
Random r = new Random();
double mySample = r.nextGaussian()*desiredStandardDeviation+desiredMean;
例如,此答案在此处给出: http://www.javamex.com/tutorials/random_numbers/gaussian_distribution_2 .shtml
For example this answer is given here: http://www.javamex.com/tutorials/random_numbers/gaussian_distribution_2.shtml
我真的不明白为什么这样做,但是仔细研究一下后,我想我已经明白了.采样点的平均值为0,标准偏差为1;这意味着原始样本也是其自己的z得分( https://en.wikipedia.org/wiki/Standard_score).引自维基百科:"z的绝对值表示原始分数和总体平均值之间的距离,以标准差为单位".公式为z =(x-mean)/stdev,因此默认值为z = x.如果我们想保留样本的z分数,但更改均值和标准差,我们将怎么办?
I didn't really understand why this worked, but after looking into it a bit I think I figured it out. The mean of the sample point is 0, and the standard deviation is 1; that means that the original sample is also its own z-score ( https://en.wikipedia.org/wiki/Standard_score ). To quote from wikipedia "The absolute value of z represents the distance between the raw score and the population mean in units of the standard deviation". The formula is z=(x-mean)/stdev, so with the default values z=x. If we wanted to retain the z score for the sample but change the mean and stdev what would we do?
z * stdev +均值= x',其中z = x,x'表示具有期望均值和标准差的分布样本.
z*stdev + mean = x' where z=x, and x' represents the sample from the distribution with the desired mean and standard deviation.
这篇关于Random.nextgaussian()可以从均值和标准差不同的分布中采样值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!