How to use boost normal distribution classes?(如何使用boost正态分布类?)
问题描述
我正在尝试使用 boost::normal_distribution 来生成均值为 0 和 sigma 1 的正态分布.
以下代码不起作用,因为某些值超过或超过 -1 和 1(并且不应该).有人能指出我做错了什么吗?
#include #include int main(){提升:: mt19937 rng;//我不是故意播种的(这无关紧要)boost::normal_distribution<>nd(0.0, 1.0);boost::variate_generator<boost::mt19937&,boost::normal_distribution<>>var_nor(rng, nd);int i = 0;对于 (; i <10; ++i){双 d = var_nor();std::cout <<d<<std::endl;}}
我机器上的结果是:
0.213436-0.495581.57538-1.05921.839271.885770.604675-0.365983-0.578264-0.634376
如您所见,所有值都不在 -1 和 1 之间.
先谢谢大家!
编辑:当你有截止日期并避免在进行实践之前学习理论时会发生这种情况.
以下代码不起作用,因为某些值超过或超过 -1 和 1(并且不应该).有人能指出我做错了什么吗?
不,这是对正态分布的标准差(构造函数中的第二个参数1)的误解.
正态分布是熟悉的钟形曲线.该曲线有效地告诉您值的分布.靠近钟形曲线峰值的值比远离(分布尾部)的值更有可能.
标准偏差告诉您值的分布情况.数字越小,均值附近的值越集中.数字越大,均值附近的值越不集中.在下图中,您可以看到红色曲线的方差(方差是标准差的平方)为 0.2.将此与具有相同均值但方差为 1.0 的绿色曲线进行比较.您可以看到绿色曲线中的值相对于红色曲线更加分散.紫色曲线的方差为 5.0,数值更加分散.
所以,这就解释了为什么这些值不限于
[-1, 1]
.然而,一个有趣的事实是,68% 的值总是在平均值的一个标准偏差内.因此,作为对自己的一个有趣的测试,编写一个程序从均值为 0 和方差为 1 的正态分布中提取大量值,并计算均值的一个标准偏差内的数字.您应该得到一个接近 68% 的数字(更准确地说是 68.2689492137%).>
1:来自 boost 文档:
<块引用>
normal_distribution(RealType mean = 0, RealType sd = 1);
用均值和标准差 sd 构造正态分布.
I'm trying to use boost::normal_distribution in order to generate a normal distribution with mean 0 and 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?
#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.
Thank you all in advance!
EDIT: This is what happens when you have deadlines and avoid studying the theory before doing the practice.
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?
No, this is a misunderstanding of the standard deviation (the second parameter in the constructor1) of the normal distribution.
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.
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: From the boost documentation:
normal_distribution(RealType mean = 0, RealType sd = 1);
Constructs a normal distribution with mean mean and standard deviation sd.
这篇关于如何使用boost正态分布类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用boost正态分布类?
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01