S
Surjit Nameirakpam
How to calculate variance on the elements of an Array
How to calculate variance on the elements of an Array
How to calculate variance on the elements of an Array
Implement this algorithm <http://mathworld.wolfram.com/Variance.html>.
1) iterate over each element and calculate the mean of the elements of
your Array
2) iterate again over each element and take the difference of each
element with the mean, square that value, and then add it to a running
total
3) that running total is your variance
ary =3D (1..100).to_a
mean =3D (ary.inject(0.0) {|s,x| s + x}) / Float(ary.length)
variance =3D ary.inject(0.0) {|s,x| s + (x - mean)**2}
m = mean
sum { |i| ( i - m )**2 } / size
end
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.