user393144 user393144 - 4 months ago 20
C++ Question

calculate mean and standard deviation from a vector of samples in C++ using boost

Is there a way to calculate mean and standard deviation for a vector containing samples using boost?
Or do I have to create an accumulator and feed the vector into it?

Answer

Using accumulators is the way to compute means and standard deviations in boost.

  accumulator_set<double, stats<tag::variance> > acc;
  for_each(a_vec.begin(), a_vec.end(), bind<void>(ref(acc), _1));

  cout << mean(acc) << endl;
  cout << sqrt(variance(acc)) << endl;