-
Notifications
You must be signed in to change notification settings - Fork 0
/
GiniCoef.py
36 lines (27 loc) · 959 Bytes
/
GiniCoef.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import numba as nb
from numpy import cumsum, insert
# # nb.jit(nopython=True) is a shortcut for @nb.njit()
# @nb.njit()
def Gini(values):
n = len(values)
assert(n > 0), 'Empty list of values'
sorted_Values = sorted(values) # Sort smallest to largest
# Alternative 0 [SLOW]
# Find cumulative totals
# cumm = [0]
# for i in range(n):
# cumm.append(sum(sorted_Values[0: (i + 1)]))
# Alternative 1 [FASTER]
cumm = cumsum(sorted_Values)
cumm = insert(cumm, 0, 0)
# Calculate Lorenz points
LorenzPoints = [[], []]
sumYs = 0 # Some of all y values
for i in range(1, n + 2):
x = 100.0 * (i - 1) / n
y = 100.0 * (cumm[i - 1] / float(cumm[n]))
LorenzPoints[0].append(x)
LorenzPoints[1].append(y)
sumYs += y
gini_Coef = 100 + (100 - 2 * sumYs) / n # Gini index
return [gini_Coef/100, LorenzPoints]