Skip to content
mentoor

Topic 2 of 5 · 5 min read

Probability distributions and expected value

Goal: Compute a conditional probability with Bayes' rule and turn a set of logits into softmax probabilities.

A probability distribution assigns a number between 0 and 1 to every possible outcome, the numbers add up to 1, and a model's output is usually exactly such a distribution. A language model's next-token answer is a distribution over its whole vocabulary.

Check yourself · 1 of 3

A fair six-sided die is rolled. What is the expected value of the roll?

Choose one answer

Why it matters

You will read model outputs as probabilities, judge classifiers by how often a positive result is right, and see the word logits in every library. Mistakes here are expensive: a screening test with 90% sensitivity does not mean a positive result is 90% likely to be right.

How to do it

  1. Expected value is the probability-weighted average. A fair die gives (1+2+3+4+5+6)/6 = 3.5, a value you never actually roll.
  2. Conditional probability. P(A | B) is the chance of A once you know B happened. Restrict attention to the cases where B is true.
  3. Bayes' rule flips a conditional: P(A | B) = P(B | A) * P(A) / P(B). Take a condition with a 1% base rate, a test that catches 90% of true cases and falsely flags 5% of healthy people. Per 10,000 people, 100 are ill and 90 test positive. Of the 9,900 healthy, 495 also test positive. A positive result is right 90 out of 585 times, about 15.4%.
  4. Softmax turns raw scores (logits) into a distribution: exponentiate each score and divide by the total. Logits [2, 1, 0] give e^2 = 7.39, e^1 = 2.72, e^0 = 1, total 11.11, so probabilities 0.665, 0.245 and 0.090. They sum to 1, and adding the same constant to every logit changes nothing.

Before and after

Weak: "The model gave class A a score of 2, so it is 2 times as likely as class B at 1." Scores are not probabilities.

Fixed: apply softmax first. A at 2 and B at 1 give 0.665 versus 0.245 in the example above, about 2.7 times as likely, because softmax uses the exponential of the score.

What goes wrong

  • Ignoring the base rate. Rare events stay rare even after a decent test result; this is the most common probability error in practice.
  • Treating softmax output as certainty. A 0.99 can come from a confidently wrong model. It is a ranking of options, not a guarantee.
  • Mixing up P(A | B) and P(B | A). "Most fraud uses a new device" is not "most new devices mean fraud".

Examples

Softmax in a few lines

import numpy as np
logits = np.array([2.0, 1.0, 0.0])
exp = np.exp(logits - logits.max())   # subtract max for numerical safety
probs = exp / exp.sum()
print(probs.round(3))                 # [0.665 0.245 0.09 ]

Subtracting the maximum does not change the result, because softmax ignores a constant shift, but it avoids overflow for large logits.

Base rate in a spam filter

Suppose 2% of mail is phishing, the filter flags 95% of phishing and 3% of normal mail. Per 10,000 messages: 200 phishing, 190 caught; 9,800 normal, 294 wrongly flagged. A flagged message is phishing 190 times in 484, about 39%. Most flags are false alarms, which is why the filter's flags should go to review rather than deletion.

Key points

  • A distribution gives every outcome a probability between 0 and 1, and the probabilities sum to 1.
  • Expected value is the probability-weighted average of the outcomes and need not be a value that can occur.
  • Bayes' rule, P(A | B) = P(B | A) * P(A) / P(B), updates a belief using the base rate and the evidence.
  • Softmax exponentiates the logits and divides by their sum, giving probabilities that sum to 1.
  • A high softmax probability is not proof that the model is right; models can be confidently wrong.

Common mistakes

  • Reading a test's detection rate as the chance that a positive result is correct.
  • Reading raw logits as probabilities or as ratios of probability.
  • Treating a probability of 0.99 from a model as measured confidence.
  • Forgetting that softmax outputs only compare the listed options, so a missing class cannot get any probability.

Check yourself · 2 of 3

A classifier outputs logits [0, 0, 0] for three classes. What does softmax return?

Choose one answer

Check yourself · 3 of 3

A test catches 90% of true cases but 5% of healthy people also test positive. The condition affects 1% of people. Roughly how likely is a positive result to be correct?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic