是提升图书馆的加权中值坏了吗? [英] is Boost Library's weighted median broken?
问题描述
我承认,我在C ++方面的专家。
我要寻找一个快速的方法来计算加权中值,从而加速似乎有。
但似乎我不能使它发挥作用。
的#include<&iostream的GT;
#包括LT&;升压/累加器/ accumulators.hpp>
#包括LT&;升压/累加器/统计/ stats.hpp>
#包括LT&;升压/累加器/统计/ median.hpp>
#包括LT&;升压/累加器/统计/ weighted_median.hpp>
使用空间boost ::蓄电池;诠释的main()
{
//定义一个累加器集
accumulator_set<双,统计与LT;标签::位数> > ACC1;
accumulator_set<双,统计与LT;标签::位数>中漂浮> ACC2; //推一些数据...
ACC1(0.1);
ACC1(0.2);
ACC1(0.3);
ACC1(0.4);
ACC1(0.5);
ACC1(0.6); ACC2(0.1,重量= 0);
ACC2(0.2,重量= 0);
ACC2(0.3,重量= 0);
ACC2(0.4,重量= 1);
ACC2(0.5,重量= 1);
ACC2(0.6,重量= 1); //显示结果...
性病::法院LT&;< 中间:<<中位数(ACC1)LT;<的std :: ENDL;
性病::法院LT&;< 加权中值:<<中位数(ACC2)LT;<的std :: ENDL; 返回0;
}
产生下面的输出,这显然是错误的。
中位数:0.3
加权中值:0.3
我是不是做错了什么?
任何帮助将大大AP preciated。
*但是,加权和工作正常*
@焕发codeR:加权和工作完全正常像这样
的#include<&iostream的GT;
#包括LT&;升压/累加器/ accumulators.hpp>
#包括LT&;升压/累加器/统计/ stats.hpp>
#包括LT&;升压/累加器/统计/ sum.hpp>
#包括LT&;升压/累加器/统计/ weighted_sum.hpp>
使用空间boost ::蓄电池;诠释的main()
{
//定义一个累加器集
accumulator_set<双,统计与LT;标签::总和> > ACC1;
accumulator_set<双,统计与LT;标签::总和>中漂浮> ACC2;
// accumulator_set<双,统计与LT;标签::位数>中漂浮> ACC2; //推一些数据...
ACC1(0.1);
ACC1(0.2);
ACC1(0.3);
ACC1(0.4);
ACC1(0.5);
ACC1(0.6); ACC2(0.1,重量= 0);
ACC2(0.2,重量= 0);
ACC2(0.3,重量= 0);
ACC2(0.4,重量= 1);
ACC2(0.5,重量= 1);
ACC2(0.6,重量= 1); //显示结果...
性病::法院LT&;< 中间:<<总和(ACC1)LT;<的std :: ENDL;
性病::法院LT&;< 加权中值:<<总和(ACC2)LT;<的std :: ENDL; 返回0;
}
和结果
总:2.1
加权总和:1.5
升压功能不破。
问题是,你不要对P ^ 2估计工作提供足够的数据。如果你把一个循环在你的数据输入,如
的for(int i = 0; I< 100000;我++){
ACC2(0.1,重量= 0);
ACC2(0.2,重量= 0);
ACC2(0.3,重量= 0);
ACC2(0.4,重量= 1);
ACC2(0.5,重量= 1);
ACC2(0.6,重量= 1);
}
你得到正确的结果。
中位数:0.3
加权中值:0.5
另外,你可以指定
accumulator_set<双,
统计<标签:: weighted_median(with_p_square_cumulative_distribution)>中
双> ACC2(p_square_cumulative_distribution_num_cells = 5);
这给加权中值:0.55
因为即使只有6添加为你的问题点的答案
I confess that I am no expert in C++.
I am looking for a fast way to compute weighted median, which Boost seemed to have. But it seems I am not able to make it work.
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/median.hpp>
#include <boost/accumulators/statistics/weighted_median.hpp>
using namespace boost::accumulators;
int main()
{
// Define an accumulator set
accumulator_set<double, stats<tag::median > > acc1;
accumulator_set<double, stats<tag::median >, float> acc2;
// push in some data ...
acc1(0.1);
acc1(0.2);
acc1(0.3);
acc1(0.4);
acc1(0.5);
acc1(0.6);
acc2(0.1, weight=0.);
acc2(0.2, weight=0.);
acc2(0.3, weight=0.);
acc2(0.4, weight=1.);
acc2(0.5, weight=1.);
acc2(0.6, weight=1.);
// Display the results ...
std::cout << " Median: " << median(acc1) << std::endl;
std::cout << "Weighted Median: " << median(acc2) << std::endl;
return 0;
}
produces the following output, which is clearly wrong.
Median: 0.3
Weighted Median: 0.3
Am I doing something wrong? Any help will be greatly appreciated.
* however, the weighted sum works correctly *
@glowcoder: The weighted sum works perfectly fine like this.
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/sum.hpp>
#include <boost/accumulators/statistics/weighted_sum.hpp>
using namespace boost::accumulators;
int main()
{
// Define an accumulator set
accumulator_set<double, stats<tag::sum > > acc1;
accumulator_set<double, stats<tag::sum >, float> acc2;
// accumulator_set<double, stats<tag::median >, float> acc2;
// push in some data ...
acc1(0.1);
acc1(0.2);
acc1(0.3);
acc1(0.4);
acc1(0.5);
acc1(0.6);
acc2(0.1, weight=0.);
acc2(0.2, weight=0.);
acc2(0.3, weight=0.);
acc2(0.4, weight=1.);
acc2(0.5, weight=1.);
acc2(0.6, weight=1.);
// Display the results ...
std::cout << " Median: " << sum(acc1) << std::endl;
std::cout << "Weighted Median: " << sum(acc2) << std::endl;
return 0;
}
and the result is
Sum: 2.1
Weighted Sum: 1.5
The boost function is not broken.
The problem is that you do not provide enough data for the P^2 estimator to work. If you put a loop around your data input such as
for(int i=0;i<100000;i++){
acc2(0.1, weight=0.);
acc2(0.2, weight=0.);
acc2(0.3, weight=0.);
acc2(0.4, weight=1.);
acc2(0.5, weight=1.);
acc2(0.6, weight=1.);
}
you get the correct result of
Median: 0.3
Weighted Median: 0.5
alternatively, you can specify
accumulator_set<double,
stats<tag::weighted_median(with_p_square_cumulative_distribution) >,
double> acc2 ( p_square_cumulative_distribution_num_cells = 5 );
which gives Weighted Median: 0.55
as an answer even with only 6 points added as in your question.
这篇关于是提升图书馆的加权中值坏了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!