Skip to content

Instantly share code, notes, and snippets.

@eedrummer
Created October 22, 2012 19:07
Show Gist options
  • Save eedrummer/3933433 to your computer and use it in GitHub Desktop.
Save eedrummer/3933433 to your computer and use it in GitHub Desktop.
Initial pass at calculating the median for CV measures
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