是Boost库';加权中值破了吗? 我承认我不是C++专家。

是Boost库';加权中值破了吗? 我承认我不是C++专家。,c++,boost,statistics,C++,Boost,Statistics,我正在寻找一种快速计算加权中值的方法,这一点在Boost看来是有帮助的。 但我似乎无法让它工作 #include <iostream> #include <boost/accumulators/accumulators.hpp> #include <boost/accumulators/statistics/stats.hpp> #include <boost/accumulators/statistics/median.hpp> #include

我正在寻找一种快速计算加权中值的方法,这一点在Boost看来是有帮助的。 但我似乎无法让它工作

#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;
}
我做错什么了吗? 任何帮助都将不胜感激

*但是,加权和工作正常*

@glowcoder:加权和像这样工作非常好

#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;
}

你好像打了两次电话。也许你想第二次打加权中值?

怎么样:

accumulator_set<double, stats<tag::weighted_median(with_weighted_density) >, float> acc2;
累加器设置acc2;

加权中值的含义是什么?中位数只考虑项目的顺序,而不考虑内容。权重不会改变顺序(它可以改变平均值或总和)。如果你使用发生计数(自然整数)而不是浮点数,你可以扩展中值的定义,但我认为这不是你在这里要做的。

根据文档,它说它使用p^2估计器来计算中值。我在谷歌上搜索了一下,发现Jain&Chlamtac是“不存储观测值的分位数和直方图动态计算的P^2算法”。令我惊讶的是,在我看来,它的中值只是一个估计值,而不是确切的值。它应该被称为中位数,而不是中位数


而且它确实看起来好像加权中值被打破了;它不考虑重量。

增压功能没有中断

问题是你没有提供足够的数据让p^2估计器工作。如果在数据输入周围放置一个循环,例如

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.);
}
或者,您可以指定

 accumulator_set<double, 
    stats<tag::weighted_median(with_p_square_cumulative_distribution) >, 
    double> acc2 ( p_square_cumulative_distribution_num_cells = 5 );
累加器集合acc2(p\u平方\累积\分布\单元数=5);

它给出了加权中值:0.55作为答案,即使像你的问题那样只增加了6分。

谢谢,但是医生说如果权重是非无效的,那么计算是用加权的对应项完成的。我认为这个方法需要的参数是概率密度。我想手动指定权重加权中值a^*(样本x_1,x_2,…,x_N给定权重w_1,w_2,…,w_N)定义为a^*=arg_a最小和{u k=1}{N}w_k | x_k-a |您也可以查看
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.);
}
Median: 0.3
Weighted Median: 0.5
 accumulator_set<double, 
    stats<tag::weighted_median(with_p_square_cumulative_distribution) >, 
    double> acc2 ( p_square_cumulative_distribution_num_cells = 5 );