# Lorenz Curve and Gini Coefficient

Last updated: May 15th, 2020
In [7]:
#Press shift+enter to run every block

In [8]:
#Press shift+enter
def trap_area(side1,side2,height):
return (int(side1)+int(side2)/2)*int(height)

In [9]:
#Press shift+enter
def lorenz_gini():
import matplotlib.pyplot as plt
import numpy as np

country=input("What country do you need the Lorenz Curve and Gini Coefficient for? :")
if country=="":
country="Country X"
print("Enter distribution per quintile from poorest to richest one by one. Entering the starting 0 value is not necessary. Make sure they add up to 100%")
T1=input("Distribution for bottom 20%: ")
if T1=="":
T2=input("Distribution for next 20%: ")
if T2=="":
T3=input("Distribution for next 20%: ")
if T3=="":
T4=input("Distribution for next 20%: ")
if T4=="":
T5=input("Distribution for top 20%: ")

Q1=int(T1)
Q2=int(Q1)+int(T2)
Q3=int(Q2)+int(T3)
Q4=int(Q3)+int(T4)
Q5=100

x1=(0,20,40,60,80,100)
y1=(0,Q1,Q2,Q3,Q4,Q5)
x2=np.linspace(0,100, num=6)
y2=np.linspace(0,100, num=6)
x3=np.linspace(0,100, num=6)
y3=np.zeros((6,1))

lorenz_fig=plt.figure
plt.plot(x1, y1,'-o', label=country)
plt.plot(x2, y2, 'g' ,label="Perfect Equality")
plt.plot(x3, y3, 'r',label="Perfect Inequality")
#plt.ylim(0,100)

plt.title('Lorenz Curve of '+country)
plt.xlabel('% of Population')
plt.ylabel('% of Income')
plt.legend()
plt.legend(loc='upper left')

print("\n")
print("The Lorenz Curve for "+country+" looks like this: ")
plt.show()

total=5000
A=5000-((0.5*20*Q1)+trap_area(Q1,Q2,20)+trap_area(Q2,Q3,20)+trap_area(Q3,Q4,20)+trap_area(Q4,Q5,20))
gini=(A)/total

gini=str(gini)
print("The Gini Coefficient for "+country+" is: "+gini)

In [10]:
##############################################
#press shift+enter to run
#a name for the country is not required. press enter to move on.
#you can use the test values(one by one, press enter after each): 1,3,6,16,74
##############################################

lorenz_gini()

Enter distribution per quintile from poorest to richest one by one. Entering the starting 0 value is not necessary. Make sure they add up to 100%


The Lorenz Curve for China looks like this:

The Gini Coefficient for China is: 0.554

In [ ]:
#click on the previous block if moved to this one