Calculating Median in Ruby

Calculating Median in Ruby

Here is a solution that works on both even and odd length array and won't alter the array:

def median(array)
return nil if array.empty?
sorted = array.sort
len = sorted.length
(sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0
end

Find the median of an array

I'm going to just jump in with a solution here...

def median(ary)
mid = ary.length / 2
sorted = ary.sort
ary.length.odd? ? sorted[mid] : 0.5 * (sorted[mid] + sorted[mid - 1])
end

Edit - I've incorporated .odd? as per BroiSatse's suggestion.

How to calculate median in a Rails app that uses SQLite3?

Try this to calculate median

def median(array)
sorted = array.sort
len = sorted.length
return (sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0
end

Finding the highest, lowest, total, average and median from an array in Ruby

lowest = arr.min
highest = arr.max
total = arr.inject(:+)
len = arr.length
average = total.to_f / len # to_f so we don't get an integer result
sorted = arr.sort
median = len % 2 == 1 ? sorted[len/2] : (sorted[len/2 - 1] + sorted[len/2]).to_f / 2

Finding median for even length array in ruby

Arrays are zero-indexed. So if the length was 4, you need to be taking average of indices 1 and 2. Your current attempt would take average of indices 3 and 2 for a length of 4. So you just need to change one small thing (plus into minus):

(array[(array.length/2) - 1] + array[array.length/2]) / 2.to_f

For an even numbered Fixnum n, this is always true: ( n - 1 ) / 2 == ( n / 2 ) - 1, which means you have figured out a similar approach to the one you found. This is not too surprising, there are a limited number of ways to calculate medians efficiently.

How can I calculate Median, deriviation, variance etc using Arel in ROR?

TLDR; No.

Its the opposite - the ORM ActiveRecord is built on top of Arel which is a domain specific language for creating SQL queries. Arel adapts to many RDBMS:es.

Arel does not as far as I know have built in functions for median, deviation, variance.

I'm guessing that the main reason is that the these functions may either be totally absent or vary wildly between database systems and it would be too complicated and require too much maintenance.

You can probably use Arels AVG, SUM, COUNT, MIN, MAX aggregate functions together with a SQL string to achieve what you are looking for though.

Determine median element of a nested array in Ruby?

Since the median needs things sorted, you could just delegate out to sort_by and work on the results of that:

class Array
def median(&block)
block = :itself unless block_given?

sorted = sort_by(&block)
if length.odd?
sorted[sorted.length / 2]
else
sorted[sorted.length / 2 - 1, 2]
end
end
end

Sample runs:

[13, 23, 11, 16, 15, 10, 26].median # => 15
# hyperbole showing the block is used on single elements
count = 0; [13, 23, 11, 16, 15, 10, 26].median { |a| count += 1 } # => 16
# even length data set
# usually you'd average these, but that becomes trickier with nested arrays
[14, 13, 23, 11, 16, 15, 10, 26].median # => [14, 15]

# your examples:
[[1,3], [2,5], [3,-4]].median { |z,_| z} # => [2, 5]
[[1,3], [2,5], [3,-4]].median { |_,w| w } # => [1, 3]

# added [6, -6] to your examples:
[[1,3], [2,5], [3,-4], [6, -6]].median { |z,_| z } # => [[2, 5], [3, -4]]
[[1,3], [2,5], [3,-4], [6, -6]].median { |_,w| w } # => [[3, -4], [1, 3]]

You don't specify what should happen for even-length arrays. For a math median (If I remember my maths correctly) you would average the two center-most elements, but then comes the question of what the average of 2 different arrays looks like. This takes the simple (for us) approach of returning both the center elements and the caller has to decide how to handle them. (What if it's not another array nested inside, what if it's a list of people and you want the median by last name, for instance)



Related Topics



Leave a reply



Submit