DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Standard Deviation

02.22.2010
| 2717 views |
  • submit to reddit
        
#!/usr/bin/ruby -w

include Math

# werte, für die die standardabweichung berechnet werden soll
arrValues = [ 4.58, 4.53, 4.1, 4.05 ]

# mittelwert der werte
fMedian = 0
arrValues.each do |fValue|
fMedian += fValue
end
fMedian /= arrValues.size.to_f
puts "Mittelwert = " + fMedian.to_s

# summieren
fStandardDeviation = 0
arrValues.each do |fValue|
fStandardDeviation += (fValue - fMedian)**2
end
puts "Zwischensumme = " + fStandardDeviation.to_s

# durch anzahl teilen
fStandardDeviation /= arrValues.size.to_f
puts fStandardDeviation

# wurzel ziehen
fStandardDeviation = Math.sqrt(fStandardDeviation)

# auf 3 stellen runden
fStandardDeviation = "%.3f" % fStandardDeviation

puts "rating = " + fStandardDeviation.to_s
Resources: <a href="http://blog.cadego.de/">blog.cadego.de</a>