C+中的Bug+;标准库在标准::泊松分布? 我认为我遇到过一个不正确的STD::C++标准库中的泊松分布。 问题: 你能确认这确实是一个错误而不是我的错误吗 假设poisson_分布函数的标准库代码确实是一个bug,那么它到底错在哪里 细节: < P>以下C++代码(文件Poisson测试)CC生成泊松分布数: #include <array> #include <cmath> #include <iostream> #include <random> int main() { // The problem turned out to be independent on the engine std::mt19937_64 engine; // Set fixed seed for easy reproducibility // The problem turned out to be independent on seed engine.seed(1); std::poisson_distribution<int> distribution(157.17); for (int i = 0; i < 1E8; i++) { const int number = distribution(engine); std::cout << number << std::endl; } }

C+中的Bug+;标准库在标准::泊松分布? 我认为我遇到过一个不正确的STD::C++标准库中的泊松分布。 问题: 你能确认这确实是一个错误而不是我的错误吗 假设poisson_分布函数的标准库代码确实是一个bug,那么它到底错在哪里 细节: < P>以下C++代码(文件Poisson测试)CC生成泊松分布数: #include <array> #include <cmath> #include <iostream> #include <random> int main() { // The problem turned out to be independent on the engine std::mt19937_64 engine; // Set fixed seed for easy reproducibility // The problem turned out to be independent on seed engine.seed(1); std::poisson_distribution<int> distribution(157.17); for (int i = 0; i < 1E8; i++) { const int number = distribution(engine); std::cout << number << std::endl; } },c++,c++11,libstdc++,c++-standard-library,C++,C++11,Libstdc++,C++ Standard Library,以下python脚本用于分析文件mypoisson.txt中的随机数序列: import numpy as np import matplotlib.pyplot as plt def expectation(x, m): " Poisson pdf " # Use Ramanujan formula to get ln n! lnx = x * np.log(x) - x + 1./6. * np.log(x * (1 + 4*x*(1+2*x))) + 1./2.

以下python脚本用于分析文件mypoisson.txt中的随机数序列:

import numpy as np
import matplotlib.pyplot as plt

def expectation(x, m):
    " Poisson pdf " 
    # Use Ramanujan formula to get ln n!
    lnx = x * np.log(x) - x + 1./6. * np.log(x * (1 + 4*x*(1+2*x))) + 1./2. * np.log(np.pi)
    return np.exp(x*np.log(m) - m - lnx)

data = np.loadtxt('mypoisson.txt', dtype = 'int')

unique, counts = np.unique(data, return_counts = True)
hist = counts.astype(float) / counts.sum()
stat_err = np.sqrt(counts) / counts.sum()
plt.errorbar(unique, hist, yerr = stat_err, fmt = '.', \
             label = 'Poisson generated \n by std::poisson_distribution')
plt.plot(unique, expectation(unique, expected_mean), \
         label = 'expected probability \n density function')
plt.legend()
plt.show()

# Determine bins with statistical significance of deviation larger than 3 sigma
deviation_in_sigma = (hist - expectation(unique, expected_mean)) / stat_err
d = dict((k, v) for k, v in zip(unique, deviation_in_sigma) if np.abs(v) > 3.0)
print d
该脚本生成以下绘图:

你可以用肉眼看到这个问题。n=158处的偏差在统计上是显著的,实际上是22σ的偏差

上一个情节的特写镜头


我的系统设置如下(Debian测试):

我可以在使用
libstdc++
时确认该错误:

g++ -o pois_gcc -std=c++11 pois.cpp
clang++ -o pois_clang -std=c++11 -stdlib=libstdc++ pois.cpp
clang++ -o pois_clang_libc -std=c++11 -stdlib=libc++ pois.cpp
结果:


它是哪个标准库?好吧,Clang倾向于在Linux上使用libstdc++和在Mac上使用libc++。@chris我在Ubuntu上,我已经检查过它是libstdc++。打印出GLIBCXX将给出20160609。关于CLAN版本,“CLAN-V”给出了“CLAN版本3.82.0~UBUTU4(标签/RelaseE380/最终)”。您能重现MAC上的bug吗?我已经用Visual C++ 2017(32位构建)编译和运行了,而我没有得到异常值。(158的值约为0.03166,略低于157的值,即0.03182)。就数学而言,泊松分布不是连续的,而是离散的,因此不具有连续性。不过,你可以计算出价格。我意识到你的线图有助于引导眼睛,但泊松分布确实有非整数变量,因此绘制一条连续线有点误导。查看源代码:
//NB:这种情况不在书中,也不在勘误表中,但应该可以…
-我对手头的问题一无所知(除了一些关于接受/拒绝算法的非常基本的uni内容外),但正是这种说法让我感到紧张…:o)你有那个bug吗?你也许应该!然后在你的问题中给出它的URL!荣誉不属于我。如果@DmytroOliinychenko愿意的话,我会等他报告这件事。但如果他决定不这样做,我也可以这样做。报告给GCC bugtracker。@ChristophTerasa请链接到bug reportGCC bugtracker链接:
libstdc++-7-dev:
  Installed: 7.2.0-16

libc++-dev:
  Installed: 3.5-2

clang:
  Installed: 1:3.8-37

g++:
  Installed: 4:7.2.0-1d1
g++ -o pois_gcc -std=c++11 pois.cpp
clang++ -o pois_clang -std=c++11 -stdlib=libstdc++ pois.cpp
clang++ -o pois_clang_libc -std=c++11 -stdlib=libc++ pois.cpp