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 是二次矩,也就是方差。

最新文章

  1. DOM事件
  2. linux用户不在sudoers文件中
  3. Ajax_showHint() 函数
  4. Stanford NLP学习笔记1:课程介绍
  5. Hibernate之Annotation(注解的方式,非映射)
  6. bzoj2219: 数论之神
  7. 第三方网站不能调用微信公众平台里的图片了 显示&quot;此图片来自微信公众号平台未经允许不可引用&quot;
  8. 表设计VIso
  9. PHP提取身份证号码中的生日并验证是否成年的函数
  10. WebService的优点和基本原理
  11. Delphi新语法
  12. ZAB协议(转)
  13. usaco5.5-Hidden Passwords
  14. Node.js 启动小结
  15. Spring Boot实战:模板引擎
  16. iOS聊天客服功能(Udesk)
  17. python之https爬虫出现 SSL: CERTIFICATE_VERIFY_FAILED (同时打开fiddler就会出现)
  18. SQL Server 各种时间业务处理
  19. JavaBasic_02
  20. (转)GCT之逻辑经验总结(拿来主义)

热门文章

  1. powerdesigenr设置主外键颜色
  2. Mac上安装Git
  3. CentOS Find命令
  4. Codeforces977D ---Divide by three, multiply by two 深搜+map存出现的数
  5. PHPlaravel中从数据库中选择数据是增加时间条件及各种条件
  6. Codeforces Beta Round #59 (Div. 2)
  7. Django import相关
  8. sql查询 !=&#39;&#39; 和 is not null的区别
  9. AnguarJS——第10章 路由
  10. JDBC的学习