boost::accumulators 用法学习

 1 #include <iostream>
 2 #include <boost/accumulators/accumulators.hpp>
 3 #include <boost/accumulators/statistics/stats.hpp>
 4 #include <boost/accumulators/statistics/mean.hpp>
 5 #include <boost/accumulators/statistics/moment.hpp>
 6 using namespace boost::accumulators;
 7 
 8 int main()
 9 {
10     // Define an accumulator set for calculating the mean and the
11     // 2nd moment ...
12     accumulator_set<double, stats<tag::mean, tag::moment<2> > > acc;//创建对象
13 
14     // push in some data ...
15     acc(1.2);
16     acc(2.3);
17     acc(3.4);
18     acc(4.5);
19 
20     // Display the results ...
21     std::cout << "Mean:   " << mean(acc) << std::endl;
22     std::cout << "Moment: " << accumulators::moment<2>(acc) << std::endl;
23 
24     return 0;
25 }

运行结果:

Mean:   2.85
Moment: 9.635
计算步骤:
 1 acc(1.2);//    1.2
 2 acc(2.3);//  + 2.3
 3 acc(3.4);//  + 3.4
 4 acc(4.5);//  + 4.5
 5          // --------
 6      //   11.4/4=2.85(mean)
 7 acc(1.2);//    1.2^2
 8 acc(2.3);//  + 2.3^2
 9 acc(3.4);//  + 3.4^2
10 acc(4.5);//  + 4.5^2
11          // --------
12      //   38.54/4=2.85(moment)
总结:accumulators用于增量统计的库,也是一个用于增量计算的可扩展的累加器框架。还有很多有用的函数,可以参见reference。

Reference:http://www.boost.org/doc/libs/1_64_0/doc/html/accumulators/user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.extending_the_accumulators_framework


posted @ 2017-06-30 17:53  实事求是>_<  阅读(1841)  评论(0编辑  收藏  举报