Boost.Accumulators is both a library for incremental statistical computation as well as an extensible framework for incremental calculation in general. The library deals primarily with the concept of an accumulator, which is a primitive computational entity that accepts data one sample at a time and maintains some internal state. These accumulators may offload some of their computations on other accumulators, on which they depend. Accumulators are grouped within an accumulator set. Boost.Accumulators resolves the inter-dependencies between accumulators in a set and ensures that accumulators are processed in the proper order.

The rolling mean is the mean over the last N samples. It is computed by dividing the rolling sum by the rolling count.

Lazy
or iterative calculation of the mean over the last N samples. The lazy
calculation is associated with the tag::lazy_rolling_mean feature, and
the iterative calculation (which is the default) with the
tag::immediate_rolling_mean feature. Both can be extracted using the
tag::rolling_mean() extractor.
 
把连续取得的N个采样值看成一个队列,队列的长度固定为N,
每次采样到一个新数据放入队尾,并扔掉原来队首的一次数据(先进先出原则),
把队列中的N个数据进行算术平均运算,获得新的滤波结果。

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

using namespace boost::accumulators;

int main()
{
        accumulator_set<int, stats<tag::rolling_mean> > acc(tag::rolling_window::window_size = 7);

// push in some data ...
        acc(1);
        acc(2);
        acc(3);
        std::cout << "Mean: " << rolling_mean(acc) << std::endl;

acc(4);
        acc(5);
        acc(6);
        acc(7);
        std::cout << "Mean: " << rolling_mean(acc) << std::endl;

return 0;
}
输出
Mean: 2
Mean: 4

#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/moment.hpp>

using namespace boost::accumulators;

int main()
{
    // Define an accumulator set for calculating the mean and the
    // 2nd moment ...
    accumulator_set<double, stats<tag::mean, tag::moment<2> > > acc;

// push in some data ...
    acc(1.2);
    acc(2.3);
    acc(3.4);
    acc(4.5);

// Display the results ...
    std::cout << "Mean: " << mean(acc) << std::endl;
    std::cout << "Moment: " << moment<2>(acc) << std::endl;

return 0;
}

结果

Mean: 2.85
Moment: 9.635

----------------------

Usage of the framework follows the following pattern:

1. Users build a computational object, called an accumulator_set<>, by selecting the
       computations in which they are interested, or authoring their own computational
       primitives which fit within the framework.

2. Users push data into the accumulator_set<> object one sample at a time.
    
   3. The accumulator_set<> computes the requested quantities in the most efficient method
        possible, resolving dependencies between requested calculations, possibly caching
        intermediate results.

The
Accumulators Framework defines the utilities needed for defining
primitive computational elements, called accumulators. It also provides
the accumulator_set<> type, described above.

// In header: <boost/accumulators/framework/accumulator_set.hpp>

template<typename Sample, typename Features, typename Weight>
struct accumulator_set {
  // types
  typedef Sample sample_type; // The type of the samples that will be accumulated.
  typedef Features features_type; // An MPL sequence of the features that should be accumulated.
  typedef Weight weight_type; // The type of the weight parameter. Must be a scalar. Defaults to void.
  typedef void result_type;

// member classes/structs/unions
  template<typename Feature>
  struct apply {
  };

可见 accumulator_set 是个类模板。模板的第一个参数表示样本的类型,
use the features<> template to specify a list of features to be calculated

template <class T>
class stats
{
public:
   stats()
      : m_min(tools::max_value<T>()),
        m_max(-tools::max_value<T>()),
        m_total(0),
        m_squared_total(0),
        m_count(0)
   {}
...

stats 也是一个类模版,T = Tag::mean 是参数类型。

namespace tag
{
    struct mean
      : depends_on<count, sum>
    {
        /// INTERNAL ONLY
        ///
        typedef accumulators::impl::mean_impl<mpl::_1, sum> impl;
    };

struct immediate_mean
      : depends_on<count>
    {
        /// INTERNAL ONLY
        ///
        typedef accumulators::impl::immediate_mean_impl<mpl::_1, tag::sample> impl;
    };

由此可见 tag 是一种命名空间 mean 是一个结构体

-------------------------关于 tag::moment< para > --------------------

Header

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

Example

accumulator_set<int, stats<tag::moment<2> > > acc1;

acc1(2); // 4
acc1(4); // 16
acc1(5); // + 25
         // = 45 / 3 = 15

BOOST_CHECK_CLOSE(15., accumulators::moment<2>(acc1), 1e-5);

-----------------------------------------------------------------
accumulator_set<int, stats<tag::moment<5> > > acc2;

acc2(2); // 32
acc2(3); // 243
acc2(4); // 1024
acc2(5); // + 3125
         // = 4424 / 4 = 1106

BOOST_CHECK_CLOSE(1106., accumulators::moment<5>(acc2), 1e-5);

可见 tag::moment< para > 是一个结构体 就是一个类模板, 模板的参数para指定了矩的类型,
para = 2 是二次矩,也就是方差。

boost的accumulator rolling_mean的使用的更多相关文章

  1. boost number handling

    Boost.Integer defines specialized for integers. 1. types for integers with number of bits #include & ...

  2. boost强分类器的实现

    boost.cpp文件下: bool CvCascadeBoost::train( const CvFeatureEvaluator* _featureEvaluator, int _numSampl ...

  3. Boost信号/槽signals2

    信号槽是Qt框架中一个重要的部分,主要用来解耦一组互相协作的类,使用起来非常方便.项目中有同事引入了第三方的信号槽机制,其实Boost本身就有信号/槽,而且Boost的模块相对来说更稳定. signa ...

  4. 玩转Windows服务系列——使用Boost.Application快速构建Windows服务

    玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务.Debug.Release版本的注册和卸载,及其原理和服务运行.停止流程浅析分别介绍了Wi ...

  5. boost::function的用法

    本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1.  介绍 Boost.Func ...

  6. Boost条件变量condition_variable_any

    Boost条件变量可以用来实现线程同步,它必须与互斥量配合使用.使用条件变量实现生产者消费者的简单例子如下,需要注意的是cond_put.wait(lock)是在等待条件满足.如果条件不满足,则释放锁 ...

  7. 新手,Visual Studio 2015 配置Boost库,如何编译和选择,遇到无法打开文件“libboost_thread-vc140-mt-gd-1_63.lib“的解决办法

    1,到官网下载最新的boost,www.boost.org 这里我下载的1-63版本. 2,安装,解压后运行bootstrap.bat文件.稍等一小会就OK. 3,编译boost库.注意一定要使用VS ...

  8. boost.python笔记

    boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...

  9. vs2013给项目统一配置boost库

    1.打开项目,然后点击菜单中的 视图->其他窗口->属性管理器 2. 打开属性管理器,点击项目前的箭头,展开项目,找到debug或者release下面的Microsoft.Cpp.Win3 ...

随机推荐

  1. Java volatile关键字的用法

    volatile不能解决同步问题 如果想要理解volatile关键字的作用不得不先了解Java内存模型 摘抄一下来自百度百科的话 在本次线程内,当读取一个变量时,为提高存取速度,编译器优化时有时会先把 ...

  2. C语言标准io函数总结

    转自:http://blog.csdn.net/sun_top/article/details/4235992本来是在vscode上用markdown排好版的,结果复制到这上面就变了形,无奈. 函数列 ...

  3. javascript 表格排序和表头浮动效果(扩展SortTable)

    前段时间一个项目有大量页面用到表格排序和表头浮动的效果,在网上找了几个表格排序的js代码,最后选择了 Stuart Langridge的SortTable,在SortTable基础上做了些扩展,加上了 ...

  4. 全国高校绿色计算大赛 预赛第二阶段(Python)第1关:统计分数的麻烦

    挑战任务 “绿盟杯”比赛过后,赛事承办方的各位工作人员们就开始分头统计各个参赛队伍和同学的成绩了.赛事规模很大,有10000个队伍参加.经过工作人员认真负责的统计,本来已经统计好了这一万个队伍的分数和 ...

  5. FZU-1752.(A^B mod C)(快速幂与快速乘优化)

    我把自己演哭了... 心酸.jpg 写了很多个版本的,包括数学公式暴力,快速幂TLE等等,最后想到了优化快速幂里的乘法,因为会爆longlong,但是和别人优化的效率简直是千差万别...? 本题大意: ...

  6. 151. Reverse Words in a String (String)

    思路: 本题考查的目的并不是使用字符串的函数.方法是两次reverse,先对每个单词先做一次翻转,然后对整个字符串做一次翻转. 需要注意的是去除extra space,并且对全space字符串.以及最 ...

  7. PHP面向对象之类的自动加载

    类的自动加载 含义: 当某行代码需要一个类的时候,php的内部机制可以做到“自动加载该类文件”,以满足该行需要一个类的这种需求. 什么时候需要一个类? 1,new一个对象的时候: 2,使用一个类的静态 ...

  8. VS中,添加完Web引用(WebServer引用/Web服务引用),写代码时引用不到

    VS中,添加完Web引用(WebServer引用/Web服务引用),写代码时引用不到 添加完之后要等一会儿 等一会儿 等一会儿 就有了

  9. linux命令学习之:ps

    Linux中的ps命令是Process Status的缩写.ps命令用于报告当前系统的进程状态,列出系统中当前运行的那些进程.可以搭配kill指令随时中断.删除不必要的程序. 要对进程进行监测和控制, ...

  10. oracle学习之数据库数据保存成文件

    常常需要将数据库中的数据生成文档,由于比较喜欢脚本的方式,所以就需要使用spool的时候进行格式设置,以下简单整理了一下oracle中进行格式设置的一些东西,一共十八条,其实常用的也就那么几个,稍后会 ...