Closed mmcky closed 3 months ago
Hi Matt @mmcky ,
Maybe we can add the following paragraph to illustrate this vectorized function using simulated data?
Let's simulate five populations by drawing from a lognormal distribution as before
```{code-cell} ipython3
k = 5
σ_vals = np.linspace(0.2, 4, k)
n = 2_000
σ_vals = σ_vals.reshape((k,1))
μ_vals = -σ_vals**2/2
y_vals = np.exp(μ_vals + σ_vals*np.random.randn(n))
We can compute the Gini coefficient for these five populations using the vectorized function as follows,
gini_coefficients =[]
for i in range(k):
gini_coefficients.append(gini(simulated_data[i]))
This gives us the Gini coefficients for these five households.
gini_coefficients
Best,
Longye
thanks @longye-tian -- if you can prepare a PR that sounds great. We can work on this together in that branch.
We can add this as an exercise to this lecture.
Hi Matt, I think this issue is closed by pull request #498.
Best, Longye
We can write a new exercise in the
inequality
lecture to teach the difference in python loops and vectorization.Here is a starting point for the exercise.
Let's take a look at some raw data for the US that is stored in
df_income_wealth
We will focus on wealth variable
n_wealth
to compute a Gini coefficient for the year 1990.We can first compute the Gini coefficient using the function defined in the lecture above.
Now we can write a vectorized version using
numpy