PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > How to calculate variance
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to calculate variance

Réponse
 
LinkBack Outils de la discussion
Vieux 04/12/2007, 15h56   #1
Surjit Nameirakpam
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to calculate variance

How to calculate variance on the elements of an Array
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 04/12/2007, 16h51   #2
Tim Pease
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to calculate variance

On Dec 4, 2007 8:56 AM, Surjit Nameirakpam <surjit.meitei@gmail.com> wrote:
> 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

  Réponse avec citation
Vieux 05/12/2007, 01h27   #3
Alex Gutteridge
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to calculate variance

On 5 Dec 2007, at 00:56, Surjit Nameirakpam wrote:

> 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



  Réponse avec citation
Vieux 05/12/2007, 12h44   #4
Sergio Gil Pérez de la Manga
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to calculate variance

On Dec 4, 2007 5:51 PM, Tim Pease <tim.pease@gmail.com> wrote:
> 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 > sgilperez@gmail.com
blog > http://www.lacoctelera.com/porras

  Réponse avec citation
Vieux 05/12/2007, 17h21   #5
Rak Rok
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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/Algorit...ating_variance

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h23.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14797 seconds with 13 queries