# Calculating Gini Coeffecient

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.

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.

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.

  | 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.

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.

• The question asked how to compute Gini manually. Running code clearly doesn't count as a manual solution. – TheSaint321 Apr 26 '17 at 18:36
• @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. – Amit Apr 27 '17 at 14:04
• 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. – BKay Jan 10 '19 at 15:15

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

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.

• Question asks for how to compute the answer manually. I doubt excel qualifies. – TheSaint321 Apr 26 '17 at 18:37
• Disagree. The process is spelled out clearly in the headers. Translates to paper just as easily as your response translates to excel. – c4sadler Apr 26 '17 at 20:15

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


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.

• The question asks for a manual solution. – TheSaint321 Apr 26 '17 at 18:39