Sha256: 887230f5e0ce53dfc55f25316b92fa42be550f09a3e577a6266668671653b428

Contents?: true

Size: 1.34 KB

Versions: 11

Compression:

Stored size: 1.34 KB

Contents

module Enumerable
  unless method_defined?(:median)
    def median(*args, &block)
      if !block && respond_to?(:scoping)
        scoping { @klass.median(*args) }
      elsif !block && respond_to?(:with_scope)
        with_scope(self) { klass.median(*args) }
      else
        raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0)" if args.any?
        percentile(0.5, &block)
      end
    end
  end

  unless method_defined?(:percentile)
    def percentile(*args, &block)
      if !block && respond_to?(:scoping)
        scoping { @klass.percentile(*args) }
      elsif !block && respond_to?(:with_scope)
        with_scope(self) { klass.percentile(*args) }
      else
        raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1)" if args.size != 1

        percentile = args[0].to_f
        raise ArgumentError, "percentile is not between 0 and 1" if percentile < 0 || percentile > 1

        # uses C=1 variant, like percentile_cont
        # https://en.wikipedia.org/wiki/Percentile#The_linear_interpolation_between_closest_ranks_method
        sorted = map(&block).sort
        x = percentile * (sorted.size - 1)
        r = x % 1
        i = x.floor
        if i == sorted.size - 1
          sorted[-1]
        else
          sorted[i] + r * (sorted[i + 1] - sorted[i])
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
active_median-0.4.1 lib/active_median/enumerable.rb
active_median-0.4.0 lib/active_median/enumerable.rb
active_median-0.3.3 lib/active_median/enumerable.rb
active_median-0.3.2 lib/active_median/enumerable.rb
active_median-0.3.1 lib/active_median/enumerable.rb
active_median-0.3.0 lib/active_median/enumerable.rb
active_median-0.2.8 lib/active_median/enumerable.rb
active_median-0.2.7 lib/active_median/enumerable.rb
active_median-0.2.6 lib/active_median/enumerable.rb
active_median-0.2.5 lib/active_median/enumerable.rb
active_median-0.2.4 lib/active_median/enumerable.rb