带提升直方图的经验概率
Empirical probability with boost histogram
我有一个 boost::histogram
,在 [-3.5,3.5] 范围内有 100 个 bin。我用数据向量 qp
填充它。由于我使用周期性 BC,qp
中的所有 q 值都在 [-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
方法可以自动获得(标准化的)相对频率?
您需要除以容器的宽度。所以需要修改密度:
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)
PS: 如果你的数据是圆形的,你可以使用圆形轴
我有一个 boost::histogram
,在 [-3.5,3.5] 范围内有 100 个 bin。我用数据向量 qp
填充它。由于我使用周期性 BC,qp
中的所有 q 值都在 [-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
方法可以自动获得(标准化的)相对频率?
您需要除以容器的宽度。所以需要修改密度:
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)
PS: 如果你的数据是圆形的,你可以使用圆形轴