【问题标题】:finding kurtosis for a dataset with boost使用 boost 查找数据集的峰度
【发布时间】:2013-03-05 12:11:34
【问题描述】:

我有一个数据向量,我想找到数据集的峰度。我想用 Boost 来做到这一点,这是我目前所拥有的(不可编译):

#include <boost/math/distributions.hpp>
using namespace std;

int main()
{
    vector<double> a;
    a.push_back(-1);
    a.push_back(0);
    a.push_back(1);

    cout << "Kurtosis:"<< kurtosis(a) << endl;
    return 0;
}

为什么这不起作用?我的编译器给了我错误:“[...]\main.cpp|28|error: 'kurtosis' was not declared in this scope|”

【问题讨论】:

  • 如果编译不出来,贴出编译错误
  • 函数在另一个命名空间,需要使用boost::some::namespace::kurtosis(a)。将 some::namespace 替换为实际(我不知道)命名空间。
  • 免责声明:我对问题域的了解不够,无法完全确定我在说什么。我的猜测是 中的算法仅适用于parameterized predefined distributions。使用数据集的替代方法可能是Boost.AccumulatorsHere 就是一个例子。
  • @llonesmitz 我喜欢你的第一个例子,我可以用它。你知道如何在 Boost 中重置累加器吗?因为我需要找到各种数据集的峰度(循环......)
  • @llonesmiz 我发现了这个,描述了如何重置累加器:stackoverflow.com/questions/5195990/…

标签: c++ boost


【解决方案1】:

对于一个你没有包括kurtosis的标题:

#include <boost/accumulators/statistics/kurtosis.hpp>

即使您这样做了,正如您看到的那样,它不适用于直接的 vector,您可能想要做的是使用 accumulator_set 以及更多标题。

这是一个使用accumulator_set 的最小示例,它显示了解决问题的两种方法:

#include <boost/math/distributions.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/variance.hpp>
#include <boost/accumulators/statistics/kurtosis.hpp>
#include <iostream>
#include <vector>

using namespace boost::accumulators;

int main()
{
    accumulator_set<double, stats<tag::mean, tag::kurtosis > > acc;
    accumulator_set<double, stats<tag::mean, tag::kurtosis > > acc2;

    acc(2) ;
    acc(3) ;
    acc(4) ;

    std::cout << mean(acc) << " " << kurtosis(acc) << std::endl ;

    std::vector<double> v1 ;

    v1.push_back(2);
    v1.push_back(3);
    v1.push_back(4);

    acc2 = std::for_each( v1.begin(), v1.end(), acc2 ) ;

    std::cout << mean(acc2) << " " << kurtosis(acc2) << std::endl ;
}

这是Accumulators Framework User's Guide 的链接。本指南有一些很好的例子。

以前的thread 找到了一种使用vector 的方法,尽管它一点也不简单,我无法让它工作。

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多