Advertisement
Guest User

Untitled

a guest
Nov 5th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.98 KB | None | 0 0
  1. def round_to(x)
  2.   (self * 10**x).round.to_f / 10**x
  3. end
  4.  
  5. def std(bia, decs, nums)
  6.   sum = 0.0
  7.   count = 0.0
  8.   nums.each { |plus| sum += plus
  9.     count += 1 }
  10.    
  11.   puts "The sum of numbers in the array is: #{sum}"
  12.   puts "The total numbers in your array is: #{count}"
  13.   mean = sum / count
  14.   dec_round = "#{decs}"
  15.   rounded_mean = mean.round_to(dec_round)
  16.   puts rounded_mean
  17. puts "The mean of all the numbers in your array is: #{mean}"
  18.  
  19.     if bia == "biased"  
  20.     bia_nums = 0.0
  21.     nums.each { |a| bia_nums += ((mean - a)**2) }
  22.     stddev_biased = Math.sqrt(bia_nums / (count - 1))
  23.   puts "The biased standard deviation is: #{stddev_biased}"
  24.    
  25.     elsif bia == "unbiased"
  26.     unbia_nums = 0.0
  27.     nums.each { |a| unbia_nums += ((mean - a)**2) }
  28.     stddev_unbiased = Math.sqrt(unbia_nums / (count))
  29.   puts "The unbiased standard deviation is: #{stddev_unbiased}"
  30.        
  31.     else
  32.   print 'That is not the right syntax, use std("biased", [1,2,3])'
  33.     end
  34. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement