Some statistics stuff (av-variance.c)
parent
9527b11c57
commit
eeeb8e4f3c
@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
|
||||
double mean(double set[], int set_n) {
|
||||
double sum = 0.0;
|
||||
for(int i=0; i<set_n; i++) {
|
||||
sum += set[i];
|
||||
}
|
||||
|
||||
return sum/set_n;
|
||||
}
|
||||
|
||||
bool isalmost(double x, double c, double epsilon) {
|
||||
assert(epsilon >= 0.0);
|
||||
return fabs(x-c) < epsilon;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Some data
|
||||
double d[] = {7, 8, 5, 1, 6, 2};
|
||||
const int d_n = 6;
|
||||
|
||||
double e[] = {-10, 0, 10, 11, 12, 13, 14, 8, 9, 10, 11, 10, 10};
|
||||
const int e_n = 13;
|
||||
|
||||
// Some tests
|
||||
assert(isalmost(mean(d, d_n), 4.833, 0.001));
|
||||
assert(isalmost(mean(e, e_n), 8.307, 0.001));
|
||||
|
||||
// XXX assert(isalmost(median(d, d_n), 5.5, 0.001));
|
||||
// XXX assert(isalmost(median(e, e_n), XXX, 0.001));
|
||||
// XXX Variance
|
||||
// XXX Stddev
|
||||
// XXX Range
|
||||
// XXX Mode
|
||||
// XXX Percentile
|
||||
}
|
Loading…
Reference in New Issue