Lesson 7.2: Gini, the CAP curve and the area under the curve
Read the CAP and ROC curves, compute Gini and AUC, relate them by Gini = 2 × AUC − 1, find the lift in the riskiest deciles and put a range on the AUC.
By the end of this lesson you will be able to draw and read a CAP curve and a ROC curve for a scorecard, compute the Gini coefficient from the first and the area under the curve, or AUC, from the second, show that the two are linked by one simple formula, read the lift in the riskiest deciles, compare a model with a random one and with a perfect one, and attach a confidence interval to an AUC so that you know how much a difference between two models is worth. You should have read Lesson 7.1, which introduced the confusion matrix and the KS statistic, and Lesson 6.2, which built the scores. The arithmetic is cumulative percentages and areas under a curve, and every figure comes from the same holdout sample of 3,000 loans, 313 of them bad.
The KS statistic of Lesson 7.1 uses one point of the score range, where two cumulative curves are furthest apart. That throws away most of the information in the curves. Two models can have the same KS and differ greatly in the rest of the range. A better summary uses the whole curve, and the two standard versions are the CAP curve with its Gini coefficient, favoured in credit risk and banking supervision, and the ROC curve with its AUC, favoured in statistics and machine learning. They are two views of the same thing.
The CAP curve
CAP stands for cumulative accuracy profile, and the curve is also called the Lorenz curve or the power curve. It answers the question: if I rank applicants from the riskiest to the safest and reject the riskiest share, what share of all the bad loans do I catch?
To build it, sort the 3,000 holdout loans from the lowest score to the highest. For each point on the list, record the share of all loans passed so far (the horizontal axis) and the share of all bad loans among them (the vertical axis). For the holdout sample, at every tenth of the population:
Riskiest share of loans rejected · Bad loans caught · Share of all bad loans caught10% · 87 · 27.8%
20% · 133 · 42.5%
30% · 172 · 55.0%
40% · 200 · 63.9%
50% · 227 · 72.5%
60% · 254 · 81.2%
70% · 272 · 86.9%
80% · 288 · 92.0%
90% · 306 · 97.8%
100% · 313 · 100.0%
The riskiest 10 per cent of applicants, 300 loans, contain 87 of the 313 bad loans, or 27.8 per cent. A random list would have contained 10 per cent of them, about 31, so the lift at the top decile is 27.8 ÷ 10 = 2.78: the riskiest tenth of the book is nearly three times as bad as average. The riskiest 20 per cent contain 42.5 per cent of the bads, a lift of 2.1, and so on. Lift falls steadily to 1.0 at the end of the list, where everyone is included.
Two reference curves frame the picture.
The random model. A score with no information would catch bad loans in proportion to the share rejected: 10 per cent of bads at 10 per cent of the population. This is the straight diagonal line from corner to corner.
The perfect model. A model that placed every bad loan at the front would catch all 313 within the first 313 loans, which is 313 ÷ 3,000 = 10.4 per cent of the list, and then stay at 100 per cent. The perfect curve climbs steeply to 100 per cent at 10.4 per cent of the population and runs flat afterwards.
The Gini coefficient
The Gini coefficient, also called the accuracy ratio or Somers' D in credit work, summarises how much of the available improvement the model delivers. Take the area between the model's curve and the random diagonal, and divide it by the area between the perfect curve and the diagonal.
> Gini = (area under the model's CAP curve − 0.5) ÷ (area under the perfect CAP curve − 0.5)
The area under the diagonal is 0.5. The area under the perfect curve depends on the bad rate: it is 1 − (bad rate) ÷ 2, which for a bad rate of 10.43 per cent is 1 − 0.0522 = 0.9478. The area under the scorecard's CAP curve, computed from the full curve of 97 points and not only the deciles in the table, is 0.6737.
> Gini = (0.6737 − 0.5) ÷ (0.9478 − 0.5) = 0.1737 ÷ 0.4478 = 0.388
So the scorecard delivers 38.8 per cent of the possible improvement over random guessing. A Gini of zero means the model is no better than chance, and a Gini of one means a perfect ranking. A Gini above 1 or below 0 is impossible, and a negative Gini would mean the score points the wrong way.
The ROC curve and the AUC
The second view builds a different curve. Move the cut-off along the score range, and for each cut-off record two numbers from the confusion matrix of Lesson 7.1:
True positive rate (sensitivity): the share of bad loans rejected.
False positive rate: the share of good loans rejected, which is one minus the specificity.
Plot the true positive rate on the vertical axis against the false positive rate on the horizontal axis. This is the receiver operating characteristic curve, or ROC, a name from radar engineering. Every point on it is a cut-off. For the scorecard, selected points are:
False positive rate (good loans rejected) · True positive rate (bad loans rejected)5.1% · 21.7%
11.3% · 33.2%
21.2% · 47.6%
30.9% · 58.8%
51.4% · 75.7%
A useless model gives the diagonal, where the two rates are equal. A perfect model goes straight up the left side to 100 per cent true positives with no false positives, and then across the top. The better the model, the more the curve bulges towards the top left corner.
The area under the ROC curve, the AUC, is the standard summary: it runs from 0.5 for a random model to 1 for a perfect one. For the scorecard it is 0.694 on the holdout, computed on the whole-point scores. (The 0.692 quoted in Lesson 6.1 was computed on the unrounded model probabilities. Rounding to whole points creates ties between loans and moves the figure by 0.002. The difference is a reminder that a performance figure depends on exactly what was scored.)
Gini and AUC are the same information
The Gini from the CAP curve and the AUC from the ROC curve look like two different calculations, and the numbers differ, 0.388 against 0.694. They are in fact tied by a fixed formula.
> Gini = 2 × AUC − 1, or equivalently AUC = (Gini + 1) ÷ 2
Check it: 2 × 0.694 − 1 = 0.388, matching the Gini from the CAP curve of 0.388. And (0.388 + 1) ÷ 2 = 0.694. The relation holds exactly, whatever the bad rate, so a lender that reports Gini and a data scientist that reports AUC are describing the same ranking power on different scales. The Gini rescales the AUC so that a random model scores 0 and a perfect one scores 1, and for this reason credit professionals often prefer it: its zero point is more natural.
Predict first: a bank's validation team reports a Gini of 0.50 for a new application scorecard. What AUC does that correspond to? And what Gini corresponds to an AUC of 0.80? Work both out before reading on.
AUC = (0.50 + 1) ÷ 2 = 0.75. For an AUC of 0.80, Gini = 2 × 0.80 − 1 = 0.60. The Gini always moves by two points of AUC at a time, so differences in Gini look twice as large as the same differences in AUC. When someone says a model improved "from 0.60 to 0.62", ask which measure they mean.
With the defaults, the standard error is about 0.017, so the 95 per cent range for the AUC is roughly 0.660 to 0.728, and the corresponding range for the Gini is 0.32 to 0.46. That is a wide band. A difference of 0.01 or 0.02 between two models tested on this sample could easily be noise. To halve the width of the range you need about four times as many bad loans. This is why validation teams insist on enough bad loans in a test sample, and why they test models on the same loans with a paired method before declaring a winner.
Reading lift and capture
The CAP curve gives more than one number. Several business-friendly readings come from it.
Capture rate at a given rejection share. The riskiest 20 per cent of applicants contain 42.5 per cent of the bad loans. A lender that can afford to decline one applicant in five avoids more than two-fifths of the bads.
Lift in a decile. Dividing the share of bads in a group by the share of the population gives the lift. The top decile has a lift of 2.78, and the second decile, which adds 15 percentage points of bads (42.5 − 27.8 = 14.7) for 10 points of population, has a lift of 1.47 on its own.
Bad rate by decile or band. From the band table of Lesson 7.1, the bad rate falls from 48.5 per cent in the lowest band to about 5 per cent in the 590 to 599 band. A well-working score gives a bad rate that falls steadily as the score rises, with no reversal. The table does show two small reversals at the top (7.0 per cent at 600 to 609, and 4.1 per cent at 620 to 629), both from few bad loans, 15 and 4, so they are noise.
These readings are easier to explain to a business audience than AUC, and they are tied to decisions: how many to reject, how many bads that avoids.
When to use which
Measure · Strength · WeaknessKS · Easy to explain, gives the best separation point · Uses only one point of the curves
Gini · Whole ranking in one number, zero for random, familiar to banks and supervisors · Hides the shape of the curve
AUC · Same information as Gini, standard in statistics and machine learning · Less natural zero point, can be misread as "accuracy"
Lift and capture · Easy to link to decisions · Depend on the chosen cut-off
A thorough validation report shows all of them, along with the curves. Two models can have the same AUC and different shapes: one that is strong in the riskiest tenth and weak elsewhere, and another that is consistent throughout. The curve shows which, and which matters depends on the use: a lender that rejects only the riskiest 10 per cent cares about the left end of the CAP curve, and one that approves only the safest 30 per cent cares about the right end.
What do the numbers usually look like?
There are no fixed standards, but a rough guide helps to place a result. For retail application scorecards, Gini values between about 0.30 and 0.60 are common, depending on the data available and the product. A thin-file market with little bureau data gives lower values. Behavioural scorecards, which use the repayment history on the account, are often stronger, with Gini values of 0.60 to 0.80. Corporate rating models and models for rare defaults tend to be judged with different expectations because there are few defaults to learn from. A value above 0.90 on any model deserves a leakage check.
The synthetic scorecard of this module, with a Gini of 0.388, is in the range expected for an application scorecard built on a handful of characteristics.
Computing the curves in Python
The CAP and ROC curves both come from sorting by score and taking cumulative sums.
python
import numpy as np
import pandas as pd
hold = pd.readcsv("holdoutscores.csv") # score (higher = safer), bad (1/0)
s = hold.sortvalues("score").resetindex(drop=True) # riskiest first
n, nb = len(s), s["bad"].sum()
ng = n - nb
CAP curve: share of population against share of bads caught
capx = np.arange(1, n + 1) / n
capy = s["bad"].cumsum() / nb
caparea = np.trapz(np.r[0, capy], np.r[0, capx])
idealarea = 1 - (nb / n) / 2
gini = (caparea - 0.5) / (idealarea - 0.5)
ROC curve: share of goods rejected against share of bads rejected
rocx = (1 - s["bad"]).cumsum() / ng
rocy = s["bad"].cumsum() / nb
auc = np.trapz(np.r[0, rocy], np.r[0, rocx])
print("Gini from CAP", round(gini, 3), "AUC from ROC", round(auc, 3),
"2AUC-1", round(2 auc - 1, 3))
deciles: lift and capture
s["decile"] = pd.qcut(s.index, 10, labels=False) + 1
d = s.groupby("decile")["bad"].agg(loans="count", bads="sum")
d["capture%"] = (d["bads"].cumsum() / nb 100).round(1)
d["lift"] = (d["bads"] / nb) / (d["loans"] / n)
print(d)
Tied scores create flat stretches in the curves, because a block of loans with the same score cannot be ranked among themselves. A careful implementation groups tied scores before computing the curves, which is what a standard library function does, and that can move the area by a small amount. Check your own numbers against such a function.
A worked example: two models, one number
A bank compares a scorecard with a challenger. The AUCs on the same 3,000-loan holdout are 0.694 and 0.711. The analyst answers the question "is the challenger better?" in four steps.
1. Convert to a common scale. Gini is 0.388 for the scorecard and 0.422 for the challenger, a difference of 0.034.
2. Put the noise next to it. With 313 bad loans the standard error of each AUC is about 0.017, and the difference between them, though smaller than the sum because the models are tested on the same loans, is still of the order of 0.01 in AUC. The observed difference of 0.017 in AUC is within about one to two standard errors of the difference, so it is suggestive and not conclusive.
3. Look at the shape. She draws both CAP curves. The challenger catches 29.5 per cent of bads in the riskiest decile against 27.8 per cent, and the two curves meet around the 50 per cent point. The gain is at the top of the risk range, where the lender's decisions are made.
4. Decide what to do. She recommends testing the challenger on a later period before adopting it, since the evidence is not yet strong, and notes that the improvement is at the right end of the curve for the lender's cut-off. This combines a statistic, a picture and a business judgement.
Common mistakes
Mixing up Gini and AUC. Say which one is meant, and convert with 2 × AUC − 1.
Reading AUC as accuracy. An AUC of 0.70 does not mean 70 per cent of loans are classified correctly. It is the chance that a random bad loan has a worse score than a random good loan.
Ignoring the noise. Put a range on any AUC, and be careful about small differences.
Comparing models on different samples. Use the same holdout, with a paired test.
Trusting the area alone. Two models with equal areas can have different curves. Look at the picture.
Forgetting ties. Rounded scores create ties that shift the numbers slightly.
Forgetting the bad rate when comparing across portfolios. The CAP ideal curve and lift depend on it.
Treating a very high value as good news. Above 0.90, look for leakage.
Key takeaways
The CAP curve plots the share of bad loans caught against the share of loans rejected, riskiest first. The random model is the diagonal and the perfect model climbs to 100 per cent at the bad rate (10.4 per cent here).
Gini is the area between the CAP curve and the diagonal divided by the same area for the perfect model. For the scorecard it is 0.388.
The ROC curve plots the share of bads rejected against the share of goods rejected. Its area, the AUC, is 0.694.
Gini = 2 × AUC − 1, so the two carry the same information. AUC 0.75 is Gini 0.50, and AUC 0.80 is Gini 0.60.
Lift tells how much worse than average a group is. The riskiest decile caught 27.8 per cent of the bads, a lift of 2.78.
With 313 bad loans the AUC has a standard error of about 0.017, so its 95 per cent range is roughly 0.66 to 0.73. Small differences between models are not reliable.
Check yourself
1. A model has an AUC of 0.72. What is its Gini?
2 × 0.72 − 1 = 0.44.
2. A portfolio has a bad rate of 8 per cent. What is the area under the perfect CAP curve?
1 − 0.08 ÷ 2 = 0.96.
3. The riskiest 10 per cent of applicants contain 30 per cent of all bad loans. What is the lift in that decile?
30 ÷ 10 = 3.0.
4. A Gini of 0 and a Gini of 1 describe which models?
0 describes a model no better than random, and 1 describes a perfect ranking.
5. Why can two models with the same AUC still differ in usefulness?
Their curves can have different shapes. One may be strong in the riskiest tenth where a lender rejects, and the other strong in the middle. The area hides that.
Try this
Draw and summarise the curves for a scorecard of your own. Set aside forty to fifty minutes.
1. Use the holdout scores and outcomes from Lesson 7.1 or invent at least 2,000 loans with about 10 per cent bad.
2. Sort from the lowest score to the highest. At every tenth of the population, record the number and share of bad loans caught. Draw the CAP curve with the diagonal and the perfect curve.
3. Compute the area under the CAP curve with the trapezoid rule, and the Gini using the formula in this lesson.
4. Compute ROC points at five or more cut-offs. Compute the AUC by the trapezoid rule and check that Gini equals 2 × AUC − 1.
5. Compute the lift of each of the first three deciles and write one sentence on what the top decile means for the lender.
6. Use the simulator to find a 95 per cent range for your AUC from your numbers of bad and good loans.
7. Write a five-sentence note for a model reviewer: the Gini, the AUC and the range, the lift at the top decile, one weakness of using the area alone and what you would test next.
A good result is two curves, a Gini and an AUC that agree through the formula, a lift you can explain and a range that shows how much to trust them. Lesson 7.3 shows four different ways of calculating the AUC by hand, and why they all give the same answer.
Tags: Credit Risk Analytics Gini CAP Curve AUC ROC Curve