2
$\begingroup$

What's the fastest way (or formulae) of manually calculating Gini Coefficient of equal-sized bin income data?

Example: No of individuals in group: 4 4 4 4 4 Income of each group: 1 2 3 4 5 respectively.

$\endgroup$
0

5 Answers 5

7
$\begingroup$

All of these answers are true but don't provide an easy solution which doesn't use excel/code. Gini can be fairly easily computed by hand too.

The Gini coefficient fundamentally shows the shaded region above the lorenz curve in order to get a relative gauge of the distance the lorenz curve is from the line of equality. Fundamentally what this shows is the average distance between incomes. with 1 being the highest possible income and 0 being the lowest.

One way to calculate this is to simply calculate the area between the line of equality and the Lorenz curve. enter image description here

This is clearly pretty difficult to do. Luckily there is an easier method.

Gini is also equal to: (The average variance for all incomes) / (2 * the mean)

The average variance for all incomes can be calculated by constructing a matrix of all incomes and then calculating the distance between each income.

For your example:

  | 1   2   3   4   5
__|___________________
1 | 0   1   2   3   4
2 | 1   0   1   2   3
3 | 2   1   0   1   2
4 | 3   2   1   0   1
5 | 4   3   2   1   0

Then add up all of the distances within the matrix to get the total variance.

0 + 1 + 2 + 3 + 4 + 1 + 0 + 1 + 2 + 3 + 2 + 1 + 0 + 1 + 2 + 3 + 2 + 1 + 0 + 1 + 4 + 3 + 2 + 1 + 0 = 40.

Then divide it by the total number of elements within the matrix to get the average variance.

Average Variance = 40 / 25 = 1.6.

Now divide the average variance by the average income times two.

Mean = (1 + 2 + 3 + 4 + 5)/5 = 15/5 = 3

Mean*2 = 6.

Since the gini is equal to: (The average variance for all incomes) / (2 * the mean):

Gini = 1.6 / 6 = .2666

Which is what the other users got through more complicated methods.

$\endgroup$
2
$\begingroup$

One of the fastest ways to calculate Gini Coefficient for any data set is running the following code:

Gini Coefficient and Lorenz Curve - in Python

When you will run this code, you will get the Gini Coefficient as 0.266666666667. Code will also generate the Lorenz curve.

$\endgroup$
3
  • $\begingroup$ The question asked how to compute Gini manually. Running code clearly doesn't count as a manual solution. $\endgroup$ Commented Apr 26, 2017 at 18:36
  • $\begingroup$ @TheSaint321 I agree your answer is more appropriate and answers the question precisely, but I guess my answer might be more useful for future visitors. The code works for any input. It will provide the Gini Coefficient and Lorenz curve by a single click. $\endgroup$
    – Amit
    Commented Apr 27, 2017 at 14:04
  • $\begingroup$ Due to the potential for bit rot, this is not an ideal answer, even for those desiring python code instead of an algorithm. This answer could be improved by working the code (properly cited) into the body of the answer. $\endgroup$
    – BKay
    Commented Jan 10, 2019 at 15:15
1
$\begingroup$

Below is a picture of how to use excel to calculate the necessary values in order to get the Gini Coeffecient.

enter image description here

Once you get the total value, the Gini Coefficient is calculated as $1-\text{total value}$, which in this case is $1-0.733=0.267$ rounded to the nearest thousandth.

$\endgroup$
2
  • $\begingroup$ Question asks for how to compute the answer manually. I doubt excel qualifies. $\endgroup$ Commented Apr 26, 2017 at 18:37
  • 1
    $\begingroup$ Disagree. The process is spelled out clearly in the headers. Translates to paper just as easily as your response translates to excel. $\endgroup$
    – c4sadler
    Commented Apr 26, 2017 at 20:15
0
$\begingroup$

Here is how you can calculate it with SQL:

with balances as (
    select '2018-01-01' as date, balance
    from unnest([1,2,3,4,5]) as balance -- Gini coef: 0.2666666666666667
    union all
    select '2018-01-02' as date, balance
    from unnest([3,3,3,3]) as balance -- Gini coef: 0.0
    union all
    select '2018-01-03' as date, balance
    from unnest([4,5,1,8,6,45,67,1,4,11]) as balance -- Gini coef: 0.625
),
ranked_balances as (
    select date, balance, row_number() over (partition by date order by balance desc) as rank
    from balances
)
SELECT date, 
    -- (1 − 2B) https://en.wikipedia.org/wiki/Gini_coefficient
    1 - 2 * sum((balance * (rank - 1) + balance / 2)) / count(*) / sum(balance) AS gini
FROM ranked_balances
GROUP BY date
ORDER BY date ASC
-- verify here http://shlegeris.com/gini

Explanation is here https://medium.com/@medvedev1088/calculating-gini-coefficient-in-bigquery-3bc162c82168

$\endgroup$
-1
$\begingroup$

A super simple calculator, assuming equally sized bins, is here. Simply input your data, comma reparated (e.g. 1,2,3,4,5 in your case), and you get the result, together with a nice graph!

This confirms that Gini in your example is 0.267, as the other two people suggested.

enter image description here

$\endgroup$
1
  • $\begingroup$ The question asks for a manual solution. $\endgroup$ Commented Apr 26, 2017 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.