Skip to content

Instantly share code, notes, and snippets.

@mrkn
Last active August 20, 2016 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkn/ad12cba5a8cb95fc712d49d6e533bd57 to your computer and use it in GitHub Desktop.
Save mrkn/ad12cba5a8cb95fc712d49d6e533bd57 to your computer and use it in GitHub Desktop.
m = 0
n = 5
10_000_000.times do
xs = Array.new(n) { 1.0 + 1e-6 * (0.5 - rand) }
sq_mean = xs.map {|x| x**2 }.sum / n
mean_sq = (xs.sum / n)**2
var = sq_mean - mean_sq
p [m += 1, var] if var.negative?
end
require 'enumerable/statistics'
require 'csv'
m, v = CSV.foreach(ARGV[0]).mean_variance do |row|
row[1].to_f * row[2].to_f
end
puts "mean: #{m}"
puts "variance: #{v}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment