How to Calculate Gini Coefficient in Python?

By Shivang Yadav Last updated : December 7, 2023

Prerequisite

To understand this solution, you should have the basic knowledge of the following Python topics:

Gini's Coefficient

Gini's Coefficient, also known as Gini's index or Gini's ratio is a statistical method to measure the income distribution of a population.

The values of Gini's Coefficient range from 0 to 1.

  • 0 denotes perfect income equality (each individual of the population has the same income).
  • 1 denotes perfect income inequality (one individual of the population has all income).

Calculating Gini's Coefficient

To calculate Gini's Coefficient in Python there is no direct method present. Here, we will define a function to calculate Gini's Coefficient using its formula.

Python program to calculate Gini Coefficient

# Program to calculate Gini's coefficient in python
import numpy as np

def giniCoeff(x):
    total = 0
    for i, xi in enumerate(x[:-1], 1):
        total += np.sum(np.abs(xi - x[i:]))
    return total / (len(x) ** 2 * np.mean(x))

populationIncome = np.array([50, 20, 100, 5, 10, 75, 150])
print("The incomes of all individuals of population(in 1000's) : \n", populationIncome)

ginis = giniCoeff(populationIncome)
print("Gini's Coefficient for the income population ", ginis)

Output

The output of the above program is:

The incomes of all individuals of population(in 1000's) : 
 [ 50  20 100   5  10  75 150]
Gini's Coefficient for the income population  0.46689895470383275

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.