Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def round_to(x)
- (self * 10**x).round.to_f / 10**x
- end
- def std(bia, decs, nums)
- sum = 0.0
- count = 0.0
- nums.each { |plus| sum += plus
- count += 1 }
- puts "The sum of numbers in the array is: #{sum}"
- puts "The total numbers in your array is: #{count}"
- mean = sum / count
- dec_round = "#{decs}"
- rounded_mean = mean.round_to(dec_round)
- puts rounded_mean
- puts "The mean of all the numbers in your array is: #{mean}"
- if bia == "biased"
- bia_nums = 0.0
- nums.each { |a| bia_nums += ((mean - a)**2) }
- stddev_biased = Math.sqrt(bia_nums / (count - 1))
- puts "The biased standard deviation is: #{stddev_biased}"
- elsif bia == "unbiased"
- unbia_nums = 0.0
- nums.each { |a| unbia_nums += ((mean - a)**2) }
- stddev_unbiased = Math.sqrt(unbia_nums / (count))
- puts "The unbiased standard deviation is: #{stddev_unbiased}"
- else
- print 'That is not the right syntax, use std("biased", [1,2,3])'
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement