Commit bb3f4462 by Carla Guillen

### Factoring out statistic functions

parent 18e7d3da
 /* * CommonStatistics.h * * Created on: Aug 7, 2019 * Author: carla */ #ifndef ANALYTICS_INCLUDES_COMMONSTATISTICS_H_ #define ANALYTICS_INCLUDES_COMMONSTATISTICS_H_ #include #include int64_t computeObs(std::vector &buffer) { return buffer.size(); } int64_t computeSum(std::vector &buffer) { int64_t acc=0; for(const auto& v : buffer) acc += v.value; return acc; } int64_t computeAvg(std::vector &buffer) { int64_t acc=0, ctr=buffer.size(); for(const auto& v : buffer) acc += v.value; acc = ctr > 0 ? acc/ctr : acc; return acc; } int64_t computeMax(std::vector &buffer) { int64_t acc=0; bool maxInit=false; for(const auto& v : buffer) if(v.value>acc || !maxInit) { acc = v.value; maxInit = true; } return acc; } int64_t computeMin(std::vector &buffer) { int64_t acc=0; bool minInit=false; for(const auto& v : buffer) if(v.value &buffer) { int64_t avg = computeAvg(buffer); int64_t acc=0, val=0, ctr=buffer.size(); for(const auto& v : buffer) { val = v.value - avg; acc += val*val; } acc = ctr > 0 ? sqrt(acc/ctr) : sqrt(acc); return acc; } #endif /* ANALYTICS_INCLUDES_COMMONSTATISTICS_H_ */