@@ -354,7 +354,7 @@ distribution.)
354
354
355
355
``` {code-cell} ipython3
356
356
k = 5
357
- σ_vals = np.linspace(0.2, 2.5 , k)
357
+ σ_vals = np.linspace(0.2, 4 , k)
358
358
n = 2_000
359
359
360
360
ginis = []
@@ -387,7 +387,7 @@ mystnb:
387
387
image:
388
388
alt: gini_simulated
389
389
---
390
- plot_inequality_measures(range(k) ,
390
+ plot_inequality_measures(σ_vals ,
391
391
ginis,
392
392
'simulated',
393
393
'$\sigma$',
@@ -411,7 +411,7 @@ The following code creates a list called ``Ginis``.
411
411
:tags: [hide_input]
412
412
413
413
varlist = ['n_wealth', # net wealth
414
- 't_income', # total income
414
+ 't_income', # total income
415
415
'l_income'] # labor income
416
416
417
417
df = df_income_wealth
@@ -646,14 +646,21 @@ def calculate_top_share(s, p=0.1):
646
646
647
647
```{code-cell} ipython3
648
648
k = 5
649
- σ_vals = np.linspace(0.2, 2.5 , k)
649
+ σ_vals = np.linspace(0.2, 4 , k)
650
650
n = 2_000
651
651
652
652
topshares = []
653
+ ginis = []
654
+ f_vals = []
655
+ l_vals = []
653
656
654
657
for σ in σ_vals:
655
658
μ = -σ ** 2 / 2
656
659
y = np.exp(μ + σ * np.random.randn(n))
660
+ f_val, l_val = qe._inequality.lorenz_curve(y)
661
+ f_vals.append(f_val)
662
+ l_vals.append(l_val)
663
+ ginis.append(qe._inequality.gini_coefficient(y))
657
664
topshares.append(calculate_top_share(y))
658
665
```
659
666
@@ -666,24 +673,55 @@ mystnb:
666
673
image:
667
674
alt: top_shares_simulated
668
675
---
669
- plot_inequality_measures(range(len(topshares)) ,
676
+ plot_inequality_measures(σ_vals ,
670
677
topshares,
671
678
"simulated data",
672
- "year ",
679
+ "$\sigma$ ",
673
680
"top $10\%$ share")
674
681
```
675
682
676
- ```{solution-end}
683
+ ```{code-cell} ipython3
684
+ ---
685
+ mystnb:
686
+ figure:
687
+ caption: "Gini coefficients of simulated data"
688
+ name: gini_coef_simulated
689
+ image:
690
+ alt: gini_coef_simulated
691
+ ---
692
+ plot_inequality_measures(σ_vals,
693
+ ginis,
694
+ "simulated data",
695
+ "$\sigma$",
696
+ "gini coefficient")
677
697
```
678
698
699
+ ```{code-cell} ipython3
700
+ ---
701
+ mystnb:
702
+ figure:
703
+ caption: "Lorenz curves for simulated data"
704
+ name: lorenz_curve_simulated
705
+ image:
706
+ alt: lorenz_curve_simulated
707
+ ---
708
+ fig, ax = plt.subplots()
709
+ ax.plot([0,1],[0,1], label=f"equality")
710
+ for i in range(len(f_vals)):
711
+ ax.plot(f_vals[i], l_vals[i], label=f"$\sigma$ = {σ_vals[i]}")
712
+ plt.legend()
713
+ plt.show()
714
+ ```
715
+
716
+ ```{solution-end}
717
+ ```
679
718
680
719
681
720
```{exercise}
682
721
:label: inequality_ex2
683
722
684
723
According to the definition of the top shares {eq}`topshares` we can also calculate the top percentile shares using the Lorenz curve.
685
724
686
-
687
725
Compute the top shares of US net wealth using the corresponding Lorenz curves data: ``f_vals_nw, l_vals_nw`` and linear interpolation.
688
726
689
727
Plot the top shares generated from Lorenz curve and the top shares approximated from data together.
0 commit comments