<jupyter_start><jupyter_text>Measurements of Inequality  The Lorenz CurveThe Lorenz Curve visually presents income inequality by plotting household income percentile on the $x$-axis, and the cumulative income share that the bottom $x$ percentile own on the $y$-axis. The households are sorted by income, so that the first household at the 0th percentile has the least amount of income, while the household at the 100th percentile has the greatest income.For any point $(x,y)$ on the Lorenz curve, “the bottom x percent own y% of the income”. For example, if the $x$-axis reads 0.30 and $y$-axis reads 0.10, then it means that the bottom 30% of the population received 10% of the total population's income. This yields 2 implications for the Lorenz Curve:- The points $(0,0)$ and $(1,1)$ are always on the curve. $(0,0)$ represents the 0% of the population owning 0% of the income and $(1,1)$ represents 100% of the population owning 100% of the income.- The slope is always increasing. This is because households are sorted by income as percentiles: for a slight increase in $x$, households become richer and hence provide a larger share of total income.  Line of Perfect EqualityIn a world of perfect equality, everyone would have the exact same income. In this case, the Lorenz curve would just be a 45-degree straight line that runs through $(0,0)$ and $(1,1)$, i.e. $y=x$. Mathematically, this is because the derivative is constant: for a slight increase in $x$, the total share of income increases at a constant rate. Another way to think about this is that the bottom 10% of the population will own 10% of the total income, the bottom 50% of the population will own 50% of the total income, and so on. This line is known as the *line of perfect equality*, and we typically display this line when plotting our Lorenz curves as a reference.  A Toy ExampleLet's suppose country 1 has the following income distribution: - The bottom 10% owns a cumulative 2% of total income - The bottom 20% owns 5% of total income- The bottom 30% owns 9% of total income- The bottom 40% owns 15% of total income - The bottom 50% owns 23% of total income- The bottom 60% owns 33% of total income- The bottom 70% with 45% of total income- The bottom 80% with 59% of total income - The bottom 90% with 75% of total income- The bottom 100% with 100% of total incomeWe will create an array of income shares and call it `Country1`.<jupyter_code>Country1 = make_array(0, 0.02, 0.05, 0.09, 0.15, 0.23, 0.33, 0.45, 0.59, 0.75, 1.0)<jupyter_output><empty_output><jupyter_text>To better see this information, we will create a table containing population percentage and cumulative income share.<jupyter_code>income_distribution = Table().with_columns(
    "Population Percentage (%)", np.arange(11) * 10, 
    "Cumulative Income Share (%)", Country1 * 100, 
    "Perfect Equality Income Share (%)", np.arange(11) * 10
)
income_distribution<jupyter_output><empty_output><jupyter_text>How will the Lorenz Curve for this income distribution look?<jupyter_code>income_distribution.scatter(0, 1, width=11, height=7)
plt.plot([0,100], [0,100], color='k');<jupyter_output><empty_output><jupyter_text>Comparing Lorenz CurvesNow let's compare 2 countries' Lorenz curves. Suppose country 2 has the following income distribution:- The bottom 10% owns a cumulative 3% of total income - The bottom 20% owns 7% of total income- The bottom 30% owns 13% of total income- The bottom 40% owns 19% of total income - The bottom 50% owns 27% of total income- The bottom 60% owns 37% of total income- The bottom 70% with 50% of total income- The bottom 80% with 65% of total income - The bottom 90% with 81% of total income- The bottom 100% with 100% of total income<jupyter_code>Country2 = make_array(0, 0.03, 0.07, 0.13, 0.19, 0.27, 0.37, 0.5, 0.65, 0.81, 1.0)
income_distribution2 = Table().with_columns(
    "Population Percentage (%)", np.arange(11) * 10, 
    "Cumulative Income Share (%)", Country2 * 100, 
    "Perfect Equality Income Share (%)", np.arange(11) * 10
)
income_distribution2<jupyter_output><empty_output><jupyter_text>Comparing the 2 countries' income distributions side by side:<jupyter_code>income_distribution.join(
    ["Population Percentage (%)", "Perfect Equality Income Share (%)"], 
    income_distribution2, ["Population Percentage (%)", "Perfect Equality Income Share (%)"]
).relabel(
    "Cumulative Income Share (%)", "Country 1 Cumulative Income Share (%)"
).relabel(
    "Cumulative Income Share (%)_2", "Country 2 Cumulative Income Share (%)"
)<jupyter_output><empty_output><jupyter_text>Plotting both countries' Lorenz curves, can you tell which country has a higher level of income inequality?<jupyter_code>plt.figure(figsize=[7,7])
plt.plot(income_distribution.column(0), income_distribution.column(1), "-o", c = 'b')
plt.plot(income_distribution.column(0), income_distribution2.column(1), "-o", c = 'r')
plt.legend(["Country 1", "Country 2"])
plt.plot([0,100], [0,100], color='k');<jupyter_output><empty_output><jupyter_text>In this case, we can see that country 2's Lorenz curve is closer to the line of equality than that of country 1, which intuitively would suggest that country 2 is more equal. If we were to look at the numbers, we see that the bottom percentiles own a higher % of total national income in country 2 than in country 1, while top percentiles own less in country 2 than in country 1. This would suggest that country 2 is more equal in income than country 1, so that country 1 has a higher level of income inequality. But now let's consider a different case; suppose country 3 has the following distribution:<jupyter_code>Country3 = make_array(0, 0.03, 0.07, 0.12, 0.18, 0.25, 0.33, 0.42, 0.54, 0.73, 1.0)
income_distribution3 = Table().with_columns(
    "Population Percentage (%)", np.arange(11) * 10, 
    "Cumulative Income Share (%)", Country3 * 100, 
    "Perfect Equality Income Share (%)", np.arange(11) * 10
)
income_distribution.join(
    ["Population Percentage (%)", "Perfect Equality Income Share (%)"], 
    income_distribution3, ["Population Percentage (%)", "Perfect Equality Income Share (%)"]
).relabel(
    "Cumulative Income Share (%)", "Country 1 Cumulative Income Share (%)"
).relabel(
    "Cumulative Income Share (%)_2", "Country 3 Cumulative Income Share (%)"
)
plt.figure(figsize=[7,7])
plt.plot(income_distribution.column(0), income_distribution.column(1), "-o", c = 'b')
plt.plot(income_distribution.column(0), income_distribution3.column(1), "-o", c = 'r')
plt.legend(["Country 1", "Country 3"])
plt.plot([0,100], [0,100], color='k');<jupyter_output><empty_output><jupyter_text>Now, ambiguity arises; while bottom income percentiles earn a larger share of national income in country 3, top income percentiles also have a larger share. We can visualize this phenomenon by the 'crossing' of Lorenz curves on the plot. As a result, we do cannot easily tell which country has a higher level of income inequality.As you may see, the Lorenz curve is not able to produce a 'quantitative' measure of income inequality, making the scenario above hard for us to compare the 2 countries. For this, we turn to the Gini coefficient.  The Gini Coefficient We can use the Gini coefficeint to quantify the level of income inequality. ```{figure} Gini.png---width: 500pxname: gini-coefficient---The Gini coefficient``` ```{admonition} DefinitionThe **Gini coefficient** is the ratio of the area between the line of equality and the Lorenz curve to the total area under the line of equality. Referring to $A$ and $B$ from {numref}`gini-coefficient`:$$\text{Gini} = \frac{\text{Area between line of equality and Lorenz curve}}{\text{Area under line of equality}} = \frac{A}{A+B}$$If we express the Lorenz curve as $L(x)$, we can use calculus to derive an equation for the Gini coefficient:$$\text{Gini} = \frac{\frac{1}{2} - \int_0^1 L(x)\text{d}x}{\frac{1}{2}} = 1 - 2\int_0^1 L(x)\text{d}x$$``` Intuitively, the closer the Lorenz curve is to the line of equality, the lower income inequality exists. Hence, the smaller the area of A, the lower the inequality. **This means that the smaller the Gini coefficient, the lower the income inequality.** Also note that the Gini coefficient will always be between 0 and 1. Mathematically, since $A$ and $B$ are both positive, $0<\frac{A}{A+B}<1$.<jupyter_code># This function estimates the Gini coefficient. You don't have to understand how this code works below.
def gini(distribution):
    sorted_distribution = sorted(distribution)
    height = 0
    area = 0
    for i in sorted_distribution:
        height += i
        area += height - i / 2
    fair_area = height * len(distribution) / 2.
    return (fair_area - area) / fair_area<jupyter_output><empty_output><jupyter_text>When we use our population as the parameter to the `gini` function, we get:<jupyter_code>gini_coefficient_country1 = gini(Country1)
gini_coefficient_country1
gini_coefficient_country2 = gini(Country2)
gini_coefficient_country2
gini_coefficient_country3 = gini(Country3)
gini_coefficient_country3<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.102160017632797, 'fraction_numerical': 0.03945338329292484, 'mean_word_length': 4.919765166340508, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '364435', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2969, 'n_tokens_neox': 2572, 'n_words': 1175, 'path': 'docs/_sources/content/10-inequality/inequality.ipynb', 'repo_name': 'ds-connectors/econ-models-textbook', 'size': '9074'}