Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 63 additions & 35 deletions lectures/inequality.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.14.1
jupytext_version: 1.14.5
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand Down Expand Up @@ -145,7 +145,7 @@ households own just over 40\% of total wealth.
---
mystnb:
figure:
caption: Lorenz Curves For Simulated Data
caption: "Lorenz curve of simulated data"
name: lorenz_simulated
---
n = 2000
Expand All @@ -165,8 +165,6 @@ ax.hlines([0.43], [0], [0.8], alpha=0.5, colors='k', ls='--')
ax.set_ylim((0, 1))
ax.set_xlim((0, 1))

plt.title("Lorenz curve of simulated data") # TODO shift to the render

plt.show()
```

Expand Down Expand Up @@ -242,10 +240,10 @@ US in 2016.
---
mystnb:
figure:
caption: "US Lorenz Curves \n"
caption: "2016 US Lorenz curves"
name: lorenz_us
image:
alt: lorenz_real
alt: lorenz_us
classes: shadow bg-primary
width: 75%
---
Expand All @@ -256,8 +254,7 @@ ax.plot(f_vals_ti[-1], l_vals_ti[-1], label=f'total income')
ax.plot(f_vals_li[-1], l_vals_li[-1], label=f'labor income')
ax.plot(f_vals_nw[-1], f_vals_nw[-1], label=f'equality')

ax.legend(fontsize=12)
plt.title("Lorenz curves of US data in 2016")
ax.legend(fontsize=12)
plt.show()
```

Expand Down Expand Up @@ -311,7 +308,7 @@ The idea is that $G=0$ indicates complete equality, while $G=1$ indicates comple
---
mystnb:
figure:
caption: "Shaded Lorenz curves (simulated data) \n"
caption: "Shaded lorenz curve of simulated data"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lorenz -> Lorenz

name: lorenz_gini
image:
alt: lorenz_gini
Expand All @@ -335,8 +332,7 @@ ax.set_ylim((0, 1))
ax.set_xlim((0, 1))

ax.text(0.04, 0.5, r'$G = 2 \times$ shaded area', fontsize=12)

plt.title("Shaded lorenz curve of simulated data")

plt.show()
```

Expand Down Expand Up @@ -374,7 +370,7 @@ for σ in σ_vals:
```

```{code-cell} ipython3
def plot_inequality_measures(x, y, legend, xlabel, ylabel, title):
def plot_inequality_measures(x, y, legend, xlabel, ylabel):

fig, ax = plt.subplots()
ax.plot(x, y, marker='o', label=legend)
Expand All @@ -383,27 +379,25 @@ def plot_inequality_measures(x, y, legend, xlabel, ylabel, title):
ax.set_ylabel(ylabel, fontsize=12)

ax.legend(fontsize=12)
plt.title(title) # TODO shift it to the render
plt.show()
```

```{code-cell} ipython3
---
mystnb:
figure:
caption: "Lorenz curves (simulated data) \n"
name: lorenz_simulated_shaded
caption: "Gini coefficients of simulated data"
name: gini_simulated
image:
alt: gini
alt: gini_simulated
classes: shadow bg-primary
width: 75%
---
plot_inequality_measures(range(k),
ginis,
'simulated',
'$\sigma$',
'gini coefficients',
'Gini coefficients of simulated data')
'gini coefficients')
```

The plots show that inequality rises with $\sigma$, according to the Gini
Expand Down Expand Up @@ -467,10 +461,10 @@ ginis_li_new[5] = (ginis_li[4] + ginis_li[6]) / 2
---
mystnb:
figure:
caption: "US Gini Coefficients \n"
name: gini_us
caption: "Gini coefficients of US net wealth"
name: gini_wealth_us
image:
alt: gini_us
alt: gini_wealth_us
classes: shadow bg-primary
width: 75%
---
Expand All @@ -483,13 +477,21 @@ ax.plot(years, ginis_nw, marker='o')

ax.set_xlabel(xlabel, fontsize=12)
ax.set_ylabel(ylabel, fontsize=12)


plt.title("Gini coefficients of US net wealth data")

plt.show()
```

```{code-cell} ipython3
---
mystnb:
figure:
caption: "Gini coefficients of US income"
name: gini_income_us
image:
alt: gini_income_us
classes: shadow bg-primary
width: 75%
---
xlabel = "year"
ylabel = "gini coefficient"

Expand All @@ -501,12 +503,11 @@ ax.plot(years, ginis_ti, marker='o', label="total income")
ax.set_xlabel(xlabel, fontsize=12)
ax.set_ylabel(ylabel, fontsize=12)

ax.legend(fontsize=12)
plt.title("Gini coefficients of US income data")
ax.legend(fontsize=12)
plt.show()
```

**TEST ===>** Here is a {ref}`gini_us` and a {numref}`gini_us`
**TEST ===>** Here is a {ref}`gini_income_us` and a {numref}`gini_income_us`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be removed now?


We see that, by this measure, inequality in wealth and income has risen
substantially since 1980.
Expand Down Expand Up @@ -550,7 +551,7 @@ The following code uses the data from dataframe ``df_income_wealth`` to generate

# transfer the survey weights from absolute into relative values
df1 = df_income_wealth
df2 = df1.groupby('year').sum().reset_index() # group
df2 = df1.groupby('year').sum(numeric_only=True).reset_index() # group
df3 = df2[['year', 'weights']]
df3.columns = 'year', 'r_weights'
df4 = pd.merge(df3, df1, how="left", on=["year"])
Expand All @@ -569,9 +570,9 @@ df7 = df4[df4['ti_groups'] == 'Top 10%']

# calculate the sum of weighted top 10% by net wealth, total income and labor income.

df5 = df4.groupby('year').sum().reset_index()
df8 = df6.groupby('year').sum().reset_index()
df9 = df7.groupby('year').sum().reset_index()
df5 = df4.groupby('year').sum(numeric_only=True).reset_index()
df8 = df6.groupby('year').sum(numeric_only=True).reset_index()
df9 = df7.groupby('year').sum(numeric_only=True).reset_index()

df5['weighted_n_wealth_top10'] = df8['weighted_n_wealth']
df5['weighted_t_income_top10'] = df9['weighted_t_income']
Expand All @@ -590,6 +591,16 @@ df_topshares = df5[['year', 'topshare_n_wealth', 'topshare_t_income', 'topshare_
Then let's plot the top shares.

```{code-cell} ipython3
---
mystnb:
figure:
caption: "US top shares"
name: top_shares_us
image:
alt: top_shares_us
classes: shadow bg-primary
width: 75%
---
xlabel = "year"
ylabel = "top $10\%$ share"

Expand All @@ -603,7 +614,6 @@ ax.set_xlabel(xlabel, fontsize=12)
ax.set_ylabel(ylabel, fontsize=12)

ax.legend(fontsize=12)
plt.title("Top shares of US data") # TODO shift to the render
plt.show()
```

Expand Down Expand Up @@ -662,12 +672,21 @@ for σ in σ_vals:
```

```{code-cell} ipython3
---
mystnb:
figure:
caption: "Top shares of simulated data"
name: top_shares_simulated
image:
alt: top_shares_simulated
classes: shadow bg-primary
width: 75%
---
plot_inequality_measures(range(len(topshares)),
topshares,
"simulated data",
"year",
"top $10\%$ share",
"Top $10\%$ share of simulated data")
"top $10\%$ share")
```

```{solution-end}
Expand Down Expand Up @@ -708,6 +727,16 @@ for f_val, l_val in zip(f_vals_nw, l_vals_nw):
```

```{code-cell} ipython3
---
mystnb:
figure:
caption: "US top shares: approximation vs Lorenz"
name: top_shares_us_al
image:
alt: top_shares_us_al
classes: shadow bg-primary
width: 75%
---
xlabel = "year"
ylabel = "top $10\%$ share"

Expand All @@ -720,7 +749,6 @@ ax.set_xlabel(xlabel, fontsize=12)
ax.set_ylabel(ylabel, fontsize=12)

ax.legend(fontsize=12)
plt.title("Top $10\%$ share of net wealth: approximation vs lorenz")
plt.show()
```

Expand Down