0

Histogram我的代码中创建了一个类,该类旨在作为boost::accumulators::accumulator_setBoost 1.54 的包装器。对我的问题来说似乎很重要的是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))) {
}

使用此直方图 ( do_iterations()in main-metropolis.cpp) 的代码以以下内容开头:

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以一种非常奇怪的方式工作。bins 全部为零,但数据被分配到第一个和最后一个 bin 的溢出 bin 中:

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请在浏览代码时使用修订版,因为我现在构建了自己的直方图实现以继续工作。

4

1 回答 1

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

此外,当给两个蓄能器提供不同的样品时,我无法让它显示任何明显的故障。

希望这可以帮助您了解您的情况有何不同(例如,您实际上是否为两个累加器提供了正确的样本?)

我用 boost 1.53-1.55 测试过

于 2014-04-03T20:56:59.423 回答