Speed

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 qq:

  • 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 H(q,p)H(q, p)?

Work through the numbers

Compute the three weighted terms −qklog⁡(pk)-q_k \log(p_k) for both candidates at the default setting blend equal to 0.0.

For Candidate A (pa = [0.65, 0.25, 0.10]):

  • Class 0: −0.50log⁡(0.65)=0.21539-0.50 \log(0.65) = 0.21539
  • Class 1: −0.30log⁡(0.25)=0.41589-0.30 \log(0.25) = 0.41589
  • Class 2: −0.20log⁡(0.10)=0.46052-0.20 \log(0.10) = 0.46052
  • Total H(q,pA)=0.21539+0.41589+0.46052=1.09180H(q, p_A) = 0.21539 + 0.41589 + 0.46052 = 1.09180 nats

For Candidate B (pb = [0.45, 0.35, 0.20]):

  • Class 0: −0.50log⁡(0.45)=0.39925-0.50 \log(0.45) = 0.39925
  • Class 1: −0.30log⁡(0.35)=0.31495-0.30 \log(0.35) = 0.31495
  • Class 2: −0.20log⁡(0.20)=0.32189-0.20 \log(0.20) = 0.32189
  • Total H(q,pB)=0.39925+0.31495+0.32189=1.03609H(q, p_B) = 0.39925 + 0.31495 + 0.32189 = 1.03609 nats

Candidate A wins on class 0 by 0.39925−0.21539=0.183860.39925 - 0.21539 = 0.18386 nats, but loses on class 1 (0.10094) and class 2 (0.13863) by a combined 0.23957 nats. Overall, H(q,pA)−H(q,pB)=1.09180−1.03609=+0.05571H(q, p_A) - H(q, p_B) = 1.09180 - 1.03609 = +0.05571 nats: Candidate B wins.

The live model

For each class, the top red bar shows Candidate A's term −qklog⁡(pA,k)-q_k \log(p_{A,k}) and the bottom blue bar shows Candidate B's term −qklog⁡(pB,k)-q_k \log(p_{B,k}). The blend slider interpolates Candidate A toward Candidate B via pa = pa0 * (1.0 - blend) + pb * blend.

At blend equal to 0.0, H(q,pA)H(q, p_A) is 1.092 and H(q,pB)H(q, p_B) 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 H(q,pA)H(q, p_A) to 1.040 (diff = 0.003). At blend equal to 0.75, pa reaches [0.50, 0.325, 0.175] and achieves H(q,pA)=1.032H(q, p_A) = 1.032 (diff = -0.004), slightly beating Candidate B because pA,0=0.50p_{A,0} = 0.50 matches q0=0.50q_0 = 0.50.

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.
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, H(q,pA)=1.09180H(q, p_A) = 1.09180 and H(q,pB)=1.03609H(q, p_B) = 1.03609, 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.
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 qq:

H(q,pA)−H(q,pB)=∑k=0K−1qklog⁡ ⁣(pB,kpA,k)H(q, p_A) - H(q, p_B) = \sum_{k=0}^{K-1} q_k \log\!\left(\frac{p_{B,k}}{p_{A,k}}\right)

Because −log⁡(p)-\log(p) is strictly convex, Jensen's inequality guarantees that a blend is no worse than the weighted average of the two losses: H(q,(1−α)pA+αpB)≤(1−α)H(q,pA)+αH(q,pB)H(q, (1-\alpha)p_A + \alpha p_B) \le (1-\alpha)H(q, p_A) + \alpha H(q, p_B). 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 pAp_A and pBp_B under qq trades off their weighted log-ratios qklog⁡(pB,k/pA,k)q_k \log(p_{B,k} / p_{A,k}) 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.03235 vs 1.03609) when their errors offset.