Skip to content
Open
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions quantecon/_inequality.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

import numpy as np
from numba import njit, prange
from numba import njit


@njit
Expand Down Expand Up @@ -54,7 +54,7 @@ def lorenz_curve(y):
return cum_people, cum_income


@njit(parallel=True)
@njit
def gini_coefficient(y):
r"""
Implements the Gini inequality index
Expand All @@ -76,11 +76,13 @@ def gini_coefficient(y):
https://en.wikipedia.org/wiki/Gini_coefficient
"""
n = len(y)
i_sum = np.zeros(n)
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))
y_s = np.sort(y)
i_weighted = 0.0
total = 0.0
for i in range(n):
i_weighted += (i + 1) * y_s[i]
total += y_s[i]
return (2 * i_weighted) / (n * total) - (n + 1) / n


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

6 changes: 5 additions & 1 deletion quantecon/tests/test_inequality.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def test_gini_coeff():
coeff = gini_coefficient(y)
assert_allclose(expected, coeff, rtol=1e-01)

# Regression against the pairwise definition
y = np.array([1.0, 2.0, 4.0, 8.0])
expected = np.abs(y[:, None] - y).sum() / (2 * len(y) * y.sum())
assert_allclose(expected, gini_coefficient(y))

# Tests Weibull: G = 1 - 2**(-1/a)
a = np.random.randint(2, 15)
expected = 1 - 2 ** (-1 / a)
Expand Down Expand Up @@ -130,4 +135,3 @@ def test_rank_size():
r_sqval_exp = r_value_exp**2

assert_raises(AssertionError, assert_allclose, r_sqval_exp, 1, rtol=1e-3)