Sha256: 354bdc0b6105d5931d22690deeff2e0494b5c66f861718ddc78661e22a17625e

Contents?: true

Size: 1.06 KB

Versions: 9

Compression:

Stored size: 1.06 KB

Contents

require 'facets/math/approx_equal'

module Math

  # Calculates the Gini Coefficient (a measure of inequality of a distribution
  # based on the area between the Lorenz curve and the uniform curve).
  #
  # http://en.wikipedia.org/wiki/Gini_coefficient
  #
  # This is a slightly cleaner way of calculating the Gini Coefficient then
  # the previous implementationj.
  #
  #   GC = \frac{\sum_{i=1}^N (2i-N-1)x_i}{N^2-\bar{x}}
  #
  def self.gini_coefficient(array)
    return -1 if size <= 0 or any? { |x| x < 0 }
    return 0 if size < 2 or all? { |x| approx_equal(x,0) }
    s = 0
    sort.each_with_index { |li,i| s += (2*i+1-size)*li }
    s.to_f/(size**2*mean).to_f
  end

  ## OLD WAY
  ##   GC = \frac{1}{N} \left ( N+1-2\frac{\sum_{i=1}^N (N+1-i)y_i}{\sum_{i=1}^N y_i} \right )
  ## def self.gini_coefficient2(array)
  ##   return -1 if size <= 0 or any? { |x| x < 0 }
  ##   return 0 if size < 2 or all? { |x| Math::float_equal(x,0) }
  ##   s = 0
  ##   sort.each_with_index { |yi,i| s += (size - i)*yi }
  ##   (size+1-2*(s.to_f/sum.to_f)).to_f/size.to_f
  ## end

end

Version data entries

9 entries across 8 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/standard/facets/math/gini_coefficient.rb
facets-3.1.0 lib/standard/facets/math/gini_coefficient.rb
facets-3.0.0 lib/standard/facets/math/gini_coefficient.rb
facets-2.9.3 lib/standard/facets/math/gini_coefficient.rb
facets-2.9.2 src/standard/facets/math/gini_coefficient.rb
facets-2.9.2 lib/standard/facets/math/gini_coefficient.rb
facets-2.9.1 lib/standard/facets/math/gini_coefficient.rb
facets-2.9.0 lib/more/facets/math/gini_coefficient.rb
facets-2.9.0.pre.2 lib/more/facets/math/gini_coefficient.rb