Skip to content

Instantly share code, notes, and snippets.

@jemmyw
Created February 8, 2010 20:37
Show Gist options
  • Save jemmyw/298543 to your computer and use it in GitHub Desktop.
Save jemmyw/298543 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
require 'csv'
require 'rubystats'
def variance(population)
mean = 0.0
s = 0.0
population.each_with_index do |x, index|
delta = x - mean
mean = mean + (delta / (index+1))
s += delta * (x - mean)
end
return s / population.size
end
def standard_deviation(population)
Math.sqrt(variance(population))
end
COLORS = %w(red green blue purple burlywood pink)
LEGEND_Y = 0.3
wave_stats = []
waves = ARGV.map{|a| Dir.glob(a) }.flatten
waves.each_with_index do |wave, index|
csvs = Dir.glob(File.join(wave, '**', '*.csv'))
estimates = [[]]
csvs.each do |csv|
clevel = 1
CSV::Reader.parse(File.open(csv, 'rb')) do |row|
level = row[0].to_i
next if level == 0
if level > clevel
estimates[estimates.size-1] = [row[7]]
elsif level == clevel
estimates[estimates.size-1] << row[7]
else
estimates << [row[7]]
end
clevel = level
end
end
estimates = estimates.flatten.compact.map{|e|e.to_f}.sort
mean = estimates.inject(0){|m,n| m + n} / estimates.size
median = estimates[estimates.size/2]
mode = estimates.inject({}){|m,n| m[n] ||= 0; m[n] += 1; m }.inject{|m,n| n[1] > m[1] ? n : m }[0]
standard_deviation = standard_deviation(estimates)
# puts "mean: #{mean}"
# puts "median: #{median}"
# puts "mode: #{mode}"
# puts "stdev: #{standard_deviation}"
wave_stats << {
:estimates => estimates,
:mean => mean,
:median => median,
:mode => mode,
:stdev => standard_deviation,
:wave => wave
}
end
wave_stats.sort{|a,b| a[:stdev] <=> b[:stdev] }.each_with_index do |stat, index|
puts "x#{index} <- c(#{stat[:estimates].join(',')})"
puts "y#{index} <- dnorm(x#{index}, mean=#{stat[:mean]}, sd=#{stat[:stdev]})"
if index == 0
puts "plot(x#{index},y#{index}, xlim=c(0,15), pch=1)"
else
puts "points(x#{index},y#{index}, pch=1)"
end
color = COLORS[index % COLORS.length]
puts "x <- x#{index}"
puts "curve(dnorm(x, mean=#{stat[:mean]}, sd=#{stat[:stdev]}), add=TRUE, col=\"#{color}\")"
puts "legend(10,#{LEGEND_Y-(index.to_f/40)}, \"#{stat[:wave]}\", fill=\"#{color}\")"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment