我在my code中创建了一个类Histogram
,用作Boost 1.54中boost::accumulators::accumulator_set
的包装器。对我的问题似乎重要的事情是来自Histogram.hpp
文件的那些行:
using namespace boost::accumulators;
class Histogram {
public:
Histogram(int bins, size_t cache);
accumulator_set<double,
features<tag::min, tag::max, tag::mean, tag::density>> acc;
};
然后在Histogram.cpp
我有构造函数:
Histogram::Histogram(int bins, size_t cache)
: acc(accumulator_set<double,
features<tag::min, tag::max, tag::mean, tag::density>>(
tag::density::num_bins = bins,
tag::density::cache_size = std::min(cache, MAX_CACHE_ENTRIES))) {
}
使用此直方图的代码(main-metropolis.cpp
中的do_iterations()
)从以下内容开始:
Histogram position_histogram{settings.position_hist_bins, settings.time_sites * settings.iterations};
//Histogram action_histogram{settings.action_hist_bins, settings.iterations};
当我在第二行停用的情况下运行时,它就像我期望的那样工作。我的模拟会生成一些数据点,将其放入Histogram::acc
并让我随后将其提取出来:
-2.86958 0
-2.37393 0.0002
-1.87829 0.0071
-1.38265 0.06621
-0.887001 0.23902
-0.391356 0.33247
0.104288 0.2342
0.599932 0.08449
1.09558 0.02843
1.59122 0.00775
2.08687 0.00012
2.58251 1e-05
# Min -2.37393
# Max 2.58251
# Mean -0.0809983
然后我激活该行,position_histogram
以一种非常奇怪的方式工作。这些箱都是零,但数据被分配到第一个和最后一个箱中的溢流箱:
0 0.57785
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0.42215
# Min -2.37393
# Max 2.58251
# Mean -0.0809983
如果我换掉这些线,那么action_histogram
会断开。所以第二个总是打破第一个。为什么初始化第二个Histogram
因此第二个accumulator_set
会导致第一个d3081a1ef7
中断?
请在浏览the code时使用修订版{{1}},因为我现在构建自己的直方图实现以继续工作。
答案 0 :(得分:2)
您将不得不对此进行调试或提供更多信息。
我在研究概念验证中使用了累加器并且总是同时使用多个实例,而我没有遇到过这种情况。然后我意识到我从未做过density
直方图,所以我测试了它。
根据您的声明在我的测试中进行调查,看到它 Live On Coliru :
#include <boost/accumulators/statistics.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/random.hpp>
#include <boost/bind.hpp>
using namespace boost::accumulators;
static const size_t MAX_CACHE_ENTRIES = 32;
class Histogram {
public:
Histogram(int bins, size_t cache)
: acc(accumulator_set<double,
features<tag::min, tag::max, tag::mean, tag::density>>(
tag::density::num_bins = bins,
tag::density::cache_size = std::min(cache, MAX_CACHE_ENTRIES))) {
}
accumulator_set<double,
features<tag::min, tag::max, tag::mean, tag::density>> acc;
};
int main()
{
Histogram position_histogram { 10, 32 };
Histogram action_histogram { 10, 32 };
auto random = boost::bind(boost::uniform_real<double>(-100,100), boost::mt19937(42));
size_t samples = 1<<20;
while (samples--)
{
auto v = random();
position_histogram.acc(v);
action_histogram.acc(v);
}
for (auto& acc : { position_histogram.acc, action_histogram.acc })
{
auto hist = density(acc);
double total = 0.0;
for( int i = 0; i < hist.size(); i++ )
{
std::cout << "Bin lower bound: " << hist[i].first << ", Value: " << hist[i].second << std::endl;
total += hist[i].second;
}
std::cout << "Total: " << total << std::endl; //should be 1 (and it is)
}
}
按预期输出:
Bin lower bound: -119.673, Value: 0.000766754
Bin lower bound: -99.8442, Value: 0.099205
Bin lower bound: -80.0156, Value: 0.0987797
Bin lower bound: -60.1869, Value: 0.0990477
Bin lower bound: -40.3583, Value: 0.0991993
Bin lower bound: -20.5296, Value: 0.0989904
Bin lower bound: -0.700967, Value: 0.0993652
Bin lower bound: 19.1277, Value: 0.0993567
Bin lower bound: 38.9563, Value: 0.0993252
Bin lower bound: 58.785, Value: 0.0993109
Bin lower bound: 78.6137, Value: 0.0989342
Bin lower bound: 98.4423, Value: 0.00771904
Total: 1
Bin lower bound: -119.673, Value: 0.000766754
Bin lower bound: -99.8442, Value: 0.099205
Bin lower bound: -80.0156, Value: 0.0987797
Bin lower bound: -60.1869, Value: 0.0990477
Bin lower bound: -40.3583, Value: 0.0991993
Bin lower bound: -20.5296, Value: 0.0989904
Bin lower bound: -0.700967, Value: 0.0993652
Bin lower bound: 19.1277, Value: 0.0993567
Bin lower bound: 38.9563, Value: 0.0993252
Bin lower bound: 58.785, Value: 0.0993109
Bin lower bound: 78.6137, Value: 0.0989342
Bin lower bound: 98.4423, Value: 0.00771904
Total: 1
另外,当给两个蓄电池输送不同的样品时,我无法显示任何明显的故障。
希望这有助于您了解您的情况有何不同(例如,您是否真的为两个蓄电池提供正确的样品?)
我用1.53-1.55增强测试