Calculate sample variance

master
neingeist 11 years ago
parent eeeb8e4f3c
commit 83f056effc

@ -60,3 +60,6 @@ mtrace-test.txt: mtrace-test.trace
uiowa-threads-example: uiowa-threads.o uiowa-threads-example.c
$(CC) $(CFLAGS) -o $@ $^
av-variance: av-variance.c
$(CC) $(CFLAGS) -o $@ $< -lm

@ -17,6 +17,20 @@ bool isalmost(double x, double c, double epsilon) {
return fabs(x-c) < epsilon;
}
double sample_variance(double set[], int set_n) {
double m = mean(set, set_n);
double ss = 0.0;
for (int i=0; i<set_n; i++) {
ss += pow((set[i]-m), 2.0);
}
// Divide by (n-1), as this is *sample* variance (Bessel's correction).
ss /= (set_n-1);
return ss;
}
int main() {
// Some data
double d[] = {7, 8, 5, 1, 6, 2};
@ -31,7 +45,10 @@ int main() {
// XXX assert(isalmost(median(d, d_n), 5.5, 0.001));
// XXX assert(isalmost(median(e, e_n), XXX, 0.001));
// XXX Variance
assert(isalmost(sample_variance(d, d_n), 7.766, 0.001));
assert(isalmost(sample_variance(e, e_n), 41.564, 0.001));
// XXX Stddev
// XXX Range
// XXX Mode

Loading…
Cancel
Save