Created
October 22, 2012 19:07
-
-
Save eedrummer/3933433 to your computer and use it in GitHub Desktop.
Initial pass at calculating the median for CV measures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'moped' | |
db = Moped::Session.new(["localhost:27017"]) | |
db.use('foo') | |
wrapped_result = db.command(:aggregate => 'patient_cache', :pipeline => [ | |
{'$unwind' => "$values"}, | |
{'$group' => {'_id' => '$values', 'count' => {"$sum" => 1}}} | |
]) | |
puts wrapped_result | |
result = {} | |
wrapped_result['result'].each do |freq_count_pair| | |
result[freq_count_pair['_id']] = freq_count_pair['count'] | |
end | |
set_size = result.values.reduce(0) {|memo, freq| memo + freq} | |
sorted_values = result.keys.sort | |
median_position = (set_size / 2) + 1 | |
current_position = 0 | |
median = 0 | |
sorted_values.each do |value| | |
current_position += result[value] | |
if current_position >= median_position | |
median = value | |
break | |
end | |
end | |
puts "Median: #{median}" | |
puts "Set size: #{set_size}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment