Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vectorize lorenz_curve and gini #702

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions quantecon/_inequality.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,13 @@ def lorenz_curve(y):

"""

n = len(y)
n = y.shape[0]
y = np.sort(y)
s = np.zeros(n + 1)
s[1:] = np.cumsum(y)
cum_people = np.zeros(n + 1)
cum_income = np.zeros(n + 1)
for i in range(1, n + 1):
cum_people[i] = i / n
cum_income[i] = s[i] / s[n]
_zero = np.zeros(1)
s = np.concatenate((_zero, np.cumsum(y)))
_cum_p = np.arange(1, n + 1) / n
cum_income = s / s[n]
cum_people = np.concatenate((_zero, _cum_p))
return cum_people, cum_income


Expand All @@ -76,11 +74,12 @@ def gini_coefficient(y):
https://en.wikipedia.org/wiki/Gini_coefficient
"""
n = len(y)
i_sum = np.zeros(n)
i_sum = 0
t_sum = 0
for i in prange(n):
for j in range(n):
i_sum[i] += abs(y[i] - y[j])
return np.sum(i_sum) / (2 * n * np.sum(y))
i_sum += np.sum(np.abs(y[i] - y))
t_sum += y[i]
return i_sum / (2 * n * t_sum)


def shorrocks_index(A):
Expand Down Expand Up @@ -150,4 +149,3 @@ def rank_size(data, c=1.0):
rank_data = np.arange(len(w)) + 1
size_data = w
return rank_data, size_data