(Ruby) Calculate Standard Deviation in Ruby

This below code is for calculate standard deviation in Ruby on Rails. Here the data set is given in a array. To know more about standard deviation calculations @ [Standard Deviation Calculator](http://ncalculators.com/statistics/mean-standard-deviation-calculator.htm “Standard Deviation Calculator”)

  1. #!/usr/bin/ruby -w
  2. include Math
  3. #Data we are going to calculate stdev
  4. arrValues = [ 4.58, 4.53, 4.1, 4.05 ]
  5. fMedian = 0
  6. arrValues.each do |fValue|
  7. fMedian += fValue
  8. end
  9. fMedian /= arrValues.size.to_f
  10. puts "Mittelwert = " + fMedian.to_s
  11. fStandardDeviation = 0
  12. arrValues.each do |fValue|
  13. fStandardDeviation += (fValue - fMedian)**2
  14. end
  15. puts "Zwischensumme = " + fStandardDeviation.to_s
  16. fStandardDeviation /= arrValues.size.to_f
  17. puts fStandardDeviation
  18. fStandardDeviation = Math.sqrt(fStandardDeviation)
  19. fStandardDeviation = "%.3f" % fStandardDeviation
  20. puts "rating = " + fStandardDeviation.to_s
This entry was posted in Ruby and tagged , , , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

One Comment

  1. Posted November 4, 2011 at 10:04 am | Permalink

    Steps for Standard Deviation
    Step 1 : Use formula for standard deviation,
    σ = √[(1/N)Σi(xi-μ)2].

    Step 2 : Where σ – the standard deviation
    N – number of data points ;
    xi – the random variable where i takes values from 1 to N ;

    Step 3 : μ- the mean of the data set;
    Then write the final answer for σ .

    if you any large problem then i will suggest to use Standard Deviation Calculator Online

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?