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

Adds a rank size plot to inequality #518

Merged
merged 2 commits into from
Dec 8, 2019
Merged
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
9 changes: 6 additions & 3 deletions quantecon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +26,8 @@
# from .game_theory import <objects-here> #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
Expand All @@ -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
#<-
Expand Down
37 changes: 37 additions & 0 deletions quantecon/inequality.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")