From 56685e343de0a25425f75698d254fb0dd9f4805c Mon Sep 17 00:00:00 2001 From: Natasha Date: Fri, 7 Dec 2018 16:28:41 +1100 Subject: [PATCH 1/3] Add Shorrock inequality index --- quantecon/__init__.py | 2 +- quantecon/inequality.py | 37 ++++++++++++++++++++++++++++++ quantecon/tests/test_inequality.py | 28 ++++++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/quantecon/__init__.py b/quantecon/__init__.py index 854859958..29b6adb01 100644 --- a/quantecon/__init__.py +++ b/quantecon/__init__.py @@ -23,7 +23,7 @@ # from .game_theory import #Place Holder if we wish to promote any general objects to the qe namespace. from .graph_tools import DiGraph, random_tournament_graph from .gridtools import cartesian, mlinspace, simplex_grid, simplex_index -from .inequality import lorenz_curve, gini_coefficient +from .inequality import lorenz_curve, gini_coefficient, shorrocks_index from .kalman import Kalman from .lae import LAE from .arma import ARMA diff --git a/quantecon/inequality.py b/quantecon/inequality.py index e317cf01b..79d7a74b6 100644 --- a/quantecon/inequality.py +++ b/quantecon/inequality.py @@ -84,3 +84,40 @@ def gini_coefficient(y): return np.sum(i_sum) / (2 * n * np.sum(y)) +def shorrocks_index(A): + """ + Implements Shorrocks index of inequality + + Parameters + ----------- + A : array_like(float) + Square matrix with transition probabilities (mobility matrix) of + dimension m + + Returns + -------- + Shorrocks index: float + The Shorrocks index calculated as + + .. math:: + + s(A) = \frac{m - \sum_j a_{jj}}{m - 1} \in (0, 1) + + An index equal to 0 indicates complete immobility. + + References + ----------- + Wealth distribution and social mobility in the US: A quantitative approach + (Benhabib, Bisin, Luo, 2017). + https://www.econ.nyu.edu/user/bisina/RevisionAugust.pdf + """ + + A = np.asarray(A) # Convert to array if not already + m, n = A.shape + + if m != n: + raise ValueError('A must be a square matrix') + + diag_sum = np.diag(A).sum() + + return (m - diag_sum) / (m - 1) diff --git a/quantecon/tests/test_inequality.py b/quantecon/tests/test_inequality.py index 8f969ef92..e0d7ffd88 100644 --- a/quantecon/tests/test_inequality.py +++ b/quantecon/tests/test_inequality.py @@ -6,7 +6,7 @@ import numpy as np from numpy.testing import assert_allclose -from quantecon import lorenz_curve, gini_coefficient +from quantecon import lorenz_curve, gini_coefficient, shorrocks_index def test_lorenz_curve(): @@ -63,4 +63,28 @@ def test_gini_coeff(): y = np.random.weibull(a, size=n) coeff = gini_coefficient(y) - assert_allclose(expected, coeff, rtol=1e-01) \ No newline at end of file + assert_allclose(expected, coeff, rtol=1e-01) + + +def test_shorrocks_index(): + """ + Test Shorrocks index function against the example used in 'Wealth + distribution and social mobility in the US: A quantitative approach' + (Benhabib, Bisin, Luo, 2017).'' + https://www.econ.nyu.edu/user/bisina/RevisionAugust.pdf + """ + + # Construct the mobility matrix from Benhabib et al. + P = [[0.222, 0.222, 0.215, 0.187, 0.081, 0.038, 0.029, 0.006], + [0.221, 0.220, 0.215, 0.188, 0.082, 0.039, 0.029, 0.006], + [0.207, 0.209, 0.210, 0.194, 0.090, 0.046, 0.036, 0.008], + [0.198, 0.201, 0.207, 0.198, 0.095, 0.052, 0.040, 0.009], + [0.175, 0.178, 0.197, 0.207, 0.110, 0.067, 0.054, 0.012], + [0.182, 0.184, 0.200, 0.205, 0.106, 0.062, 0.050, 0.011], + [0.123, 0.125, 0.166, 0.216, 0.141, 0.114, 0.094, 0.021], + [0.084, 0.084, 0.142, 0.228, 0.170, 0.143, 0.121, 0.028]] + + expected = 0.98 # result from paper + index = shorrocks_index(P) + assert_allclose(expected, index, rtol=1e-2) + From 0a9c1f9ce73266fbf90ab4a433f4f711a956c550 Mon Sep 17 00:00:00 2001 From: Natasha Date: Fri, 7 Dec 2018 16:32:32 +1100 Subject: [PATCH 2/3] add mobility to description --- quantecon/inequality.py | 4 ++-- quantecon/tests/test_inequality.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/quantecon/inequality.py b/quantecon/inequality.py index 79d7a74b6..6c0abc89b 100644 --- a/quantecon/inequality.py +++ b/quantecon/inequality.py @@ -86,7 +86,7 @@ def gini_coefficient(y): def shorrocks_index(A): """ - Implements Shorrocks index of inequality + Implements Shorrocks mobility index Parameters ----------- @@ -97,7 +97,7 @@ def shorrocks_index(A): Returns -------- Shorrocks index: float - The Shorrocks index calculated as + The Shorrocks mobility index calculated as .. math:: diff --git a/quantecon/tests/test_inequality.py b/quantecon/tests/test_inequality.py index e0d7ffd88..9a6c109d3 100644 --- a/quantecon/tests/test_inequality.py +++ b/quantecon/tests/test_inequality.py @@ -68,7 +68,7 @@ def test_gini_coeff(): def test_shorrocks_index(): """ - Test Shorrocks index function against the example used in 'Wealth + Test Shorrocks mobility index function against the example used in 'Wealth distribution and social mobility in the US: A quantitative approach' (Benhabib, Bisin, Luo, 2017).'' https://www.econ.nyu.edu/user/bisina/RevisionAugust.pdf From 3fc5b4b9a3edb0d2db065c1a6434f86d3374125f Mon Sep 17 00:00:00 2001 From: Natasha Date: Fri, 7 Dec 2018 17:02:24 +1100 Subject: [PATCH 3/3] add docs --- docs/source/inequality.rst | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 docs/source/inequality.rst diff --git a/docs/source/inequality.rst b/docs/source/inequality.rst new file mode 100644 index 000000000..2cf290756 --- /dev/null +++ b/docs/source/inequality.rst @@ -0,0 +1,7 @@ +Inquality +========== + +.. automodule:: quantecon.inquality + :members: + :undoc-members: + :show-inheritance: