Skip to content

modifying rank size plots #139

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

Merged
merged 1 commit into from
Apr 27, 2020
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
41 changes: 14 additions & 27 deletions source/rst/heavy_tails.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Let's start with some imports:
.. code-block:: ipython

import numpy as np
import quantecon as qe
import matplotlib.pyplot as plt
%matplotlib inline

Expand Down Expand Up @@ -416,7 +417,9 @@ Exercise 4

Replicate the rank-size plot figure :ref:`presented above <rank_size_fig1>`.

For each sample that you plot, you will need to reverse sort it, from largest to smallest, in order to generate the plot.
You can use the function ``qe.rank_size_plot`` from the ``quantecon`` library to generate the rank size plots.

You can find the documentation for this function `here <https://quanteconpy.readthedocs.io/en/latest/tools/inequality.html#quantecon.inequality.rank_size_plot>`__.

Use ``np.random.seed(13)`` to set the seed.

Expand Down Expand Up @@ -572,27 +575,10 @@ Exercise 3
Exercise 4
----------

First we will create a function and then generate the plot
First generate the data for the plots.

.. code:: ipython3

def rank_size_data(data, c=1.0):
"""
Generate rank-size data corresponding to distribution data.

* data is array like
* c is a float indicating the top (c x 100)% of the
distribution
"""
w = - np.sort(- data) # Reverse sort
w = w[:int(len(w) * c)] # extract top c%
rank_data = np.log(np.arange(len(w)) + 1)
size_data = np.log(w)
return rank_data, size_data

fig, axes = plt.subplots(3, 1, figsize=(6, 8))
axes = axes.flatten()

sample_size = 1000
np.random.seed(13)
z = np.random.randn(sample_size)
Expand All @@ -602,19 +588,20 @@ First we will create a function and then generate the plot
data_3 = np.exp(np.random.exponential(scale=1.0, size=sample_size))

data_list = [data_1, data_2, data_3]

Now we will plot the data using ``qe.rank_size_plot``.

.. code:: ipython3

fig, axes = plt.subplots(3, 1, figsize=(6, 8))
axes = axes.flatten()
labels = ['$|z|$', '$\exp(z)$', 'Pareto with tail index $1.0$']


for data, label, ax in zip(data_list, labels, axes):
rank_data, size_data = rank_size_data(data)
ax.plot(rank_data, size_data, 'o', markersize=3.0, alpha=0.5, label=label)

ax.set_xlabel("log rank")
ax.set_ylabel("log size")

qe.rank_size_plot(data, ax, label=label)
ax.legend()



fig.subplots_adjust(hspace=0.4)

plt.show()
Expand Down
36 changes: 7 additions & 29 deletions source/rst/wealth_dynamics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ At the same time, given the similarities, perhaps Pareto tails will arise.
To test this, run a simulation that generates a cross-section of wealth and
generate a rank-size plot.

In the ``quantecon`` library there is a function called ``rank_size_plot`` which will generate a rank size plot from inputted data.
You can find the documentation for this function `here <https://quanteconpy.readthedocs.io/en/latest/tools/inequality.html#quantecon.inequality.rank_size_plot>`__.

In viewing the plot, remember that Pareto tails generate a straight line. Is
this what you see?

Expand All @@ -587,7 +590,7 @@ For sample size and initial conditions, use
T = 500 # shift forward T periods
ψ_0 = np.ones(num_households) * wdy.y_mean # initial distribution
z_0 = wdy.z_mean


Solutions
=========
Expand Down Expand Up @@ -637,37 +640,12 @@ First let's generate the distribution:

ψ_star = update_cross_section(wdy, ψ_0, shift_length=T)

Here's a function to produce rank-size data for the plot.

.. code:: ipython3

def rank_size_data(data, c=0.001):
"""
Generate rank-size data corresponding to distribution data.

* data is array like
* c is a float indicating the top (c x 100)% of the
distribution
"""
w = - np.sort(- data) # Reverse sort
w = w[:int(len(w) * c)] # extract top c%
rank_data = np.log(np.arange(len(w)) + 1)
size_data = np.log(w)
return rank_data, size_data


Now let's see the rank-size plot.

Now let's see the rank-size plot by using the ``rank_size_plot`` function in the ``quantecon`` library.

.. code:: ipython3


rank_data, size_data = rank_size_data(ψ_star)

fig, ax = plt.subplots()
ax.plot(rank_data, size_data, 'o', markersize=3.0, alpha=0.5)

ax.set_xlabel("log rank")
ax.set_ylabel("log size")

qe.rank_size_plot(ψ_star, ax, c=0.001)

plt.show()