A simple bit of code in multiple languages that lets you calculate useful statistics without storing every sample.
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
#pragma once
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include "stats.hpp"
|
|
|
|
|
|
|
|
|
|
struct WelfordStats {
|
|
|
|
|
double Mean = 0.0; // LOL
|
|
|
|
|
double n = 0.0;
|
|
|
|
|
double M2 = 0.0;
|
|
|
|
|
|
|
|
|
|
double min = 0.0;
|
|
|
|
|
double max = 0.0;
|
|
|
|
|
|
|
|
|
|
inline void reset() {
|
|
|
|
|
Mean = 0.0;
|
|
|
|
|
n = 0.0;
|
|
|
|
|
M2 = 0.0;
|
|
|
|
|
|
|
|
|
|
min = 0.0;
|
|
|
|
|
max = 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline double mean() {
|
|
|
|
|
return Mean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline double stddev(bool use_sample=true) {
|
|
|
|
|
if(use_sample) {
|
|
|
|
|
return std::sqrt(sample_variance());
|
|
|
|
|
} else {
|
|
|
|
|
return std::sqrt(variance());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline double variance() {
|
|
|
|
|
return M2 / n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline double sample_variance() {
|
|
|
|
|
return M2 / (n - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void sample(double s) {
|
|
|
|
|
n += 1;
|
|
|
|
|
double old_mean = Mean;
|
|
|
|
|
Mean += (s - Mean) / n;
|
|
|
|
|
M2 += (s - old_mean) * (s - Mean);
|
|
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
|
min = s;
|
|
|
|
|
max = s;
|
|
|
|
|
} else {
|
|
|
|
|
if (min > s) min = s;
|
|
|
|
|
if (max < s) max = s;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TTest t_test(WelfordStats& other);
|
|
|
|
|
};
|