Wednesday, October 7, 2009

calculating the variance - groovy style

just had to calculate some variances in a flexible way and like always groovy comes to the rescue.


static double variance(def population) {
long n = 0
double mean = 0
double s = 0.0

population.each {double x ->
n++;
double delta = x - mean
mean += delta / n
s += delta * (x - mean)
}

(s / (n - 1))
}

No comments:

Post a Comment