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