Forked from CMCDragonkai/lorenz_curve_gini_coefficient.py
Created
September 20, 2023 02:31
-
-
Save luckmoon/c1851f05ef5c36022717b3666d12097d to your computer and use it in GitHub Desktop.
Lorenz Curve and Gini Coefficient #python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
# ensure your arr is sorted from lowest to highest values first! | |
arr = np.array([1,4,6,9,100]) | |
def gini(arr): | |
count = arr.size | |
coefficient = 2 / count | |
indexes = np.arange(1, count + 1) | |
weighted_sum = (indexes * arr).sum() | |
total = arr.sum() | |
constant = (count + 1) / count | |
return coefficient * weighted_sum / total - constant | |
def lorenz(arr): | |
# this divides the prefix sum by the total sum | |
# this ensures all the values are between 0 and 1.0 | |
scaled_prefix_sum = arr.cumsum() / arr.sum() | |
# this prepends the 0 value (because 0% of all people have 0% of all wealth) | |
return np.insert(scaled_prefix_sum, 0, 0) | |
# show the gini index! | |
print(gini(arr)) | |
lorenz_curve = lorenz(arr) | |
# we need the X values to be between 0.0 to 1.0 | |
plt.plot(np.linspace(0.0, 1.0, lorenz_curve.size), lorenz_curve) | |
# plot the straight line perfect equality curve | |
plt.plot([0,1], [0,1]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment