C++ 如何使用boost正态分布类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2078474/
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-08-27 22:02:01  来源:igfitidea点击:

How to use boost normal distribution classes?

c++booststatisticsnormal-distribution

提问by davidag

I'm trying to use boost::normal_distribution in order to generate a normal distribution with mean 0 and sigma 1.

我正在尝试使用 boost::normal_distribution 来生成均值为 0 和 sigma 1 的正态分布。

The following code doesn't work as some values are over or beyond -1 and 1 (and shouldn't be). Could someont point out what I am doing wrong?

以下代码不起作用,因为某些值超过或超过 -1 和 1(并且不应该)。有人能指出我做错了什么吗?

#include <boost/random.hpp>
#include <boost/random/normal_distribution.hpp>

int main()
{
  boost::mt19937 rng; // I don't seed it on purpouse (it's not relevant)

  boost::normal_distribution<> nd(0.0, 1.0);

  boost::variate_generator<boost::mt19937&, 
                           boost::normal_distribution<> > var_nor(rng, nd);

  int i = 0; for (; i < 10; ++i)
  {
    double d = var_nor();
    std::cout << d << std::endl;
  }
}

The result on my machine is:

我机器上的结果是:

0.213436
-0.49558
1.57538
-1.0592
1.83927
1.88577
0.604675
-0.365983
-0.578264
-0.634376

As you can see all values are not between -1 and 1.

如您所见,所有值都不在 -1 和 1 之间。

Thank you all in advance!

谢谢大家!

EDIT: This is what happens when you have deadlines and avoid studying the theory before doing the practice.

编辑:这就是当您有截止日期并避免在进行实践之前学习理论时会发生的情况。

回答by jason

The following code doesn't work as some values are over or beyond -1 and 1 (and shouldn't be). Could someont point out what I am doing wrong?

以下代码不起作用,因为某些值超过或超过 -1 和 1(并且不应该)。有人能指出我做错了什么吗?

No, this is a misunderstanding of the standard deviation (the second parameter in the constructor1) of the normal distribution.

不,这是对正态分布的标准偏差(构造函数1 中的第二个参数)的误解。

The normal distribution is the familiar bell curve. That curve effectively tells you the distribution of values. Values close to where the bell curve peaks are more likely than values far away (the tail of the distribution).

正态分布是熟悉的钟形曲线。该曲线有效地告诉您值的分布。靠近钟形曲线峰值的值比远离(分布的尾部)的值更有可能。

The standard deviation tells you how spread out the values are. The smaller the number, the more concentrated values are around the mean. The larger the number, the less concentrated values are around the mean. In the image below you see that the red curve has a variance (variance is the square of the standard deviation) of 0.2. Compare this to the green curve which has the same mean but a variance of 1.0. You can see that the values in the green curve are more spread out relative to the red curve. The purple curve has variance 5.0 and the values are even more spread out.

标准偏差告诉您值的分布情况。数字越小,均值附近的值越集中。数字越大,平均值附近的值越不集中。在下图中,您可以看到红色曲线的方差(方差是标准差的平方)为 0.2。将此与具有相同均值但方差为 1.0 的绿色曲线进行比较。您可以看到绿色曲线中的值相对于红色曲线更加分散。紫色曲线的方差为 5.0,并且值更加分散。

So, this explains why the values are not confined to [-1, 1]. It is, however, an interesting fact that 68% of the values are always within one standard deviation of the mean. So, as an interesting test for yourself write a program to draw a large number of values from a normal distribution with mean 0 and variance 1 and count the number that are within one standard deviation of the mean. You should get a number close to 68% (68.2689492137% to be a little more precise).

因此,这解释了为什么这些值不限于[-1, 1]. 然而,一个有趣的事实是,68% 的值总是在平均值的一个标准偏差内。因此,作为一个有趣的测试,您可以编写一个程序,从均值为 0 且方差为 1 的正态分布中提取大量值,并计算均值一个标准偏差内的数字。你应该得到一个接近 68% 的数字(更精确一点是 68.2689492137%)。

alt text

替代文字

1: From the boost documentation:

1:来自boost文档

normal_distribution(RealType mean = 0, RealType sd = 1);

Constructs a normal distribution with mean mean and standard deviation sd.

normal_distribution(RealType mean = 0, RealType sd = 1);

构造具有均值和标准差 sd 的正态分布。

回答by Jim Lewis

You're not doing anything wrong. For a normal distribution, sigma specifies the standard deviation, not the range. If you generate enough samples, you will see that only about 68% of them lie in the range [mean - sigma, mean + sigma], about 95% within 2 sigma, and more than 99% within 3 sigma.

你没有做错任何事。对于正态分布,sigma 指定标准偏差,而不是范围。如果您生成足够多的样本,您将看到其中只有大约 68% 位于 [mean - sigma, mean + sigma] 范围内,大约 95% 在 2 sigma 内,超过 99% 在 3 sigma 内。