Lesson 5.2: Weight of evidence, turning bins into numbers
Compute weight of evidence for each bin from good and bad counts, read it as log odds, merge bins with equal WoE, handle zero cells and feed it to a scorecard.
By the end of this lesson you will be able to compute the weight of evidence of every bin of a variable from counts of good and bad loans, read it as a log of odds, check it for the right direction and spacing, spot bins that can be merged because they carry the same evidence, deal with bins that have no bad loans at all, and explain why scorecards use it in place of the raw bad rate. You should have read Lesson 5.1, which produced the bins used here. The arithmetic needs a calculator with a natural logarithm, and each calculation is shown in full for one bin so that you can check the rest.
Lesson 5.1 ended with five bins of credit utilisation and a bad rate for each: 4.50, 6.50, 8.75, 12.50 and 17.75 per cent. Bad rates are easy to read, but they are not the best numbers to put into a model. They run from zero to one, so equal steps in the bad rate do not mean equal steps in risk: a move from 1 to 2 per cent doubles the risk, while a move from 41 to 42 per cent hardly changes it. They also depend on how the sample was drawn. Weight of evidence, usually shortened to WoE, repairs both problems by working on the scale of log odds.
The idea: who is in this bin?
Think of the whole sample as two crowds: the good loans and the bad loans. A bin is a room. Ask what share of all the good loans is in that room, and what share of all the bad loans is in it.
If the room holds 20 per cent of the goods and 20 per cent of the bads, it is an average room. Being in it tells you nothing.
If it holds 30 per cent of the goods and 10 per cent of the bads, there are relatively more goods in it than bads. Being in it is evidence of a good loan.
If it holds 10 per cent of the goods and 30 per cent of the bads, being in it is evidence of a bad loan.
Weight of evidence puts a number on how strong that evidence is. It is the natural logarithm of the ratio of the two shares:
> WoE of a bin = ln( share of all goods in the bin ÷ share of all bads in the bin )
Take care with the sign, because books and software differ. In this course a positive WoE means the bin is safer than average (more goods than bads), and a negative WoE means the bin is riskier than average. Some references define it the other way round, as the log of bads over goods. Check which convention a tool uses before comparing numbers.
Computing it for the utilisation bins
Using the five coarse bins of Lesson 5.1, with 9,000 good and 1,000 bad loans in total, the working table is as follows.
Bin · Goods · % of all goods · Bads · % of all bads · WoE0 to 20% · 1,910 · 21.22% · 90 · 9.00% · +0.858
20 to 40% · 1,870 · 20.78% · 130 · 13.00% · +0.469
40 to 60% · 1,825 · 20.28% · 175 · 17.50% · +0.147
60 to 80% · 1,750 · 19.44% · 250 · 25.00% · −0.251
80% and above · 1,645 · 18.28% · 355 · 35.50% · −0.664
Total · 9,000 · 100% · 1,000 · 100% ·
Here is the calculation for the first bin, step by step.
1. Goods in the bin: 2,000 loans − 90 bad = 1,910.
2. Share of all goods: 1,910 ÷ 9,000 = 0.2122, or 21.22 per cent.
3. Share of all bads: 90 ÷ 1,000 = 0.0900, or 9.00 per cent.
4. Ratio: 0.2122 ÷ 0.0900 = 2.358.
5. Natural logarithm: ln(2.358) = 0.858.
For the last bin: goods 1,645, share 18.28 per cent; bads 355, share 35.50 per cent; ratio 0.1828 ÷ 0.3550 = 0.515; ln(0.515) = −0.664.
The pattern matches the bad rates but the scale is different. The first two bins differ in bad rate by 2 points (4.50 against 6.50 per cent), and in WoE by 0.39. The last two differ by 5.25 points in bad rate and 0.41 in WoE. In log-odds terms the steps are close to even, while in bad-rate terms the later steps look much bigger. That evenness is why WoE suits a model whose form is a straight line in log odds.
Predict first: what is the WoE of a bin whose bad rate is exactly equal to the overall bad rate of 10 per cent? Decide before reading on.
It is zero. Such a bin holds the same share of the goods as of the bads, so the ratio is one, and ln(1) = 0. A WoE of zero means no information, and it is the dividing line between the safe and the risky bins.
WoE is log odds in disguise
There is a second way to read the same number, and it makes the meaning clearer. The odds of a bin are the number of goods per bad. The overall odds are 9,000 ÷ 1,000 = 9 goods per bad. For the last bin, odds are 1,645 ÷ 355 = 4.63 goods per bad. WoE is the log of the bin's odds divided by the overall odds:
> WoE = ln( bin odds ÷ overall odds ) = ln(4.63 ÷ 9) = ln(0.515) = −0.664
It is the same number, because the two ways of writing it are algebraically identical. This second form is the useful one in practice, since it needs only the bin's bad rate and the overall bad rate:
> WoE = ln( (1 − p) ÷ p ) − ln( (1 − P) ÷ P ), where p is the bin's bad rate and P is the overall bad rate.
The first part is the log odds of the bin, the second the log odds of the whole sample. WoE is how far above or below the average a bin's log odds lie. Use the simulator to explore the shape of this relationship, and to see a bin's contribution to the information value that Lesson 5.3 introduces.
Set the bin's bad rate equal to the overall rate and the WoE is zero. Set it to half the overall rate and the WoE is about +0.75. Set it to double and the WoE is about −0.8. The curve is steepest at low bad rates, where a small change in the rate is a large change in the odds, and flat at high bad rates.
Why scorecards use it
There are four good reasons to replace a bin by its WoE before modelling.
1. It fits a logistic regression exactly. The standard scorecard model is a logistic regression, which predicts the log odds of a good outcome as a sum of terms. WoE is already a log odds measure, so using it as the input puts every variable on the same additive scale. In the simplest case, if a single variable is used, a coefficient of 1 on its WoE reproduces the bin's own odds. When several variables are used, each coefficient scales its variable's evidence up or down. Module 6 builds the model from this idea.
2. It orders the bins. A single number per bin, running from positive to negative, carries both the direction and the strength of the evidence. A variable with a clean, steady fall in WoE across its bins is one a model can use with confidence.
3. It treats missing values and categories like any other bin. A missing-value bin, a category such as "rented", and a range such as "60 to 80 per cent" all become one number each. The regression does not need to know what the bins were.
4. It does not depend on how the sample was drawn. Because WoE compares the share of goods to the share of bads, it does not change if you keep all the bad loans and a random tenth of the good ones, as lenders often do when bads are rare. The share of goods in a bin is the same in the full and the reduced sample, apart from random noise. The raw bad rate, by contrast, would jump. This property makes WoE convenient when the development sample was built by sampling.
A second example: categories with the same evidence
Now take home ownership, a categorical variable with three values, on the same 10,000 loans.
Category · Loans · Goods · Bads · Bad rateOwned · 4,000 · 3,720 · 280 · 7.0%
Rented · 4,000 · 3,520 · 480 · 12.0%
Living with family · 2,000 · 1,760 · 240 · 12.0%
Compute WoE for each:
Owned: share of goods 3,720 ÷ 9,000 = 0.4133, share of bads 280 ÷ 1,000 = 0.2800, ratio 1.476, ln = +0.390.
Rented: share of goods 3,520 ÷ 9,000 = 0.3911, share of bads 0.4800, ratio 0.815, ln = −0.205.
Living with family: share of goods 1,760 ÷ 9,000 = 0.1956, share of bads 0.2400, ratio 0.815, ln = −0.205.
Renters and people living with family have identical WoE, because they have the same bad rate. They carry the same evidence, so keeping them as two bins adds a parameter and no information. Merge them into "Rented or with family" with 6,000 loans, 720 bads and 12.0 per cent bad rate. Its WoE is still −0.205. Nothing is lost: the information value of the variable, which Lesson 5.3 defines, is the same before and after the merge. This is a general rule of WoE: bins with equal WoE can always be merged for free, and bins with nearly equal WoE can be merged at a small cost.
Bins with no bad loans
WoE has a weakness. If a bin contains no bad loans, the share of bads is zero, the ratio is infinite, and the logarithm cannot be computed. This is not a rare nuisance. It happens with small categories, with rare codes, and in samples with few bads.
Suppose a bin holds 300 loans and none is bad. The share of goods is 300 ÷ 9,000 = 0.0333. The share of bads is 0.
There are three common remedies.
1. Merge the bin with a neighbour until it has bads. This is the best fix, since a bin with no bads has failed the minimum-bads rule of Lesson 5.1 in any case.
2. Smooth the count by adding a small amount, such as 0.5, to the bad count (and the good count if needed). With 0.5 bads the share of bads is 0.5 ÷ 1,000 = 0.0005 and WoE = ln(0.0333 ÷ 0.0005) = ln(66.7) = +4.20.
3. Cap the WoE at a limit, for instance plus or minus 3, so that no single bin can dominate the model.
The smoothed value of 4.20 shows the danger. A bin with zero bads from only 300 loans is not strong evidence of anything, but the number looks very confident, and a regression would give it a lot of weight. Capping or merging is safer. A reviewer who sees a WoE above 3 or below −3 should ask how many bads sit behind it.
Missing values
The missing-value bin from Lesson 5.1 is handled in the same way. Of the 10,000 loans, 800 have no value for months at current employer, and 128 of them are bad. Goods: 672. Share of all goods: 672 ÷ 9,000 = 0.0747. Share of all bads: 128 ÷ 1,000 = 0.128. Ratio 0.583, ln = −0.539. The missing-value bin therefore has a clearly negative WoE, comparable to a high-utilisation bin. A scorecard will deduct points for a blank, as it should.
Building WoE in Python
With a table of bins, WoE takes a few lines. The key is to compute the shares from the totals.
python
import numpy as np
import pandas as pd
df = pd.readcsv("applications.csv") # columns: bin (already assigned), bad (1/0)
t = df.groupby("bin", observed=True)["bad"].agg(loans="count", bads="sum")
t["goods"] = t["loans"] - t["bads"]
smoothing: add 0.5 so that no bin has a zero count
t["distgood"] = (t["goods"] + 0.5) / (t["goods"].sum() + 0.5 len(t))
t["distbad"] = (t["bads"] + 0.5) / (t["bads"].sum() + 0.5 len(t))
t["woe"] = np.log(t["distgood"] / t["distbad"])
t["woe"] = t["woe"].clip(-3, 3) # cap extreme values
t["badrate"] = t["bads"] / t["loans"] 100
print(t.round(3))
apply to the data: replace each loan's bin by its WoE
df["utilisationwoe"] = df["bin"].map(t["woe"])
The smoothing adds a half to every bin so that the totals remain consistent. The last line is the step that turns the data into model input: the bin labels are gone, and each loan carries a number. Save the table t, because production code must apply exactly the same mapping to future applications.
A worked example: checking a variable before using it
An analyst receives the WoE table for a variable called number of credit enquiries in the last six months, with five bins: none, 1, 2 to 3, 4 to 5, and 6 or more. The WoE values are +0.42, +0.18, −0.10, −0.55 and −0.20. How should she read it?
1. Direction. More enquiries should mean more risk, so WoE should fall as enquiries rise. It does so up to the fourth bin: +0.42, +0.18, −0.10, −0.55.
2. The last bin breaks the order. Six or more enquiries has a WoE of −0.20, safer than the 4 to 5 bin. That is unexpected. She checks the size: the bin holds 1.2 per cent of loans and 38 bads. By the rule of Lesson 5.1 it is too thin, and the reversal is within the noise. She merges it with the 4 to 5 bin, giving a combined WoE of about −0.50.
3. Spacing. After the merge the WoE falls by 0.24, 0.28 and 0.40 between neighbours: evenly enough.
4. Business sense. Many recent enquiries signal a borrower hunting for credit, which supports a negative WoE.
5. Stability. She recomputes the table on last year's applications. The WoE values are within 0.05 of those above in every bin. The variable is stable.
Only after these steps would she pass the variable on to the selection stage of Lesson 5.3. The check is quick and it catches the reversals and the thin bins that otherwise cause trouble in the model.
Common mistakes
Mixing sign conventions. Know whether positive means safe or risky in your tool.
Computing shares from bin totals. The shares must be of all goods and all bads in the sample, not of the loans in the bin.
Taking the logarithm of zero. Merge, smooth or cap bins with no bads.
Trusting extreme WoE from small bins. A value above 3 in a bin with a handful of bads is noise.
Keeping bins with equal WoE separate. They carry identical evidence and can be merged.
Using the bad rate in the model instead of WoE. The raw rate is not on the log-odds scale and depends on the sampling.
Recomputing WoE on new data without freezing the old table. Production must use the development mapping.
Skipping the order, spacing and stability checks. They catch most binning errors.
Key takeaways
Weight of evidence is the natural log of the share of all goods in a bin divided by the share of all bads. Positive means safer than average, negative riskier, and zero no information.
Equivalently it is the log of the bin's odds divided by the sample's odds, so it can be computed from the two bad rates.
In the utilisation example it fell from +0.858 to −0.664 as utilisation rose, with fairly even steps on the log-odds scale.
Bins with equal WoE carry the same evidence and can be merged at no cost, as renters and people living with family showed.
WoE suits logistic regression, orders the bins, treats missing values and categories like any other bin, and does not change if goods are sampled down.
Bins with no bads need merging, smoothing or a cap, and extreme WoE from small bins should be distrusted.
Check yourself
1. A bin holds 300 of the 6,000 good loans and 60 of the 400 bad loans. What are its shares and its WoE?
Share of goods 300 ÷ 6,000 = 5 per cent. Share of bads 60 ÷ 400 = 15 per cent. Ratio 0.333, ln = −1.10. The bin is clearly risky.
2. A bin has the same bad rate as the whole sample. What is its WoE?
Zero, because its odds equal the overall odds and ln(1) = 0.
3. The overall bad rate is 8 per cent. A bin has a bad rate of 4 per cent. Using the odds form, what is the WoE?
The bin odds are 96 ÷ 4 = 24, and overall odds are 92 ÷ 8 = 11.5. The ratio is 2.087 and ln = +0.74.
4. Two bins have WoE of −0.31 and −0.30. What would you do and why?
Merge them. They carry almost identical evidence, so keeping both adds a parameter and no information.
5. A modeller keeps all 1,000 bad loans and a random 10 per cent of the good loans. Does WoE change, and does the bad rate?
WoE stays about the same because shares of goods and bads are unchanged. The bad rate changes a great deal, since the goods are reduced tenfold.
Try this
Compute weight of evidence for the bins you built in Lesson 5.1, or the table in this lesson. Set aside forty to forty-five minutes.
1. Take your coarse bins with loans and bads. Compute goods in each bin and the totals of goods and bads.
2. For each bin compute the share of all goods and the share of all bads. Check that each set of shares adds to 100 per cent.
3. Compute WoE for each bin and check that bins with a lower bad rate have a higher WoE. Chart the result.
4. Recompute WoE through the odds form, using only the bad rates, and check you get the same numbers. Use the simulator to confirm one bin.
5. Find any pair of bins whose WoE differ by less than about 0.1 and decide whether to merge them. Recompute the table after merging.
6. Create a bin with zero bads on purpose, compute WoE with smoothing at 0.5 and then with a cap at plus or minus 3. Write down how much the two differ.
7. Write a four-sentence note for a model reviewer: the direction of the WoE, the spacing, any bin you merged or capped and why, and the check you would run on a second period of data.
A good result is a WoE column that falls cleanly across the bins, a paragraph that reads it in plain words, and a record of every merge and cap. Lesson 5.3 uses the same shares to measure how much each variable is worth, so that you can choose which to keep.
Tags: Credit Risk Analytics Weight of Evidence WoE Scorecard Logistic Regression