@@ -354,7 +354,7 @@ distribution.)
354354
355355``` {code-cell} ipython3
356356k = 5
357- σ_vals = np.linspace(0.2, 2.5 , k)
357+ σ_vals = np.linspace(0.2, 4 , k)
358358n = 2_000
359359
360360ginis = []
@@ -387,7 +387,7 @@ mystnb:
387387 image:
388388 alt: gini_simulated
389389---
390- plot_inequality_measures(range(k) ,
390+ plot_inequality_measures(σ_vals ,
391391 ginis,
392392 'simulated',
393393 '$\sigma$',
@@ -411,7 +411,7 @@ The following code creates a list called ``Ginis``.
411411:tags: [hide_input]
412412
413413varlist = ['n_wealth', # net wealth
414- 't_income', # total income
414+ 't_income', # total income
415415 'l_income'] # labor income
416416
417417df = df_income_wealth
@@ -646,14 +646,21 @@ def calculate_top_share(s, p=0.1):
646646
647647```{code-cell} ipython3
648648k = 5
649- σ_vals = np.linspace(0.2, 2.5 , k)
649+ σ_vals = np.linspace(0.2, 4 , k)
650650n = 2_000
651651
652652topshares = []
653+ ginis = []
654+ f_vals = []
655+ l_vals = []
653656
654657for σ in σ_vals:
655658 μ = -σ ** 2 / 2
656659 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))
657664 topshares.append(calculate_top_share(y))
658665```
659666
@@ -666,24 +673,55 @@ mystnb:
666673 image:
667674 alt: top_shares_simulated
668675---
669- plot_inequality_measures(range(len(topshares)) ,
676+ plot_inequality_measures(σ_vals ,
670677 topshares,
671678 "simulated data",
672- "year ",
679+ "$\sigma$ ",
673680 "top $10\%$ share")
674681```
675682
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")
677697```
678698
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+ ```
679718
680719
681720```{exercise}
682721:label: inequality_ex2
683722
684723According to the definition of the top shares {eq}`topshares` we can also calculate the top percentile shares using the Lorenz curve.
685724
686-
687725Compute the top shares of US net wealth using the corresponding Lorenz curves data: ``f_vals_nw, l_vals_nw`` and linear interpolation.
688726
689727Plot the top shares generated from Lorenz curve and the top shares approximated from data together.
0 commit comments