Skip to content

Instantly share code, notes, and snippets.

@ACEfanatic02
Last active Jun 12, 2016
Embed
What would you like to do?
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