How to calculate variance

  • Thread starter Surjit Nameirakpam
  • Start date
T

Tim Pease

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 = (1..100).to_a
mean = (ary.inject(0.0) {|s,x| s + x}) / Float(ary.length)
variance = ary.inject(0.0) {|s,x| s + (x - mean)**2}

Another option is to install the NArray library and use that ...

nary = NArray.new(NArray::FLOAT, 100)
nary[0...100] = (1..100).to_a
nary.variance

Blessings,
TwP
 
A

Alex Gutteridge

How to calculate variance on the elements of an Array

*Shameless plug and probably overkill if you just want the variance*

RSRuby allows you to use any R function, including the built-in
variance function ('var'). It's available as a gem.

irb(main):002:0> require 'rsruby'
=> true
irb(main):004:0> RSRuby.instance.var([1,2,3])
=> 1.0
irb(main):006:0> RSRuby.instance.var(RSRuby.instance.rnorm(10))
=> 0.812117410016217
irb(main):008:0> RSRuby.instance.var(RSRuby.instance.rnorm(100))
=> 0.960224242747171

Alex Gutteridge

Bioinformatics Center
Kyoto University
 
S

Sergio Gil Pérez de la Manga

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}

You can also write this, if you are going to need it in more places,
in a more generic way:


module Variance

def sum(&blk)
map(&blk).inject { |sum, element| sum + element }
end

def mean
(sum.to_f / size.to_f)
end

def variance
m =3D mean
sum { |i| ( i - m )**2 } / size
end

def std_dev
Math.sqrt(variance)
end
end

Array.send :include, Variance

puts [1, 2].sum # 3
puts [1, 2].mean # 1.5
puts [1, 2].variance # 0.25
puts [1, 2].std_dev # 0.5

Longer but nicer (maybe ;) )

--=20
Sergio Gil P=E9rez de la Manga
e-mail > (e-mail address removed)
blog > http://www.lacoctelera.com/porras
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,273
Messages
2,571,364
Members
48,050
Latest member
Carson62C3

Latest Threads

Top