Skip to content

Commit 7db60ff

Browse files
authored
Merge pull request #518 from QuantEcon/add_rank_size
Adds a rank size plot to inequality
2 parents ad70e0e + 64a16f1 commit 7db60ff

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

quantecon/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import numba
88
except:
99
raise ImportError(
10-
"Cannot import numba from current anaconda distribution. Please run `conda install numba` to install the latest version.")
10+
"Cannot import numba from current anaconda distribution. \
11+
Please run `conda install numba` to install the latest version.")
1112

1213
#-Modules-#
1314
from . import distributions
@@ -25,7 +26,8 @@
2526
# from .game_theory import <objects-here> #Place Holder if we wish to promote any general objects to the qe namespace.
2627
from .graph_tools import DiGraph, random_tournament_graph
2728
from .gridtools import cartesian, mlinspace, simplex_grid, simplex_index
28-
from .inequality import lorenz_curve, gini_coefficient, shorrocks_index
29+
from .inequality import lorenz_curve, gini_coefficient, shorrocks_index, \
30+
rank_size_plot
2931
from .kalman import Kalman
3032
from .lae import LAE
3133
from .arma import ARMA
@@ -37,7 +39,8 @@
3739
from .quadsums import var_quadratic_sum, m_quadratic_sum
3840
#->Propose Delete From Top Level
3941
#Promote to keep current examples working
40-
from .markov import MarkovChain, random_markov_chain, random_stochastic_matrix, gth_solve, tauchen, rouwenhorst
42+
from .markov import MarkovChain, random_markov_chain, random_stochastic_matrix, \
43+
gth_solve, tauchen, rouwenhorst
4144
#Imports that Should be Deprecated with markov package
4245
from .markov import mc_compute_stationary, mc_sample_path
4346
#<-

quantecon/inequality.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,40 @@ def shorrocks_index(A):
117117
diag_sum = np.diag(A).sum()
118118

119119
return (m - diag_sum) / (m - 1)
120+
121+
122+
def rank_size_plot(data, ax, label=None, c=1.0):
123+
"""
124+
Generate rank-size data corresponding to distribution data.
125+
126+
Examples
127+
--------
128+
129+
> import numpy as np
130+
> import matplotlib.pyplot as plt
131+
> y = np.exp(np.random.randn(1000)) # simulate data
132+
> fig, ax = plt.subplots()
133+
> rank_size_plot(y, ax)
134+
> plt.show()
135+
136+
Parameters
137+
----------
138+
139+
data : array_like
140+
the set of observations
141+
c : int or float
142+
restrict plot to top (c x 100)% of the distribution
143+
ax : axis object
144+
for plotting on, has method ax.loglog
145+
"""
146+
w = - np.sort(- data) # Reverse sort
147+
w = w[:int(len(w) * c)] # extract top c%
148+
rank_data = np.arange(len(w)) + 1
149+
size_data = w
150+
ax.loglog(rank_data, size_data, 'o', markersize=3.0, alpha=0.5, label=label)
151+
if label:
152+
ax.legend()
153+
ax.set_xlabel("log rank")
154+
ax.set_ylabel("log size")
155+
156+

0 commit comments

Comments
 (0)