Created
March 23, 2020 15:17
-
-
Save AndreaPasqualini/a961d9f5752d1bd1edc66e2fd801176e to your computer and use it in GitHub Desktop.
A function to compute the Gini coefficient, from a vector of data.
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 | |
def G(v): | |
""" | |
Found at https://stackoverflow.com/questions/39512260/calculating-gini-coefficient-in-python-numpy | |
""" | |
bins = np.linspace(0., 100., 11) | |
total = float(np.sum(v)) | |
yvals = [] | |
for b in bins: | |
bin_vals = v[v <= np.percentile(v, b)] | |
bin_fraction = (np.sum(bin_vals) / total) * 100.0 | |
yvals.append(bin_fraction) | |
# perfect equality area | |
pe_area = np.trapz(bins, x=bins) | |
# lorenz area | |
lorenz_area = np.trapz(yvals, x=bins) | |
gini_val = (pe_area - lorenz_area) / float(pe_area) | |
return bins, yvals, gini_val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment