Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcky committed Mar 7, 2024
1 parent edb76c6 commit 1c587e3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
year,n_wealth,t_income,l_income
1950,0.8257332034366347,0.44248654139458754,0.5342948198773423
1953,0.8059487586599338,0.42645440609359486,0.5158978980963697
1956,0.8121790488050631,0.4442694287339931,0.5349293526208138
1959,0.7952068741637922,0.43749348077061523,0.5213985948309406
1962,0.808694507657936,0.4435843103853638,0.5345127915054347
1965,0.7904149225687935,0.43763715466663455,0.7487860020887751
1968,0.7982885066993515,0.4208620794438898,0.5242396427381535
1971,0.7911574835420261,0.42333442460902587,0.5576454812313479
1977,0.7571418922185222,0.46187678800902643,0.5704448110072055
1983,0.7494335400643035,0.43934561846447007,0.5662220844385907
1989,0.7715705301674298,0.5115249581654171,0.6013995687471435
1992,0.75081266140553,0.47406506720767927,0.5983592657979551
1995,0.756949238811024,0.4896552355840093,0.5969779516716919
1998,0.7603291991801191,0.49117441585168625,0.5774462841723321
2001,0.7816118750507045,0.5239092994681127,0.6042739644967284
2004,0.7700355469522353,0.48843503839032515,0.598143220179272
2007,0.782141377648697,0.5197156312086194,0.6263452195753192
2010,0.825082529519343,0.5195972120145608,0.6453653328291911
2013,0.8227698931835298,0.5314001749843348,0.649868291777264
2016,0.8342975903562216,0.5541400068900835,0.6706846793375284
1950,0.8257332034366358,0.4424865413945875,0.5342948198773428
1953,0.8059487586599331,0.42645440609359453,0.5158978980963707
1956,0.8121790488050629,0.4442694287339922,0.5349293526208139
1959,0.7952068741637917,0.43749348077061556,0.5213985948309421
1962,0.8086945076579354,0.4435843103853641,0.5345127915054346
1965,0.790414922568793,0.4376371546666345,0.7487860020887755
1968,0.7982885066993517,0.42086207944388987,0.524239642738153
1971,0.7911574835420266,0.4233344246090252,0.5576454812313467
1977,0.7571418922185217,0.46187678800902604,0.5704448110072053
1983,0.7494335400643017,0.4393456184644693,0.5662220844385908
1989,0.7715705301674308,0.5115249581654219,0.6013995687471431
1992,0.7508126614055308,0.47406506720767694,0.5983592657979548
1995,0.756949238811026,0.48965523558400526,0.5969779516716914
1998,0.7603291991801175,0.4911744158516885,0.5774462841723366
2001,0.7816118750507022,0.523909299468113,0.6042739644967348
2004,0.770035546952236,0.48843503839032615,0.5981432201792747
2007,0.7821413776486992,0.5197156312086196,0.6263452195753294
2010,0.8250825295193438,0.51959721201456,0.6453653328291932
2013,0.8227698931835281,0.5314001749843356,0.6498682917772638
2016,0.8342975903562243,0.5541400068900844,0.6706846793375284
48 changes: 27 additions & 21 deletions lectures/inequality.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ We will need to install the following packages

```{code-cell} ipython3
:tags: [hide-output]
!pip install wbgapi
```

Expand Down Expand Up @@ -298,12 +299,10 @@ mystnb:
alt: lorenz_us
---
fig, ax = plt.subplots()
ax.plot(f_vals_nw[-1], l_vals_nw[-1], label=f'net wealth')
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()
plt.show()
```
Expand Down Expand Up @@ -346,6 +345,14 @@ The Gini coefficient is closely related to the Lorenz curve.
In fact, it can be shown that its value is twice the area between the line of
equality and the Lorenz curve (e.g., the shaded area in the following Figure below).
```{note}
Another way to think of the gini coefficient is the area between the 45-degree line of
perfect equality and the Lorenz curve minus the area below the Lorenz curve devided by
the total area below the 45-degree line.
In other words, it is a measure of average deviation from the line of equality.
```
The idea is that $G=0$ indicates complete equality, while $G=1$ indicates complete inequality.
```{code-cell} ipython3
Expand Down Expand Up @@ -478,7 +485,7 @@ coefficient.
Now let's look at Gini coefficients for US data.
In this section we will get Gini coefficients from the World Bank using the [wbgapi](https://blogs.worldbank.org/opendata/introducing-wbgapi-new-python-package-accessing-world-bank-data).
In this section we will get pre-computed Gini coefficients from the World Bank using the [wbgapi](https://blogs.worldbank.org/opendata/introducing-wbgapi-new-python-package-accessing-world-bank-data).
Let's search the world bank data for gini to find the Series ID.
Expand All @@ -502,7 +509,7 @@ data = wb.data.DataFrame("SI.POV.GINI", "USA")
data
```
```{note}
```{tip}
This package often returns data with year information contained in the columns. This is not always convenient for simple plotting with pandas so it can be useful to transpose the results before plotting
```
Expand All @@ -518,9 +525,9 @@ ax.set_ylim(0,data_usa.max()+5)
plt.show()
```
The gini coefficient does not have significant variation in the full range from 0 to 100.
The gini coefficient is relatively slow moving and does not have significant variation in the full range from 0 to 100.
In fact we can take a quick look across all countries and all years in the world bank dataset to observe this.
Using `pandas` we can take a quick look across all countries and all years in the world bank dataset to understand how the Gini coefficient varies across countries and time.
```{code-cell} ipython3
gini_all = wb.data.DataFrame("SI.POV.GINI")
Expand All @@ -535,7 +542,7 @@ gini_all = gini_all.unstack(level='economy').dropna()
gini_all.plot(kind="hist", title="Gini coefficient");
```
Therefore we can see that across 50 years of data and all countries the measure only varies between 20 and 65.
Therefore we can see that across 50 years of data and all countries (including low and high income countries) the measure only varies between 20 and 65.
This variation would be even smaller for the subset of wealthy countries, so let us zoom in a little on the US data and add some trendlines.
Expand Down Expand Up @@ -582,18 +589,20 @@ plt.show()
Looking at this graph you can see that inequality was falling in the USA until 1981 when it appears to have started to change course and steadily rise over time (growing inequality).
```{admonition} TODO
Why did GINI fall in 2020? I would have thought it accelerate in the other direction or was there a lag in investment returns around COVID
```
## Comparing income and wealth inequality (the US case)
The Gini coefficient can also be computed over different distributions such as *income* and *wealth*.
We can use the data collected above {ref}`survey of consumer finances <data:survey-consumer-finance>` to look at the gini coefficient when using income when compared to wealth data.
Let's compute the gin coefficient for net wealth, total income, and labour income.
Let's compute the gin coefficient for net wealth, total income, and labour income for the most recent year in our sample.
This section makes use of the following code to compute the data, however to speed up execution we have pre-compiled the results and will use that in the subsequent analysis.
```{code-cell} ipython3
df_income_wealth.year.describe()
```
```{code-cell} ipython3
:tags: [skip-execution, hide-input, hide-output]
Expand Down Expand Up @@ -631,6 +640,10 @@ results = pd.DataFrame(results, index=years)
results.to_csv("_static/lecture_specific/inequality/usa-gini-nwealth-tincome-lincome.csv", index_label='year')
```
While the data can be computed using the code above, we will import a pre-computed dataset from the lecture repository.
<!-- TODO: update from csv to github location -->
```{code-cell} ipython3
ginis = pd.read_csv("_static/lecture_specific/inequality/usa-gini-nwealth-tincome-lincome.csv", index_col='year')
ginis.head(n=5)
Expand Down Expand Up @@ -926,20 +939,13 @@ Plot the top shares generated from Lorenz curve and the top shares approximated
:class: dropdown
```
We will use the `interpolation` package in this solution.
```{code-cell} ipython3
:tags: [hide-output]
!pip install --upgrade interpolation
from interpolation import interp
```
+++
Here is one solution:
```{code-cell} ipython3
def lorenz2top(f_val, l_val, p=0.1):
t = lambda x: interp(f_val, l_val, x)
t = lambda x: np.interp(x, f_val, l_val)
return 1- t(1 - p)
```
Expand Down

0 comments on commit 1c587e3

Please sign in to comment.