Skip to content

Instantly share code, notes, and snippets.

@ACEfanatic02
Last active June 12, 2016 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ACEfanatic02/74c1e28d4d9777f815705842b97ea11e to your computer and use it in GitHub Desktop.
Save ACEfanatic02/74c1e28d4d9777f815705842b97ea11e to your computer and use it in GitHub Desktop.
struct SampleStatistics {
u64 count;
double sum;
double sum_of_squares;
double mean;
double standard_deviation;
double min;
double p25;
double median;
double p75;
double max;
};
static void
CalculateSampleStatistics(double * values, u64 count, SampleStatistics * result) {
std::sort(values, values + count);
result->count = count;
result->min = values[0];
result->max = values[count - 1];
result->median = values[count / 2]
+ values[count / 2 + (~count & 1)]
/ 2.0;
u64 half_count = count / 2;
result->p25 = values[half_count / 2]
+ values[half_count + (~half_count & 1)]
/ 2.0;
result->p75 = values[half_count + (count & 1) + half_count / 2]
+ values[half_count + (count & 1) + half_count / 2 + (~half_count & 1)]
/ 2.0;
for (u64 i = 0; i < count; ++i) {
double x = values[i];
result->sum += x;
result->sum_of_squares += x * x;
}
result->mean = result->sum / count;
result->standard_deviation = sqrt((result->sum_of_squares - (result->sum*result->sum)/result->count) / (result->count - 1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment