~/teaching · machine learning · session 7, wednesday 16 december 2026
Explaining a Credit Score with SHAP, Auditing its Fairness, and Finding Structure with PCA, k-means and EM
Session 7 of the Machine Learning course: three questions a bank asks before a model decides.
Abstract
This half-day session of the Machine Learning course at Centrale Méditerranée (option DDEFI, 2026-2027) takes the champion model of a credit default project and puts three questions to it. Which inputs drove each individual decision? How are different groups of clients treated? And, setting the default column aside, what structure do the applicants show on their own?
The first part builds the Shapley value from cooperative game theory, computes it by hand on a three-feature example, and moves on to SHAP and TreeSHAP with their pitfalls. The second part measures fairness with rates computed group by group, proves why they cannot all be equal at once, and places credit scoring in European law. The third part leaves the labels aside: principal component analysis through the SVD, k-means, and Gaussian mixtures fitted by EM, each on a handful of numbers you can check with a pencil.
After reading, you should be able to compute a Shapley value from the values of every coalition, choose and justify a fairness measure, and run one iteration of PCA, k-means or EM without a library.
Plan of the deck
- Explaining a prediction. Shapley's 1953 sharing rule, its four axioms, SHAP, TreeSHAP, local and global readings, and three pitfalls.
- SHAP on the champion model. A short lab: an explainer with a fixed background, an additivity check, a beeswarm and two commented waterfalls.
- Fairness. Proxy variables, three families of measures, a worked audit of two groups, the impossibility result and the law.
- Principal component analysis. Rotating the axes so that variance sorts itself, derived as an eigenvector problem and computed with the SVD.
- k-means. Lloyd's assign and re-centre steps, choosing k, and why the starting centres matter.
- Gaussian mixtures and EM. Soft groups, responsibilities from Bayes' formula, and a likelihood that never goes down.
- Back to the credit score. Segments, anomalies, and a three-card summary of the whole session.
Shapley values: sharing a prediction among features
A Shapley value splits the gap between one client's prediction and the average prediction fairly among the features, by averaging what each one adds.
A model gives a client a 62 % default probability while its average prediction is 22 %. Forty points need an owner. Lloyd Shapley answered the same question for players in 1953: if a coalition wins together, how should the gain be shared? Here the players are features, and the value of a coalition is the prediction when only the features in are known and the others are averaged out.
The cleanest definition uses arrival orders. Bring the features in one at a time, credit each with the change it causes when it arrives, then average over all orders. Averaging matters because a feature's contribution depends on who is already there: interaction is shared, not given to whoever arrives last. Grouping the orders by the set of features already present gives the classical formula, a weighted average of marginal contributions:
The weight counts the orders in which exactly precedes , namely ways to arrange before, ways to arrange the rest after, out of . For three features the weights are , , and , and they sum to one.
Worked example: three features, one client
The deck uses illustrative values for the credit features PAY_0 (latest repayment delay), LIMIT_BAL (credit limit) and AGE. In percent, , , , , , , and . Each row of the table gives what each feature adds in one arrival order.
| Order | PAY_0 | LIMIT_BAL | AGE |
|---|---|---|---|
| P, L, A | 30 | 6 | 4 |
| P, A, L | 30 | 8 | 2 |
| L, P, A | 30 | 6 | 4 |
| L, A, P | 32 | 6 | 2 |
| A, P, L | 32 | 8 | 0 |
| A, L, P | 32 | 8 | 0 |
| Mean | 31 | 7 | 2 |
PAY_0 receives 31 points, LIMIT_BAL 7 and AGE 2, and . AGE is the instructive one: alone it adds nothing, next to the other two it adds 4, and its share is the average over contexts. The same 31 comes out of the coalition formula: .
Remark (Why this rule and no other). Shapley proved it is the only sharing rule with four properties: efficiency (nothing of the gap is left unassigned, and nothing is added), symmetry (interchangeable players get equal shares), null player (a feature that never changes anything gets zero, such as a coefficient zeroed by the lasso) and additivity (shares of a sum of games add up). Additivity is what lets the explanation of a forest be the average of the explanations of its trees.
SHAP and TreeSHAP in practice, and what they do not say
SHAP turns every prediction into the average plus one share per feature; it is exact and fast for trees, but it describes the model, not the world.
Lundberg and Lee (NeurIPS 2017) made the translation explicit: players are features, is the expected prediction given the known features, and every prediction decomposes as the average output plus one share per feature. Their paper also places older attribution techniques, LIME included, inside this one additive family.
The obstacle is cost. The 23 features of the UCI credit card dataset give coalitions for a single client. KernelSHAP samples coalitions and works on any model, approximately and slowly. TreeSHAP (Lundberg et al., Nature Machine Intelligence, 2020) walks the tree paths and computes the exact values for trees, forests and boosting in polynomial time. On a toy tree of the deck, LIMIT_BAL receives 4.2 points although the client's path never tests it: also reasons about clients who would have gone the other way, so a waterfall is not the path the model followed.
Locally, the waterfall shows one client going from the average to their prediction. Globally, the mean absolute share ranks features; the absolute value stops pushes up and down from cancelling, at the price of losing the direction, which the beeswarm plot gives back with one dot per client coloured by the feature's value. Check the scale: for boosting, exact additivity holds on the log-odds margin, not on probabilities.
- Correlated features. The six monthly bill amounts are near twins; how their credit is split depends on the definition of (interventional or conditional), not on the world. Group them and read the sum.
- Explaining is not causing. A high credit limit lowers the predicted risk because banks grant high limits to clients already judged safe. Raising a fragile client's limit is a different, causal question.
- After-the-fact stories. Explanations can be fooled (Slack et al., 2020) or cherry-picked across settings. Fix the explainer, background sample and scale before looking, cross-check with permutation importance, and for high stakes prefer a model that is readable by construction (Rudin, 2019).
Fairness measures: what the score does to groups
Removing the sensitive column does not make a score fair; you have to measure, group by group, how often it rejects, how often it is wrong, and whether its probabilities hold.
Dropping SEX from the inputs, called fairness through unawareness, was shown insufficient by Dwork and co-authors in 2012: other columns can carry the same information. These are proxy variables. The test turns the problem around: predict SEX from all the other columns, with the same model family as the champion and cross-validation on the training set. An AUC near 0.5 means little leaks through; further away, the permutation importance of that model names the carriers.
Barocas, Hardt and Narayanan (2023) sort the many measures into three families. With the course's coding, positive means default and means rejected, so a false positive is a good client turned down.
| Family | Condition | Usual measure | Question it answers |
|---|---|---|---|
| Independence | Demographic parity | Does it reject as often in each group? | |
| Separation | Equal opportunity, equalised odds | Does it make the same errors? | |
| Sufficiency | Calibration by group | Does a 30 % score mean 30 % for everyone? |
Demographic parity is readable without knowing who defaults, so it suits monitoring from day one, but it ignores differing base rates. Equal opportunity (Hardt, Price and Srebro, 2016) asks for the same share of good clients rejected, the false positive rate; equalised odds also equalises the true positive rate. Both need the outcomes, so they are audit measures. Calibration by group is what banks often check first, because a default probability also feeds provisions and capital.
Worked example: two groups of 1,000 applications
Same model, same threshold. Group A: 150 true positives, 100 false negatives, 75 false positives, 675 true negatives. Group B: 120, 80, 60 and 740. The five rates follow.
| Rate | Formula | Group A | Group B |
|---|---|---|---|
| Base rate | 25.0 % | 20.0 % | |
| Rejection rate | 22.5 % | 18.0 % | |
| TPR | 60.0 % | 60.0 % | |
| FPR | 10.0 % | 7.5 % | |
| PPV | 66.7 % | 66.7 % |
So the score is fair in the sense of TPR and PPV, and unfair in the sense of rejections and FPR: a good client of group A is wrongly rejected one time in ten, against one in about thirteen in group B. Both readings are correct; they answer different questions. That is why the measure must be chosen, and written down, before the numbers are seen, and why every gap needs a confidence interval: with small groups the honest verdict is often inconclusive.
Why you cannot equalise every rate at once
When two groups default at different rates, an imperfect score cannot give them the same precision, the same detection rate and the same false alarm rate together.
The example above is not rigged. Chouldechova (2017) wrote an identity that follows from the definitions alone, with the base rate of the group: the number of defaults caught is , the false positives follow from the PPV, and dividing by the good clients gives the FPR. The group size cancels.
With equal PPV and TPR, the FPR grows with , which is strictly increasing. Check on the two groups: for A, , and for B, . Exactly the rates of the table.
Kleinberg, Mullainathan and Raghavan proved the score version in 2016: calibration within groups and balance for both classes are incompatible unless prediction is perfect or base rates are equal. Both results came out of the debate on the COMPAS recidivism tool in the United States. The practical consequence is a procedure, not a number: measure by group with intervals, choose the measure that matters for the use and justify it, then correct at the source (a proxy that can be removed, a badly defined target, a badly placed threshold).
Remark (A threshold per group). Hardt, Price and Srebro showed that equal opportunity can be reached after training by giving each group its own threshold. It works on the rates, but two clients with the same score then get different decisions, and the group must be used at decision time. In access to financial services, deciding on the basis of sex is direct discrimination under Directive 2004/113/EC, so this is a question for lawyers, which the session turns into a debate.
Credit scoring under the EU AI Act
European law classifies creditworthiness scoring of individuals as high risk, which brings duties to examine data for bias, document the system and explain decisions.
Regulation (EU) 2024/1689, Annex III, point 5(b), lists systems that evaluate the creditworthiness of natural persons or establish their credit score as high risk, fraud detection excepted. According to the deck, Regulation (EU) 2026/1744 moved the application of the Annex III rules to 2 December 2027; such dates have already moved once, so the consolidated text should be reread before quoting one.
- Articles 9 to 15: risk management, data governance, technical documentation (Annex IV), automatic logging, transparency to the deployer, human oversight, and accuracy, robustness and cybersecurity.
- Article 10(2)(f): examine the data for biases likely to lead to prohibited discrimination. The proxy test and the group audit are this examination.
- Article 86: a person affected by a decision based on a high-risk system can obtain a clear explanation of the system's role. The SCHUFA judgment of the Court of Justice (C-634/21, 2023) had already brought determinative credit scores under Article 22 of the GDPR.
- Directive 2004/113/EC: indirect discrimination is an apparently neutral criterion that puts one sex at a particular disadvantage without objective justification. The effect counts, not the intention, which is why the audit measures gaps even without SEX in the model.
The deck maps each of these articles onto files a student project already produces: a decision log, a model card in the format of Mitchell et al. (2019), service logs and a reject option that come later in the course (see drift and conformal prediction). For banking supervisors, the European Banking Authority report EBA/REP/2023/28 on machine learning in internal rating models found Shapley values to be the explanation method banks mention most often.
Principal component analysis through the SVD
PCA turns the axes so that the first direction carries as much of the spread of the data as possible, then the next perpendicular one, and so on.
Without a target, the only material is . Principal component analysis, Pearson's line of closest fit (1901) and Hotelling's direction of maximal variance (1933), looks for the viewpoint from which the cloud looks widest. The variance of the projections on a unit vector is , with the covariance of the centred columns. Maximising it under with a Lagrange multiplier gives an eigenvector equation, and the variance carried is the eigenvalue.
In practice the SVD of the least squares session does the same work without forming . With , the columns of are the principal directions, the coordinates are , and . Eckart and Young (1936) add why truncation is legitimate: keeping the first components is the best rank- approximation, and the error is the sum of the discarded .
Five points in two dimensions
- Centre. The points (1, 1), (2, 3), (3, 2), (4, 4) and (5, 5) have mean (3, 3); three lie on the diagonal, two sit just off it.
- Covariance. The centred sums of squares are 10 and 10, the cross sum is 9; dividing by gives variances 2.5 and covariance 2.25.
- Eigenvalues. Trace 5, determinant , so and , with , the diagonal.
- Read the share. The first component carries of the variance; the SVD gives , the same ratio.
- Check the loss. Projecting on the diagonal moves the two off-axis points by a squared distance of 0.5 each: total error 1, the discarded .
Remark (The scale pitfall). On raw credit data, amounts in Taiwan dollars dwarf ages in years, and the first components simply rediscover the currency columns. Standardise with training means and standard deviations, and PCA works on correlations. PCA discovers nothing by itself; it rotates the axes so that variance comes out in decreasing order.
k-means and Gaussian mixtures fitted by EM
k-means puts each client in exactly one group around a centre; a Gaussian mixture gives each client a probability of belonging to each group, and EM fits it.
k-means minimises the inertia, the sum of squared distances from each point to its centre. Lloyd's algorithm (written in 1957, published in 1982) alternates two steps, each of which can only lower the inertia: assign each point to the nearest centre, then move each centre to the mean of its group, the minimiser of squared deviations as in least squares. It stops, but at a local minimum that depends on the start.
On the six values 1, 2, 4, 7, 8, 9 started from centres 1 and 4, the inertia falls from 51 to 14.5 after the first iteration (centres 1.5 and 7), then to about 6.67 when the point 4 changes side (centres 7/3 and 8); nothing moves after that. With three groups, a poor start at 7, 8, 9 stalls at 5.17 while the optimum is 2.5, which is why k-means++ (Arthur and Vassilvitskii, 2007) draws spread-out starting centres and why several restarts are kept. The elbow of the inertia curve and Rousseeuw's silhouette help choose .
A Gaussian mixture assumes bell curves with weights , centres and variances . The log-likelihood has a sum inside the logarithm and no closed form. EM (Dempster, Laird and Rubin, 1977) alternates an E step, which computes each client's responsibility for each group by Bayes' formula, and an M step, which recomputes weights, means and variances as averages weighted by those responsibilities.
On the four values 1, 2, 5 and 8, starting from bells at 2 and 8 with unit variance and equal weights, the value 5 sits exactly halfway and gets a responsibility of one half for each bell, while the others belong almost entirely to their nearest one. After one M step the second centre is pulled from 8 towards 5, both bells widen, and the log-likelihood rises from about to , as the 1977 theorem guarantees. Forcing responsibilities to 0 or 1 with equal fixed variances gives back k-means.
Key takeaways
- A Shapley value is a weighted average of what a feature adds across all coalitions; the shares add up exactly to the gap between the prediction and the average.
- TreeSHAP makes the computation exact for trees, forests and boosting, but SHAP describes the model, not a causal effect in the world.
- Removing a sensitive column is not enough: test whether the other columns predict it, then measure rates group by group with intervals.
- With different base rates, an imperfect score cannot equalise PPV, TPR and FPR together, so the fairness measure is a choice to write down before computing.
- Credit scoring of individuals is a high-risk use under the EU AI Act, with duties on data bias, documentation, oversight and explanation.
- PCA is the SVD of the centred, usually standardised, columns; the eigenvalues give the variance each direction carries.
- k-means and EM share one scheme, alternating two steps that never make the objective worse, and both need several starts.
Glossary
- Shapley value
- The share of a coalition's gain given to one player, averaged over all arrival orders; in machine learning, a feature's share of one prediction.
- Marginal contribution
- What a feature adds to the prediction when it joins a given set of features already known.
- Proxy variable
- A column correlated with a sensitive attribute, through which a model can recover it even when the attribute is removed.
- Demographic parity
- The requirement that the rejection rate be the same in every group.
- Equal opportunity
- The requirement that qualified people, here good clients, have the same chance of the favourable decision in every group.
- Base rate
- The share of actual positives, here defaults, in a group; a fact of the data, not a choice of the model.
- Calibration by group
- The requirement that a given score corresponds to the same observed default rate in every group.
- Principal component analysis
- A change of orthonormal basis that orders directions by the variance they carry, computed from the covariance or the SVD.
- k-means clustering
- Partitioning points into k groups that minimise the sum of squared distances to their centres.
- Inertia
- The k-means objective: the total squared distance of each point to its group centre.
- Gaussian mixture model
- A density written as a weighted sum of Gaussian bells, giving each point a probability of belonging to each component.
- Responsibility
- In EM, the posterior probability that a point comes from a given component, computed with Bayes' formula.
The other notes of the course
One public summary per deck, in the order of the course.
Previous: Forests, boosting, honest comparisonNext: A documented champion model
- IntroductionAI winters, jobs and method
- ToolsKaggle, GitHub, Python: the tools
- Session 1Project framing and least squares
- Session 2Maximum likelihood and logistic regression
- Session 3Gradient descent, ridge and lasso
- Session 4Decision threshold and reproducible ML
- HomeworkA defensible baseline model
- Session 5Kernels, SVM and decision trees
- Session 6Forests, boosting, honest comparison
- Session 7SHAP, fairness, PCA and clustering
- HomeworkA documented champion model
- Session 8Neural networks, drift, conformal prediction
- HomeworkA delivered machine learning project
- Session 9Defending a machine learning project
- ReferenceCredit default scoring project
- ReferenceAsset returns: XGBoost and sentiment
- ReferencePython code for every ML model
- ReferenceDeep learning essentials
- ReferenceOpen data ML projects by sector
- ReferenceData and AI engineer handbook
- ReferenceData engineering, SRE and cloud
- ReferencePanorama of machine learning models
About these notes
Written by the lecturer from the slides of the deck, as a public summary: the key ideas, the formulas and one worked example. The exercises, their solutions, the tests and the grading stay in the deck, for the students of the course.
Revision: .