user393144 user393144 - 1 year ago 172
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 Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download