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

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

<p>One way to calculate this is to simply calculate the area between the line of equality and the Lorenz curve.
<a href="https://i.sstatic.net/iawuF.jpg" rel="noreferrer"><img src="https://i.sstatic.net/iawuF.jpg" alt="enter image description here"></a></p>

<p>This is clearly pretty difficult to do. Luckily there is an easier method.</p>

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

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

<p><strong>For your example:</strong></p>

<pre><code>  | 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
</code></pre>

<p><strong>Then add up all of the distances within the matrix to get the total variance.</strong></p>

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

<p><strong>Then divide it by the total number of elements within the matrix to get the average variance.</strong></p>

<p>Average Variance = 40 / 25 = 1.6.</p>

<p><strong>Now divide the average variance by the average income times two.</strong></p>

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

<p>Mean*2 = 6.</p>

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

<p>Gini = 1.6 / 6 =  .2666</p>

<p>Which is what the other users got through more complicated methods.</p>

CC BY-SA 3.0
2017-04-26T18:32:47.160Z
2017-04-26T18:42:25.630Z
2017-04-26T18:42:25.630Z
TheSaint321
<p>One of the fastest ways to calculate Gini Coefficient for any data set is running the following code:</p>

<p><a href="http://www.codeskulptor.org/#user43_hXQJURbrDGdh56D.py" rel="nofollow noreferrer">Gini Coefficient and Lorenz Curve - in Python</a></p>

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

CC BY-SA 3.0
2017-04-26T18:36:57.940Z
The question asked how to compute Gini manually. Running code clearly doesn't count as a manual solution.
TheSaint321
CC BY-SA 3.0
2017-04-27T14:04:04.563Z
@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
CC BY-SA 4.0
2019-01-10T15:15:39.513Z
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
CC BY-SA 3.0
2017-04-24T16:35:22.720Z
2017-04-24T16:35:22.720Z
Amit
<p>Below is a picture of how to use excel to calculate the necessary values in order to get the Gini Coeffecient.</p>

<p><a href="https://i.sstatic.net/Bzxkv.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Bzxkv.png" alt="enter image description here"></a></p>

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

CC BY-SA 3.0
2017-04-26T18:37:47.747Z
Question asks for how to compute the answer manually. I doubt excel qualifies.
TheSaint321
CC BY-SA 3.0
2017-04-26T20:15:48.887Z
Disagree. The process is spelled out clearly in the headers. Translates to paper just as easily as your response translates to excel.
c4sadler
CC BY-SA 3.0
2017-04-24T13:44:47.243Z
2017-07-26T08:29:19.937Z
2017-07-26T08:29:19.937Z
c4sadler
<p>Here is how you can calculate it with SQL:</p>

<pre><code>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
</code></pre>

<p>Explanation is here <a href="https://medium.com/@medvedev1088/calculating-gini-coefficient-in-bigquery-3bc162c82168" rel="nofollow noreferrer">https://medium.com/@medvedev1088/calculating-gini-coefficient-in-bigquery-3bc162c82168</a></p>

CC BY-SA 4.0
2019-01-10T07:25:32.383Z
2019-01-10T07:25:32.383Z
medvedev1088
<p>A super simple calculator, assuming equally sized bins, is <a href="http://shlegeris.com/gini" rel="nofollow noreferrer">here</a>. 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!</p>

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

<p><a href="https://i.sstatic.net/eUUR0.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/eUUR0.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2017-04-26T18:39:28.940Z
The question asks for a manual solution.
TheSaint321
CC BY-SA 3.0
2017-04-24T14:22:15.877Z
2017-04-24T14:22:15.877Z
luchonacho
<p>What's the fastest way (or formulae) of manually calculating Gini Coefficient of equal-sized bin income data?</p>

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

<statistics><inequality>
Calculating Gini Coeffecient
Yoda
# Calculating Gini Coeffecient

(Asked by: username_0 on 2017-04-24)

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.

**Tags:** statistics, inequality


---

## 5 answers

### Answer by username_1 on 2017-04-26. Score: 7

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](https://i.sstatic.net/iawuF.jpg)](https://i.sstatic.net/iawuF.jpg)

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.


---

### Answer by username_2 on 2017-04-24. Score: 2

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](http://www.codeskulptor.org/#user43_hXQJURbrDGdh56D.py)

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

> **Comments:**
> **username_1**: The question asked how to compute Gini manually. Running code clearly doesn't count as a manual solution.  
> **username_2**: @username_1  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.  
> **username_3**: 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.  

---

### Answer by username_4 on 2017-04-24. Score: 1

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](https://i.sstatic.net/Bzxkv.png)](https://i.sstatic.net/Bzxkv.png)

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.

> **Comments:**
> **username_1**: Question asks for how to compute the answer manually. I doubt excel qualifies.  
> **username_4**: Disagree. The process is spelled out clearly in the headers. Translates to paper just as easily as your response translates to excel.  

---

### Answer by username_5 on 2019-01-10. Score: 0

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/@username_5/calculating-gini-coefficient-in-bigquery-3bc162c82168>


---

### Answer by username_6 on 2017-04-24. Score: -1

A super simple calculator, assuming equally sized bins, is [here](http://shlegeris.com/gini). 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](https://i.sstatic.net/eUUR0.png)](https://i.sstatic.net/eUUR0.png)

> **Comments:**
> **username_1**: The question asks for a manual solution.  

---

