Advertisement
Guest User

Untitled

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