c++ - boost::accumulators::rolling_mean 返回不正确的平均值

标签 c++ boost boost-accumulators

环境:VS 2013,Boost 1.58

我写了一些东西,为 Boost 的累加器提供了一个更友好的界面,它可用于在窗口上投影总和,并计算窗口上的实际滚动平均值。在插入将 VS 2013 作为我们的主要编译器的过程中,此类的单元测试之一开始失败。剥离层,我把它缩小到这个最小的例子:

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>

namespace ba = boost::accumulators;
namespace bt = ba::tag;
typedef ba::accumulator_set < uint32_t, ba::stats <bt::rolling_mean > > MeanAccumulator;

int main() {

    MeanAccumulator acc(bt::rolling_window::window_size = 5u);

    for (uint32_t i : { 3, 2, 1, 0 }) {
        acc(i);
        std::cout << i << " actualMean: " << std::fixed << boost::accumulators::rolling_mean(acc) << "\n";
    }
}

在循环的最后一次通过时,我没有得到预期的平均值 (1.5),而是得到了一个非常高的值 (1431655766.333333)。

此代码在带有 Boost 1.49 的 VS 2008 中正确执行(显然替换了 C++11 vector 初始化),但在带有 Boost 1.58 的 VS 2012 和 VS 2013 中执行失败。我无法解释此故障,因此无法修复它。

其他有趣的点:

  • 手动检查累加器内的内存值表明其循环缓冲区中包含正确的数据。
  • 如果放入累加器的数据按递增顺序排列,则 rolling_mean 将是正确的。
  • 一旦窗口填满,如果任何新元素小于它从窗口中敲出的数字,则 rolling_mean 将不正确。如果等于或大于,则 rolling_mean 将是正确的。

这似乎是一个 Boost 错误,但我想在报告错误或尝试构建 Boost 1.59 之前验证我没有做愚蠢的事情。提前致谢!

编辑:感谢您的回复,因为这确实是一个 Boost 错误。相关的 Boost 票是 here. .这是一个与带累加器的无符号整数相关的问题。具体来说,如果在窗口填满后添加到累加器的值严格小于窗口中已经存在的所有值,则 rolling_mean 调用将返回无效结果。

有一个变通方法,即不对累加器使用无符号整数。这解决了我的问题,非常感谢您的帮助!

最佳答案

显然那里潜伏着一个错误,可能在编译器中,但更有可能在库中,因为我已经能够在 GCC 上重现它:

Live On Coliru

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>

namespace ba = boost::accumulators;
namespace bt = ba::tag;
typedef ba::accumulator_set < uint32_t, ba::stats <bt::rolling_mean > > MeanAccumulator;

int main() {

    MeanAccumulator acc(bt::rolling_window::window_size = 5u);

    for (uint32_t i : { 252, 189, 248, 154, 620, 885, 939, 196 }) {
        acc(i);
        std::cout << i << " actualMean: " << std::fixed << boost::accumulators::rolling_mean(acc) << "\n";
    }
}

打印

g++-5.2 -std=c++1y -O2 -Wall -pedantic main.cpp && ./a.out
252 actualMean: 252.000000
189 actualMean: 220.500000
248 actualMean: 229.666667
154 actualMean: 210.750000
620 actualMean: 292.600000
885 actualMean: 419.200000
939 actualMean: 569.200000
196 actualMean: 858994018.000000

现在问题似乎与 unsigned 样本类型的选择有关`:将其更改为 signed 可消除症状: Live On Coliru


简而言之:我会在 boost 邮件列表或 Trac 中报告此问题:https://svn.boost.org/trac/boost/

关于c++ - boost::accumulators::rolling_mean 返回不正确的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32957465/

相关文章:

c++ - OpenSceneGraph - 真实大小的对象

c++ - 无法获得boost::spirit解析器和词法分析器,用于除std::string或int或double以外的 token 类型

c++ - boost 文件系统中的段错误?

c++ - 将 Boost 累加器与 Eigen::Vector 类型一起使用

c++ - 使用空的 boost::accumulators

c++ - 链表深拷贝构造函数

c++ - 如何在调用 SQLGetData 之前知道数据的大小

c++ - 使用一个 for 循环如何迭代和测试迭代器值?

派生类中的C++成员函数模板,如何从基类中调用它?

c++ - 如何在 boost 累加器中使用/访问用户参数?