Skip to content

Commit ac97342

Browse files
committed
added rank size plot to inequality
1 parent ad70e0e commit ac97342

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

quantecon/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# from .game_theory import <objects-here> #Place Holder if we wish to promote any general objects to the qe namespace.
2626
from .graph_tools import DiGraph, random_tournament_graph
2727
from .gridtools import cartesian, mlinspace, simplex_grid, simplex_index
28-
from .inequality import lorenz_curve, gini_coefficient, shorrocks_index
28+
from .inequality import lorenz_curve, gini_coefficient, shorrocks_index, rank_size_plot
2929
from .kalman import Kalman
3030
from .lae import LAE
3131
from .arma import ARMA

quantecon/inequality.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,42 @@ 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+
142+
c : int or float
143+
restrict plot to top (c x 100)% of the distribution
144+
145+
ax : axis object
146+
for plotting on, has method ax.loglog
147+
"""
148+
w = - np.sort(- data) # Reverse sort
149+
w = w[:int(len(w) * c)] # extract top c%
150+
rank_data = np.arange(len(w)) + 1
151+
size_data = w
152+
ax.loglog(rank_data, size_data, 'o', markersize=3.0, alpha=0.5, label=label)
153+
if label:
154+
ax.legend()
155+
ax.set_xlabel("log rank")
156+
ax.set_ylabel("log size")
157+
158+

0 commit comments

Comments
 (0)