Skip to content

Fix Math.gini_coefficient(array) #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .index
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ paths:
- lib/standard
name: facets
title: Ruby Facets
version: 3.1.0
version: 3.1.1
summary: The orginal well curated collection of extension methods for Ruby.
slogan: ALL YOUR BASE ARE BELONG TO RUBY!
description: Facets is the premier collection of extension methods for the Ruby programming
Expand Down
9 changes: 5 additions & 4 deletions lib/standard/facets/math/gini_coefficient.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'facets/math/approx_equal'
require 'facets/math/mean'

module Math

Expand All @@ -13,11 +14,11 @@ module Math
# 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) }
return -1 if array.size <= 0 or array.any? { |x| x < 0 }
return 0 if array.size < 2 or array.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
array.sort.each_with_index { |li,i| s += (2*i+1-array.size)*li }
s.to_f/(array.size**2*mean(array)).to_f
end

## OLD WAY
Expand Down