I wanted to use boost accumulators to calculate statistics of a variable that is a vector. Is there a simple way to do this. I think it’s not possible to use the dumbest thing:
using namespace boost::accumulators;
//stuff...
accumulator_set<vector<double>, stats<tag::mean> > acc;
vector<double> some_vetor;
//stuff
some_vector = doStuff();
acc(some_vector);
maybe this is obvious, but I tried anyway. 😛
What I wanted was to have an accumulator that would calculate a vector which is the mean of the components of many vectors. Is there an easy way out?
EDIT:
I don’t know if I was thoroughly clear. I don’t want this:
for_each(vec.begin(), vec.end(),acc);
This would calculate the mean of the entries of a given vector. What I need is different. I have a function that will spit vectors:
vector<double> doSomething();
// this is a monte carlo simulation;
And I need to run this many times and calculate the vectorial mean of those vectors:
for(int i = 0; i < numberOfMCSteps; i++){
vec = doSomething();
acc(vec);
}
cout << mean(acc);
And I want mean(acc) to be a vector itself, whose entry [i] would be the means of the entries [i] of the accumulated vectors.
Theres a hint about this in the docs of Boost, but nothing explicit. And I’m a bit dumb. 😛
I’ve looked into your question a bit, and it seems to me that Boost.Accumulators already provides support for
std::vector. Here is what I could find in a section of the user’s guide :Indeed, after verification, the file
boost/accumulators/numeric/functional/vector.hppdoes contain the necessary operators for the ‘naive’ solution to work.I believe you should try :
boost/accumulators/numeric/functional/vector.hppbefore any other accumulators headerboost/accumulators/numeric/functional.hppwhile definingBOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORTusing namespace boost::numeric::operators;.There’s only one last detail left : execution will break at runtime because the initial accumulated value is default-constructed, and an assertion will occur when trying to add a vector of size n to an empty vector. For this, it seems you should initialize the accumulator with (where n is the number of elements in your vector) :
I tried the following code,
meangives me astd::vectorof size 2 :I believe this is what you wanted ?