Inequality: Gini Coefficient and Lorenz Curve

easy
Unit 2 · Statistics II0/3 to milestone

How concentrated is revenue across customers? The Lorenz curve plots the cumulative share of revenue against the cumulative share of customers, sorted from smallest to largest. The Gini coefficient is twice the area between that curve and the line of perfect equality: 0 means everyone spends the same, and values near 1 mean one customer has nearly everything.

Implement for non-negative values:

  • lorenz_curve(x): return (pop_share, value_share) arrays of length n + 1, both starting at 0 and ending at 1
  • gini(x): using the sorted-values formula G=∑i(2i−n−1) x(i)n∑ixiG = \frac{\sum_i (2i - n - 1)\,x_{(i)}}{n \sum_i x_i} with i = 1…n. Return 0.0 if the total is 0.
  • top_share(x, fraction): the share of the total held by the top fraction of values (rounding the count up)
#statistics#inequality#concentration2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Inequality: Gini Coefficient and Lorenz Curve · TechGradify