Calculate the Gini coefficient of a numpy array.
Switch branches/tags
Nothing to show
Clone or download
Latest commit 184347f Mar 23, 2017
Failed to load latest commit information.
docs Update Feb 2, 2017
.gitignore Initial commit Jul 29, 2016
LICENSE Update LICENSE Mar 23, 2017 Update Feb 2, 2017
gini.png Add files via upload Jul 29, 2016 just tidying, no code change Feb 24, 2017


A Gini coefficient calculator in Python.


This is a function that calculates the Gini coefficient of a numpy array. Gini coefficients are often used to quantify income inequality, read more here.

The function in is based on the third equation from here, which defines the Gini coefficient as:

G = \dfrac{ \sum_{i=1}^{n} (2i - n - 1) x_i}{n  \sum_{i=1}^{n} x_i}


For a very unequal sample, 999 zeros and a single one,

>>> from gini import *
>>> a = np.zeros((1000))
>>> a[0] = 1.0

the Gini coefficient is very close to 1.0:

>>> gini(a)

For uniformly distributed random numbers, it will be low, around 0.33:

>>> s = np.random.uniform(-1,0,1000)
>>> gini(s)

For a homogeneous sample, the Gini coefficient is 0.0:

>>> b = np.ones((1000))
>>> gini(b)

Input Assumptions

The Gini calculation by definition requires non-zero positive (ascending-order) sorted values within a 1d vector. This is dealt with within gini(). So these four assumptions can be violated, as they are controlled for:

def gini(array):
    """Calculate the Gini coefficient of a numpy array."""
    # based on bottom eq:
    # from:
    array = array.flatten() #all values are treated equally, arrays must be 1d
    if np.amin(array) < 0:
        array -= np.amin(array) #values cannot be negative
    array += 0.0000001 #values cannot be 0
    array = np.sort(array) #values must be sorted
    index = np.arange(1,array.shape[0]+1) #index per array element
    n = array.shape[0]#number of array elements
    return ((np.sum((2 * index - n  - 1) * array)) / (n * np.sum(array))) #Gini coefficient


Many other Gini coefficient functions found online do not produce equivalent results, hence why I wrote this.