6

I have a vector of data and I want to find the kurtosis of the data set. I wanted to do so with Boost and here is what I have so far (not compilable):

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

Why doesn't this work? My compiler gives me the error: "[...]\main.cpp|28|error: 'kurtosis' was not declared in this scope|"

BillyJean
  • 1,537
  • 1
  • 22
  • 39
  • 3
    If it wont compile, post the compiler error – mathematician1975 Mar 05 '13 at 12:13
  • 1
    The function is in another namespace, you need to use `boost::some::namespace::kurtosis(a)`. Replace `some::namespace` with the actual (which I don't know) namespace. – Some programmer dude Mar 05 '13 at 12:17
  • 1
    Disclaimer:I don't know enough about the problem domain to be absolutely sure of what I'm saying. My guess is that the algorithms in only work with [parameterized predefined distributions](http://www.boost.org/libs/math/doc/sf_and_dist/html/math_toolkit/dist/dist_ref/dists.html). An alternative using data sets could be [Boost.Accumulators](http://www.boost.org/libs/accumulators). [Here](http://liveworkspace.org/code/2cDjQ9$0) is an example. –  Mar 05 '13 at 13:46
  • @llonesmitz I like your first example, I can work with that. Do you know how to reset an accumulator in Boost? Because I need to find the kurtosis for various datasets (in a loop...) – BillyJean Mar 05 '13 at 14:14
  • 1
    @llonesmiz I found this, describing how to reset the accumulator: http://stackoverflow.com/questions/5195990/using-boostaccumulators-how-can-i-reset-a-rolling-window-size-does-it-keep-e – BillyJean Mar 05 '13 at 14:22

1 Answers1

1

For one you were not including the header for kurtosis:

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

Even if you did, as you see it does not work with a straight vector, what you probably want to do is use an accumulator_set and more headers as well.

Here is a minimal example using accumulator_set, this shows a two methods to solve the problem:

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

Here is a link to the Accumulators Framework User's Guide. This guide has some nice examples.

This previous thread found a way to use vector, although it is not straight forward at all and I could not get it work.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740