Skip to content

Commit

Permalink
update plots
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcky committed Feb 22, 2024
1 parent 8fa5945 commit 1444052
Showing 1 changed file with 35 additions and 54 deletions.
89 changes: 35 additions & 54 deletions lectures/inequality.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,12 @@ for σ in σ_vals:
```{code-cell} ipython3
def plot_inequality_measures(x, y, legend, xlabel, ylabel):
fig, ax = plt.subplots()
ax.plot(x, y, marker='o', label=legend)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.legend()
plt.show()
return fig, ax
```
```{code-cell} ipython3
Expand All @@ -461,11 +458,12 @@ mystnb:
image:
alt: gini_simulated
---
plot_inequality_measures(σ_vals,
ginis,
'simulated',
'$\sigma$',
'gini coefficients')
fix, ax = plot_inequality_measures(σ_vals,
ginis,
'simulated',
'$\sigma$',
'gini coefficients')
plt.show()
```
The plots show that inequality rises with $\sigma$, according to the Gini
Expand All @@ -475,9 +473,9 @@ coefficient.
Now let's look at Gini coefficients for US data derived from the SCF.
The following code creates a list called `Ginis`.
The following code creates a list called `ginis`.
It stores data of Gini coefficients generated from the dataframe ``df_income_wealth`` and method [gini_coefficient](https://quanteconpy.readthedocs.io/en/latest/tools/inequality.html#quantecon.inequality.gini_coefficient), from [QuantEcon](https://quantecon.org/quantecon-py/) library.
It stores data of Gini coefficients generated from the dataframe `df_income_wealth` and method [gini_coefficient](https://quanteconpy.readthedocs.io/en/latest/tools/inequality.html#quantecon.inequality.gini_coefficient), from [QuantEcon](https://quantecon.org/quantecon-py/) library.
```{code-cell} ipython3
:tags: [hide-input]
Expand All @@ -489,30 +487,30 @@ varlist = ['n_wealth', # net wealth
df = df_income_wealth
# create lists to store Gini for each inequality measure
Ginis = []
results = {}
for var in varlist:
# create lists to store Gini
ginis = []
gini_yr = []
for year in years:
# repeat the observations according to their weights
counts = list(round(df[df['year'] == year]['weights'] ))
y = df[df['year'] == year][var].repeat(counts)
y = np.asarray(y)
rd.shuffle(y) # shuffle the sequence
# calculate and store Gini
gini = qe.gini_coefficient(y)
ginis.append(gini)
gini_yr.append(gini)
Ginis.append(ginis)
results[var] = gini_yr
```
```{code-cell} ipython3
ginis_nw, ginis_ti, ginis_li = Ginis
ginis_nw = results['n_wealth'] # net wealth
ginis_ti = results['t_income'] # total income
ginis_li = results['l_income'] # labour income
```
Let's plot the Gini coefficients for net wealth, labor income and total income.
Expand All @@ -532,16 +530,10 @@ mystnb:
image:
alt: gini_wealth_us
---
xlabel = "year"
ylabel = "gini coefficient"
fig, ax = plt.subplots()
ax.plot(years, ginis_nw, marker='o')
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.set_xlabel("year")
ax.set_ylabel("gini coefficient")
plt.show()
```
Expand All @@ -554,17 +546,11 @@ mystnb:
image:
alt: gini_income_us
---
xlabel = "year"
ylabel = "gini coefficient"
fig, ax = plt.subplots()
ax.plot(years, ginis_li_new, marker='o', label="labor income")
ax.plot(years, ginis_ti, marker='o', label="total income")
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.set_xlabel("year")
ax.set_ylabel("gini coefficient")
ax.legend()
plt.show()
```
Expand All @@ -574,7 +560,6 @@ substantially since 1980.
The wealth time series exhibits a strong U-shape.
## Top shares
Another popular measure of inequality is the top shares.
Expand Down Expand Up @@ -658,21 +643,15 @@ mystnb:
image:
alt: top_shares_us
---
xlabel = "year"
ylabel = "top $10\%$ share"
fig, ax = plt.subplots()
ax.plot(years, df_topshares["topshare_l_income"],
marker='o', label="labor income")
ax.plot(years, df_topshares["topshare_n_wealth"],
marker='o', label="net wealth")
ax.plot(years, df_topshares["topshare_t_income"],
marker='o', label="total income")
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.set_xlabel("year")
ax.set_ylabel("top $10\%$ share")
ax.legend()
```
Expand Down Expand Up @@ -742,11 +721,12 @@ mystnb:
image:
alt: top_shares_simulated
---
plot_inequality_measures(σ_vals,
topshares,
"simulated data",
"$\sigma$",
"top $10\%$ share")
fig, ax = plot_inequality_measures(σ_vals,
topshares,
"simulated data",
"$\sigma$",
"top $10\%$ share")
plt.show()
```
```{code-cell} ipython3
Expand All @@ -758,11 +738,12 @@ mystnb:
image:
alt: gini_coef_simulated
---
plot_inequality_measures(σ_vals,
ginis,
"simulated data",
"$\sigma$",
"gini coefficient")
fig, ax = plot_inequality_measures(σ_vals,
ginis,
"simulated data",
"$\sigma$",
"gini coefficient")
plt.show()
```
```{code-cell} ipython3
Expand Down

0 comments on commit 1444052

Please sign in to comment.