Compare Two Candidate Distributions
Score two candidate probability vectors against the same target distribution and see how rare-class underprediction flips the winner.
The problem
Suppose a three-class outcome follows the true distribution q = [0.50, 0.30, 0.20]. Two models propose candidate distributions for :
- Candidate A:
pa0 = [0.65, 0.25, 0.10] - Candidate B:
pb = [0.45, 0.35, 0.20]
Both candidates agree that class 0 is the most likely outcome. Candidate A places much higher probability (0.65) on that dominant class 0 than Candidate B (0.45), but it achieves that confidence by cutting the probability of class 2 in half (0.10 versus the true 0.20). Which candidate achieves lower cross-entropy ?
Work through the numbers
Compute the three weighted terms for both candidates at the default setting blend equal to 0.0.
For Candidate A (pa = [0.65, 0.25, 0.10]):
- Class 0:
- Class 1:
- Class 2:
- Total nats
For Candidate B (pb = [0.45, 0.35, 0.20]):
- Class 0:
- Class 1:
- Class 2:
- Total nats
Candidate A wins on class 0 by nats, but loses on class 1 (0.10094) and class 2 (0.13863) by a combined 0.23957 nats. Overall, nats: Candidate B wins.
The live model
For each class, the top red bar shows Candidate A's term and the bottom blue bar shows Candidate B's term . The blend slider interpolates Candidate A toward Candidate B via pa = pa0 * (1.0 - blend) + pb * blend.
At blend equal to 0.0, is 1.092 and is 1.036, giving a positive gap diff = 0.056. At blend equal to 0.50, pa becomes [0.55, 0.30, 0.15], cutting to 1.040 (diff = 0.003). At blend equal to 0.75, pa reaches [0.50, 0.325, 0.175] and achieves (diff = -0.004), slightly beating Candidate B because matches .
![Candidate A [0.65, 0.25, 0.10] has cross-entropy 1.092 against q=[0.50, 0.30, 0.20], losing to Candidate B [0.45, 0.35, 0.20] at 1.036 by 0.056.](../assets/ml/compare-two-candidate-distributions--demo.png)
let blend = scene.parameter("blend", 0.0, 0.0, 1.0, 0.25);
let q = input.select(0, 0);
let pa0 = input.select(0, 1);
let pb = input.select(0, 2);
let pa = pa0 * (1.0 - blend) + pb * blend;
let terms_a = q * pa.log().neg();
let terms_b = q * pb.log().neg();
let la = terms_a.sum().reshape(1);
let lb = terms_b.sum().reshape(1);
let diff = la - lb;
let answer = torch.cat(torch.list().add(la).add(lb).add(diff), 0);
out.set("pa", pa).set("la", la).set("lb", lb).set("diff", diff);
{
"pa": [
0.65,
0.25,
0.1
],
"la": [
1.0917967849810033
],
"lb": [
1.0360880679453093
],
"diff": [
0.05570871703569402
]
}Your turn
At blend = 0.0, and , so Candidate A has higher cross-entropy by +0.05571; the starter reports diff = -0.05571 with the comparison sign reversed. Fix diff so positive numbers mean Candidate A has higher loss than Candidate B.
![Two candidates compared under q=[0.50, 0.30, 0.20]; the starter reverses diff as lb - la.](../assets/ml/compare-two-candidate-distributions--exercise.png)
let blend = scene.parameter("blend", 0.0, 0.0, 1.0, 0.25);
let q = input.select(0, 0);
let pa0 = input.select(0, 1);
let pb = input.select(0, 2);
let pa = pa0 * (1.0 - blend) + pb * blend;
let terms_a = q * pa.log().neg();
let terms_b = q * pb.log().neg();
let la = terms_a.sum().reshape(1);
let lb = terms_b.sum().reshape(1);
let diff = lb - la;
let answer = torch.cat(torch.list().add(la).add(lb).add(diff), 0);
out.set("pa", pa).set("la", la).set("lb", lb).set("diff", diff);
{
"pa": [
0.65,
0.25,
0.1
],
"la": [
1.0917967849810033
],
"lb": [
1.0360880679453093
],
"diff": [
-0.05570871703569402
]
}Summary diff must be positive whenever Candidate A has higher cross-entropy than Candidate B. Check tests unseen candidate pairs and blend settings.
With the signed difference restored, the default run returns [1.09179658, 1.03608831, 0.05570827]. The check reruns the comparison on q = [0.60, 0.25, 0.15] with candidates [0.40, 0.45, 0.15] and [0.55, 0.30, 0.15] at blend = 0.25, and on q = [0.40, 0.40, 0.20] with [0.70, 0.20, 0.10] and [0.45, 0.35, 0.20] at blend = 0.50.
Read the result carefully
Notice the algebraic form of the difference between two candidates under the same target :
Because is strictly convex, Jensen's inequality guarantees that a blend is no worse than the weighted average of the two losses: . Convexity does not guarantee beating the better candidate alone, though when the candidates err in opposite directions a blend can beat both (here 1.03235 at blend = 0.75 against 1.03609 at blend = 1.0, by 0.004).
Recap
- Comparing two candidates and under trades off their weighted log-ratios across all classes.
- Over-concentrating mass on the top class (
0.65) at the expense of a 20-percent class (0.10) costs more in rare-class log-penalty than it saves on the dominant class. - Convexity guarantees that a blend of two forecasts is no worse than the weighted average of their two losses, and can beat the better one (
1.03235vs1.03609) when their errors offset.