0

boost::histogram在 [-3.5,3.5] 范围内有 100 个垃圾箱。我用数据向量填充它qp。由于我使用周期性 BC,所有 q in 的值qp都在 [-3.5,3.5] 中。

auto h = boost::histogram::make_histogram(boost::histogram::axis::regular<>(100, -3.5, 3.5));
for (auto&& value : qp)
    h(value.first);

为了安全起见,我计算了垃圾箱中的所有

int samplesize = 0;
for (auto&& it : indexed(h)) 
    samplesize += *it;

我为情节准备数据

for (auto&& it : indexed(h)) {
    const auto bin = it.bin(0_c);
    double xaxis = bin.lower();
    double density = *it / samplesize;
    const std::pair<double, double> paar = std::make_pair(xaxis,density);
    printToStream(file, paar);
}

结果让我很困惑。它应该是一个归一化的概率分布,但绝对不是(y 轴上的值太低了)

在此处输入图像描述

有没有一种boost方法可以自动获得(标准化)相对频率?

4

1 回答 1

1

您需要除以垃圾箱的宽度。所以需要修改密度:

double density = *it / bin.width() / samplesize;

这是 boost-histogram 的 Python 绑定中的完整代码,因为它可以更快地模拟:

import boost_histogram as bh
import numpy as np
import matplotlib.pyplot as plt

h = bh.Histogram(bh.axis.Regular(100, -3.5, 3.5))

data = np.concatenate([
    np.random.normal(-.75,.3, 1_000_000),
    np.random.normal(.75,.3, 750_000),
    np.random.normal(-1.5,.2, 200_000),
])

h.fill(data)

# Compute the areas of each bin (any number of axes)
areas = np.prod(h.axes.widths, axis=0)

# Compute the density (any number of axes)
density = h.view() / h.sum() / areas

plt.plot(h.axes.centers[0], density)

Python 版本的示例

PS:如果您的数据是圆形的,您可以使用圆形轴

于 2020-06-03T01:30:36.870 回答