https://en.wikipedia.org/wiki/Gini_coefficient the Gini coefficient can theoretically range from 0 (complete equality) to 1 (complete inequality


In [1]:
def get_gini(wealths):
    num = 0.0
    denum = 0.0
    for xi in wealths:
        for xj in wealths:
            num += abs(xi - xj)
            denum += xj
    return num / (2 * denum)

In [2]:
examples = [
    [0.0, 100.0],
    [0.0, 100000.0],
    [20.0, 30.0, 40.0, 50.0, 60.0],
    [50.0, 90.0, 130.0, 170.0, 270.0],
    [20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 120.0, 150.0],
]

In [3]:
for wealth in examples:
    gini = get_gini(wealth)
    print('wealths=%s of %d persons -> gini_coef=%.3f' % (str(wealth), len(wealth), gini))


wealths=[0.0, 100.0] of 2 persons -> gini_coef=0.500
wealths=[0.0, 100000.0] of 2 persons -> gini_coef=0.500
wealths=[20.0, 30.0, 40.0, 50.0, 60.0] of 5 persons -> gini_coef=0.200
wealths=[50.0, 90.0, 130.0, 170.0, 270.0] of 5 persons -> gini_coef=0.293
wealths=[20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 120.0, 150.0] of 10 persons -> gini_coef=0.303