使用Boost的累加器,我可以轻松地计算加权或非加权输入集的统计量。我想知道是否有可能在同一个累加器内混合加权量和未加权量。从文档的角度看,情况似乎并非如此。
这编译得很好,但却产生了我所希望的另一个结果:
using namespace boost::accumulators;
const double a[] = {1, 1, 1, 1, 1, 2, 2, 2, 2};
const double w[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
accumulator_set<double, features<tag::sum, tag::weighted_sum>, double> stats;
for (size_t i=0; i<9; ++i)
stats(a[i], weight = w[i]);
std::cout << sum(stats) <<" "<< weighted_sum(stats) << std::endl;
// outputs "75 75" instead of "13 75"此外,使用accumulator_set的第三个模板参数,即使使用“未加权”特性和提取器,我似乎总是获得加权数量:
accumulator_set<double, features<tag::sum>, double> stats;
for (size_t i=0; i<9; ++i)
stats(a[i], weight = w[i]);
std::cout << sum(stats) << std::endl;
// outputs "75" instead of 13如果要同时计算加权量和未加权量,我总是必须使用两个不同的累加器吗?
编辑--我只是以sum为例,实际上我对多个、更复杂的数量感兴趣。
发布于 2010-06-23 05:56:25
它确实在文档中说
指定权重时,集合中的所有累加器都将替换为它们的加权等效项。
也许有更好的方法可以这样做,但您可以尝试这样的方法(基本上将值的含义与权重的含义交换):
accumulator_set< double, stats< tag::sum, tag::sum_of_weights >, double > acc;
const double a[] = {1, 1, 1, 1, 1, 2, 2, 2, 2};
const double w[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
for( int i = 0; i < sizeof( a ) / sizeof( a[ 0 ] ); i++ )
acc( w[ i ], weight = a[ i ] );
std::cout << extract_result< tag::sum >( acc ) << std::endl; // weighted sum, prints 75
std::cout << extract_result< tag::sum_of_weights >( acc ) << std::endl; // sum, prints 13https://stackoverflow.com/questions/3098940
复制相似问题