4

Including the mean tag returns an incorrect variance. I have tried this with both weighted and straight variances with similar results. I have included my code below. Am I doing something wrong?

Sample output:

./stats 1 2 3 4 5 6
Variances:
        Var: 2.91667
    VarKurt: 2.91667
    VarMean: 7.46389
VarMeanKurt: 7.46389

Code:

#include <iostream>
#include <stdlib.h>
#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>

using namespace boost::accumulators;

// set up the various accumulators
typedef accumulator_set<int, stats<tag::variance> > Var;
typedef accumulator_set<int, stats<tag::variance, tag::kurtosis> > VarKurt;
typedef accumulator_set<int, stats<tag::variance, tag::mean> > VarMean;
typedef accumulator_set<int, stats<tag::variance, tag::mean, tag::kurtosis> > VarMeanKurt;

Var accumulate_Var(int *v, int n) {
    Var acc;
  for(unsigned int i=0; i<n; i++) {
   acc(v[i]);
  }
  return acc;
}

VarKurt accumulate_VarKurt(int *v, int n) {
  VarKurt acc;
  for(unsigned int i=0; i<n; i++) {
       acc(v[i]);
  }
  return acc;
}

VarMean accumulate_VarMean(int *v, int n) {
  VarMean acc;
  for(unsigned int i=0; i<n; i++) {
       acc(v[i]);
  }
  return acc;
}

VarMeanKurt accumulate_VarMeanKurt(int *v, int n) {
  VarMeanKurt acc;
  for(unsigned int i=0; i<n; i++) {
      acc(v[i]);
  }
  return acc;
}

int main(int argc, char *argv[]) {
   int n=argc-1;
   int x[n];
   for (int i=1; i<=n; i++) {
     x[i-1] = atoi(argv[i]);
   }

   std::cout << std::endl << " Variances: " << std::endl;
   std::cout << "        Var: " << variance(accumulate_Var(x, n)) << std::endl;
   std::cout << "    VarKurt: " << variance(accumulate_VarKurt(x, n)) << std::endl;
   std::cout << "    VarMean: " << variance(accumulate_VarMean(x, n)) << std::endl;
   std::cout << "VarMeanKurt: " << variance(accumulate_VarMeanKurt(x, n)) << std::endl;

   return 0;
}
Colm
  • 41
  • 3
  • 2
    user You Should Check Whether YOU Have the Wrong Before Saying `boost` is borken –  Jul 18 '12 at 20:32
  • 5
    Since when is "Am I doing something wrong?" == ( "`boost` is broken"?) – dwerner Jul 18 '12 at 20:35
  • 1
    @dwerner because it's in the title? – RedX Jul 18 '12 at 20:38
  • @desolator Sorry, I have changed the title to something more suitable. – Colm Jul 18 '12 at 20:48
  • 2
    switching the declaration order of tag::variance and tag::mean, e.g. `typedef accumulator_set< int , stats< tag::mean , tag::variance > > VarMean;` yields the expected results. I'm unaware of where the Boost documentation indicates that the results should depend on this declaration order. – Dave Jul 19 '12 at 12:04
  • 1
    However, I suspect that it has something to do with the fact that `tag::variance` depends on `tag::mean`. – Dave Jul 19 '12 at 12:08
  • 2
    What version of boost are you using? With boost 1.55 it works: http://coliru.stacked-crooked.com/a/50927af3c47348d5 – mr_georg Feb 27 '14 at 08:42

0 Answers0