From: Rak Rok Date: 2007-12-06T02:21:40+09:00 Subject: Re: How to calculate variance > def variance > m = mean > sum { |i| ( i - m )**2 } / size > end If you wanted it to be a little more efficient you can do it like so: def variance( arr ) n, mean, s = [0, 0, 0] arr.each_with_index do |x, n| delta = (x - mean).to_f mean += delta/(n+1) s += delta*(x - mean) end s/n end It calculates the variance in one pass by calculating the mean and variance iteratively [1]. -rr- [1] http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance