-
Notifications
You must be signed in to change notification settings - Fork 10
/
inequality.py
159 lines (123 loc) · 4.38 KB
/
inequality.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import numpy as np
import pandas as pd
import microdf as mdf
def gini(x, w=None, negatives=None):
"""Calculates Gini index.
Args:
x: A float numpy array of data to calculate Gini index on.
w: An optional float numpy array of weights. Should be the same length
as x.
negatives: An optional string indicating how to treat negative values
of x:
'zero' replaces negative values with zeroes.
'shift' subtracts the minimum value from all values of x,
when this minimum is negative. That is, it adds the absolute
minimum value.
Defaults to None, which leaves negative values as they are.
Returns:
A float, the Gini index.
"""
# Requires float numpy arrays (not pandas Series or lists) to work.
x = np.array(x).astype('float')
if negatives == 'zero':
x[x < 0] = 0
if negatives == 'shift' and np.amin(x) < 0:
x -= np.amin(x)
if w is not None:
w = np.array(w).astype('float')
sorted_indices = np.argsort(x)
sorted_x = x[sorted_indices]
sorted_w = w[sorted_indices]
cumw = np.cumsum(sorted_w)
cumxw = np.cumsum(sorted_x * sorted_w)
return (np.sum(cumxw[1:] * cumw[:-1] - cumxw[:-1] * cumw[1:]) /
(cumxw[-1] * cumw[-1]))
else:
sorted_x = np.sort(x)
n = len(x)
cumxw = np.cumsum(sorted_x)
# The above formula, with all weights equal to 1 simplifies to:
return (n + 1 - 2 * np.sum(cumxw) / cumxw[-1]) / n
def top_x_pct_share(val, top_x_pct, w=None):
"""Calculates top x% share.
Args:
val: Value (list-like).
top_x_pct: Decimal between 0 and 1 of the top %, e.g. 0.1, 0.001.
w: Weight (list-like, same length as val).
Returns:
The share of w-weighted val held by the top x%.
"""
val = pd.Series(val)
if w is None:
w = np.ones(val.size)
w = pd.Series(w)
threshold = mdf.weighted_quantile(val, 1 - top_x_pct, w)
filt = val >= threshold
top_x_pct_sum = (val[filt] * w[filt]).sum()
total_sum = (val * w).sum()
return top_x_pct_sum / total_sum
def bottom_x_pct_share(val, bottom_x_pct, w=None):
"""Calculates bottom x% share.
Args:
val: Value (list-like).
bottom_x_pct: Decimal between 0 and 1 of the bottom %, e.g. 0.1, 0.001.
w: Weight (list-like, same length as val).
Returns:
The share of w-weighted val held by the bottom x%.
"""
return 1 - top_x_pct_share(val, 1 - bottom_x_pct, w, top=False)
def bottom_50_pct_share(val, w=None):
"""Calculates bottom 50% share.
Args:
val: Value (list-like).
w: Weight (list-like, same length as val).
Returns:
The share of w-weighted val held by the bottom 50%.
"""
return bottom_x_pct_share(val, 0.5, w)
def top_50_pct_share(val, w=None):
"""Calculates top 50% share.
Args:
val: Value (list-like).
w: Weight (list-like, same length as val).
Returns:
The share of w-weighted val held by the top 50%.
"""
return top_x_pct_share(val, 0.5, w)
def top_10_pct_share(val, w=None):
"""Calculates top 10% share.
Args:
val: Value (list-like).
w: Weight (list-like, same length as val).
Returns:
The share of w-weighted val held by the top 10%.
"""
return top_x_pct_share(val, 0.1, w)
def top_1_pct_share(val, w=None):
"""Calculates top 1% share.
Args:
val: Value (list-like).
w: Weight (list-like, same length as val).
Returns:
The share of w-weighted val held by the top 1%.
"""
return top_x_pct_share(val, 0.01, w)
def top_0_1_pct_share(val, w=None):
"""Calculates top 0.1% share.
Args:
val: Value (list-like).
w: Weight (list-like, same length as val).
Returns:
The share of w-weighted val held by the top 0.1%.
"""
return top_x_pct_share(val, 0.001, w)
def t10_b50(val, w=None):
"""Calculates ratio between the top 10% and bottom 50% shares.
Args:
val: Value (list-like).
w: Weight (list-like, same length as val).
Returns:
The share of w-weighted val held by the top 10% divided by
the share of w-weighted val held by the bottom 50%.
"""
return top_10_pct_share(val, w) / bottom_50_pct_share(val, w)