Calculate the Gini coefficient (Gini index) from a list of incomes or other values, or Gini impurity from class counts for a decision tree node.
Related Calculators
- Gain Ratio Calculator
- Dominance Index Calculator
- Concordance Index Calculator
- Gamma Index Calculator
- All Statistics Calculators
Calculator Functions
| Function | What it does | What it requires |
|---|---|---|
| Gini coefficient from values | Finds how unequally a list of incomes or other values is shared | Requires a list of non-negative values |
| Gini impurity from class counts | Finds how mixed the classes are in one group of items | Requires the number of items in each class |
| Weighted Gini of a decision-tree split | Finds the impurity left after a split, with the parent impurity and the Gini gain | Requires the class counts for each child node, one node per line |
Entries can be separated by commas, spaces or new lines, without thousands separators. The first function takes up to 10,000 values and gives every value equal weight. The coefficient is shown from 0 to 1 and again as an index from 0 to 100.
Gini Index Formulas
(n ร ฮฃyi) [1]
| G | Gini coefficient. Multiply by 100 for the Gini index |
| n | Number of values |
| yi | The i-th value after sorting from smallest to largest |
| i | Rank of the value, from 1 to n |
Gini coefficient from values
The main formula applies after the values are sorted in ascending order. For 1, 2, 3, 4, 5 the weights (2i โ n โ 1) are โ4, โ2, 0, 2, 4, so the top sum is โ4 โ 4 + 0 + 8 + 20 = 20. The bottom is 5 ร 15 = 75, and G = 20 รท 75 = 0.266667, a Gini index of 26.6667.
The same number comes from adding the absolute difference between every pair of values and dividing by 2n ร ฮฃy [1]. Equal values give 0. With n values the largest possible result is 1 โ 1/n, reached when one value holds everything, so 5 values cannot score above 0.8. Multiplying by n รท (n โ 1) turns the result into an unbiased estimate for a larger population [1]. The calculator does not apply that correction.
Gini impurity from class counts
pk is the share of items in class k, and the formula is the same as ฮฃpk(1 โ pk) [2]. A node with 40 items of one class and 10 of another has shares 0.8 and 0.2, so G = 1 โ 0.64 โ 0.04 = 0.32. A single-class node scores 0, and k equally filled classes score the maximum, 1 โ 1/k. This measures class mixing in a classification tree. It is a different quantity from the income-inequality coefficient, although both carry the Gini name.
Weighted Gini of a decision-tree split
Each child node j has nj items and its own impurity Gj, and N is the total number of items. Children are weighted by size, not equally [2]. For a child with counts 40 and 10 (G = 0.32) and a child with counts 5 and 45 (G = 0.18), each holding half of the 100 items, the weighted Gini is 0.5 ร 0.32 + 0.5 ร 0.18 = 0.25.
The parent node is the column totals, 45 and 55, with impurity 1 โ 0.2025 โ 0.3025 = 0.495. Gini gain is the parent impurity minus the weighted Gini, 0.495 โ 0.25 = 0.245. A larger gain means the split separates the classes better. Every line must list the classes in the same order, with a zero for a class that is absent.
Limits of This Estimate
- The coefficient treats every value as one equally weighted observation. Survey weights, grouped income brackets and negative values such as net debt are not handled.
- No small-sample correction is applied, so short lists give a lower coefficient than the population they were drawn from would.
- The split function scores the child nodes that are entered. It does not search for the best feature or threshold, and it takes up to 100 children and 100 classes.
Sources
- Wolfram MathWorld, Gini Coefficient: the sorted-values formula, the pairwise-difference form and the n รท (n โ 1) correction
- scikit-learn documentation, 1.10 Decision Trees, Mathematical formulation: Gini impurity of a node and the sample-weighted impurity of a split
