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.
magic_stats/cpp/src/stats.cpp

42 lines
1.2 KiB

#include "stats.hpp"
#include <fmt/core.h>
#include <numeric>
#include <numbers>
void Stats::dump(std::string msg)
{
fmt::println("{}: sum: {}, sumsq: {}, n: {}, "
"min: {}, max: {}, mean: {}, stddev: {}",
msg, sum, sumsq, n, min, max, mean(),
stddev());
}
// WARNING: got this code from google AI. PROBABLY BULLSHIT
void Stats::t_test(Stats& other) {
double n1 = this->n;
double n2 = other.n;
double mean1 = this->mean();
double mean2 = other.mean();
double var1 = this->variance();
double var2 = other.variance();
// calculate t-stat
double delta_mean = mean1 - mean2;
double pooled_se = std::sqrt((var1 / n1) + (var2 / n2));
double t_stat = delta_mean / pooled_se;
// calculate degrees of freedom
double num = std::pow((var1 / n1) + (var2 / n2), 2);
double den = (std::pow(var1 / n1, 2) / (n1 - 1)) + (std::pow(var2 / n2, 2) / (n2 - 1));
double df = num / den;
double x = df / (df + t_stat * t_stat);
double p_val = std::tgamma((df + 1.0) / 2.0) / (std::sqrt(df * std::numbers::pi) * std::tgamma(df / 2.0)) * std::pow(1.0 + (t_stat * t_stat) / df, -(df + 1.0) / 2.0);
fmt::println("t_stat={}, df={}, p_val={}",
t_stat, df, p_val);
}