Подтвердить что ты не робот

Ошибка в стандартной библиотеке С++ в std:: poisson_distribution?

Я думаю, что я столкнулся с неправильным поведением std:: poisson_distribution из стандартной библиотеки С++.

Вопросы:

  • Не могли бы вы подтвердить, что это действительно ошибка, а не моя ошибка?
  • Что именно неправильно в стандартном библиотечном коде функции poisson_distribution, если предположить, что это действительно ошибка?

Детали:

Следующий код С++ (файл poisson_test.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;
  }
}

Я компилирую этот код следующим образом:

clang++ -o poisson_test -std=c++11 poisson_test.cc
./poisson_test > mypoisson.txt

Следующий python script использовался для анализа последовательности случайных чисел из файла 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

script выводит следующий график:

Вы можете видеть проблему неважно. Отклонение при n = 158 статистически значимо, это фактически отклонение 22σ!

You can see the problem by bare eye. The deviation at n = 158 is statistically significant, it is in fact a 22σ deviation!

Крупный план предыдущего графика.

Close-up of the previous plot.

4b9b3361

Ответ 1

Моя система настроена следующим образом (тестирование Debian):

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

Я могу подтвердить ошибку при использовании 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

Результат:

введите описание изображения здесь