From 8175a6b1459e030901a21c83fd0e87587a3b52b2 Mon Sep 17 00:00:00 2001 From: HarveyT47 Date: Mon, 27 Apr 2020 17:18:03 +1000 Subject: [PATCH] modifying rank size plots --- source/rst/heavy_tails.rst | 41 ++++++++++++---------------------- source/rst/wealth_dynamics.rst | 36 ++++++----------------------- 2 files changed, 21 insertions(+), 56 deletions(-) diff --git a/source/rst/heavy_tails.rst b/source/rst/heavy_tails.rst index deb3639..7c0dfb3 100644 --- a/source/rst/heavy_tails.rst +++ b/source/rst/heavy_tails.rst @@ -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 @@ -416,7 +417,9 @@ Exercise 4 Replicate the rank-size plot figure :ref:`presented above `. -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 `__. Use ``np.random.seed(13)`` to set the seed. @@ -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) @@ -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() diff --git a/source/rst/wealth_dynamics.rst b/source/rst/wealth_dynamics.rst index 090529d..8d818f0 100644 --- a/source/rst/wealth_dynamics.rst +++ b/source/rst/wealth_dynamics.rst @@ -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 `__. + In viewing the plot, remember that Pareto tails generate a straight line. Is this what you see? @@ -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 ========= @@ -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()