Class Array
In: lib/facets/core-uncommon/facets/array/median.rb
lib/facets/core-uncommon/facets/array/percentile.rb
lib/facets/core-uncommon/facets/array/op_pow.rb
Parent: Object

Methods

median   percentile  

External Aliases

product -> **
  Array#** is an alias for Array#product.

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require ‘facets‘.

Public Instance methods

Returns the median for the array; nil if array is empty.

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require ‘facets‘.

[Source]

# File lib/facets/core-uncommon/facets/array/median.rb, line 12
  def median
    percentile(50)
  end

Returns the percentile value for percentile p; nil if array is empty.

p should be expressed as an integer; percentile(90) returns the 90th percentile of the array.

Algorithm from NIST

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require ‘facets‘.

CREDT: ?

[Source]

# File lib/facets/core-uncommon/facets/array/percentile.rb, line 15
  def percentile(p)
    sorted_array = self.sort
    rank = (p.to_f / 100) * (self.length + 1)

    if self.length == 0
      return nil
    elsif rank.to_i == rank #fractional_part?
      sample_0 = sorted_array[rank.truncate - 1]
      sample_1 = sorted_array[rank.truncate]

      fractional_part = rank.abs.modulo(1)
      return (fractional_part * (sample_1 - sample_0)) + sample_0
    else
      return sorted_array[rank.to_i - 1]
    end    
  end

[Validate]