I am calculating a Gini coefficient (similar to: Calculating a Python-Gini coefficient using Numpy ), but I get an odd result. for a uniform distribution taken from np.random.rand() , the Gini coefficient is 0.3, but I expected it to be close to 0 (perfect equality). what is going wrong here?
def G(v): 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 v = np.random.rand(500) bins, result, gini_val = G(v) plt.figure() plt.subplot(2, 1, 1) plt.plot(bins, result, label="observed") plt.plot(bins, bins, '--', label="perfect eq.") plt.xlabel("fraction of population") plt.ylabel("fraction of wealth") plt.title("GINI: %.4f" %(gini_val)) plt.legend() plt.subplot(2, 1, 2) plt.hist(v, bins=20)
for a given set of numbers, the above code calculates a fraction of the total distribution values ββthat are in each percentile box.
result:

uniform distributions should be close to "perfect equality", so the bending of the lorenz curve is turned off.
mvd source share