java Random.nextgaussian() 可以从具有不同均值和标准差的分布中采样值吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31754209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:04:18  来源:igfitidea点击:

Can Random.nextgaussian() sample values from a distribution with different mean and standard deviation?

javastatisticsgaussianrandom-sample

提问by darkhipo

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?

这是一个结合了 Java 和基本数学的问题。Random.nextGaussian() 的文档说明它从均值为 0 和标准差为 1 的正态分布中采样。如果我想从具有不同均值和方差的正态分布中采样怎么办?

回答by darkhipo

The short answer is

简短的回答是

Random r = new Random();
double mySample = r.nextGaussian()*desiredStandardDeviation+desiredMean;

For example this answer is given here: http://www.javamex.com/tutorials/random_numbers/gaussian_distribution_2.shtml

例如这里给出了这个答案:http: //www.javamex.com/tutorials/random_numbers/gaussian_distribution_2.shtml

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?

我真的不明白为什么这会起作用,但是在仔细研究了一下之后,我想我明白了。样本点的均值为0,标准差为1;这意味着原始样本也是它自己的 z-score ( https://en.wikipedia.org/wiki/Standard_score)。引用维基百科的“z 的绝对值表示原始分数与总体平均值之间的距离,以标准差为单位”。公式为 z=(x-mean)/stdev,因此使用默认值 z=x。如果我们想保留样本的 z 分数但改变均值和标准差,我们会怎么做?

z*stdev + mean = x' where z=x, and x' represents the sample from the distribution with the desired mean and standard deviation.

z*stdev + mean = x' 其中 z=x,x' 代表来自具有所需均值和标准差的分布的样本。