diff --git a/quantecon/__init__.py b/quantecon/__init__.py index 0c906744d..090e3b85f 100644 --- a/quantecon/__init__.py +++ b/quantecon/__init__.py @@ -7,7 +7,8 @@ import numba except: raise ImportError( - "Cannot import numba from current anaconda distribution. Please run `conda install numba` to install the latest version.") + "Cannot import numba from current anaconda distribution. \ + Please run `conda install numba` to install the latest version.") #-Modules-# from . import distributions @@ -25,7 +26,8 @@ # 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, shorrocks_index +from .inequality import lorenz_curve, gini_coefficient, shorrocks_index, \ + rank_size_plot from .kalman import Kalman from .lae import LAE from .arma import ARMA @@ -37,7 +39,8 @@ from .quadsums import var_quadratic_sum, m_quadratic_sum #->Propose Delete From Top Level #Promote to keep current examples working -from .markov import MarkovChain, random_markov_chain, random_stochastic_matrix, gth_solve, tauchen, rouwenhorst +from .markov import MarkovChain, random_markov_chain, random_stochastic_matrix, \ + gth_solve, tauchen, rouwenhorst #Imports that Should be Deprecated with markov package from .markov import mc_compute_stationary, mc_sample_path #<- diff --git a/quantecon/inequality.py b/quantecon/inequality.py index 2cac426a4..809b5022b 100644 --- a/quantecon/inequality.py +++ b/quantecon/inequality.py @@ -117,3 +117,40 @@ def shorrocks_index(A): diag_sum = np.diag(A).sum() return (m - diag_sum) / (m - 1) + + +def rank_size_plot(data, ax, label=None, c=1.0): + """ + Generate rank-size data corresponding to distribution data. + + Examples + -------- + + > import numpy as np + > import matplotlib.pyplot as plt + > y = np.exp(np.random.randn(1000)) # simulate data + > fig, ax = plt.subplots() + > rank_size_plot(y, ax) + > plt.show() + + Parameters + ---------- + + data : array_like + the set of observations + c : int or float + restrict plot to top (c x 100)% of the distribution + ax : axis object + for plotting on, has method ax.loglog + """ + w = - np.sort(- data) # Reverse sort + w = w[:int(len(w) * c)] # extract top c% + rank_data = np.arange(len(w)) + 1 + size_data = w + ax.loglog(rank_data, size_data, 'o', markersize=3.0, alpha=0.5, label=label) + if label: + ax.legend() + ax.set_xlabel("log rank") + ax.set_ylabel("log size") + +