Probability, loss, and a gradient step
How to study this lesson
Read one section, trace the worked example, and try the knowledge check. Bookmark saves a shortcut; notes appear in My notebook. Mark lesson complete is your own assessment and does not mark its lab passed.
By the end, you can
- Compute a posterior from a base rate and conditional rates.
- Relate log loss to probability quality.
- Trace a gradient update and its learning-rate tradeoff.
A score becomes a probability through a claim
A model score of 0.8 is a probability only when the system intends it to describe a chance under a specified event and population. A similarity score, a ranking score, and an estimated success probability are different quantities. Treating them interchangeably leads to thresholds that work on one dataset and fail on another.
A probability also has a conditioning context. The chance that a retrieved passage is useful given a query is not the same as the chance that an agent's final answer is correct. Several passages can be individually relevant while the final synthesis misuses them. Write the event in words before choosing a probability model or a calibration test. This forces you to name what a numeric confidence claim actually means.
Base rates matter even when detection looks strong
Consider 1,000 incoming requests, of which 100 truly require escalation. A detector catches 90 percent of those requests, producing 90 true positives. It also flags 10 percent of the remaining 900, producing 90 false positives. Among the 180 flagged requests, only 90 need escalation. The probability of a real escalation given a flag is therefore 0.5, despite the detector's 90 percent sensitivity.
This is Bayes' rule expressed as counts. In symbolic form, posterior equals likelihood times prior divided by the total probability of the observation. Counts often make the denominator easier to understand. If the base rate changes while conditional detection rates stay fixed, precision changes too. A threshold validated on one workflow may therefore need re-evaluation after a product launch changes the request mix.
Log loss punishes confident mistakes
For a binary outcome y and predicted probability p, log loss is minus y log p minus (1-y) log(1-p). Predicting p = 0.9 for a positive example gives a loss near 0.105. Predicting p = 0.1 for that same example gives a loss near 2.303. The second prediction is not merely incorrect at a threshold; it assigned little probability to the observed event.
Log loss evaluates the probabilities before choosing an action threshold. Accuracy only evaluates a thresholded decision. A model can preserve accuracy while improving or worsening probability estimates. Numerical implementations usually clip probabilities away from zero and one or compute loss directly from logits using a stable formula. The mathematical loss is infinite for a confidently impossible event that actually occurs.
Optimization follows local sensitivity
For a scalar prediction w and target 3, define loss L = (w-3) squared. Its derivative is 2(w-3). Starting at w = 0 gives derivative -6. A gradient descent step with learning rate 0.1 subtracts 0.1 times -6, moving w to 0.6 and reducing loss from 9 to 5.76. The derivative tells us which local direction increases loss; subtracting it moves in the opposite direction.
A larger step is not automatically better. With learning rate 1.1, the update becomes 6.6, and the new loss is 12.96. The direction was locally sensible but the step overshot. In many dimensions, gradients supply one sensitivity per parameter. Feature scale, curvature, stochastic batches, and optimizer state all influence what constitutes a useful step size.
Keep uncertainty and optimization separate
A low training loss proves that an optimization procedure fitted its training objective, not that the resulting system generalizes. A probability estimate can also be systematically overconfident even when its class ranking is useful. Evaluate loss and calibration on held-out data, and inspect important slices rather than averaging away a failure that affects one kind of request.
The code computes an escalation posterior from rates and follows three deterministic gradient updates. Its tiny objective is convex and fully visible, unlike a modern network's loss landscape. That simplicity is useful: you can verify every arithmetic operation and distinguish an implementation error from a modeling limitation. When moving to a neural network, preserve the same discipline of checking the objective, the gradient, and the behavior on unseen examples.
Work through the code
The first calculation uses a 10 percent prior, 90 percent sensitivity, and 10 percent false-positive rate. The next compares probability penalties. Three scalar updates demonstrate local optimization. Change only the learning rate first so the effect of step size remains interpretable.
import math
def posterior(prior, sensitivity, false_positive_rate):
positive = prior * sensitivity
total = positive + (1 - prior) * false_positive_rate
if total == 0:
raise ValueError("conditioning event has zero probability")
return positive / total
def log_loss(y, p):
p = min(max(p, 1e-12), 1 - 1e-12)
return -(y * math.log(p) + (1 - y) * math.log(1 - p))
print(f"posterior: {posterior(0.1, 0.9, 0.1):.3f}")
print(f"positive losses: {log_loss(1, 0.9):.3f}, {log_loss(1, 0.1):.3f}")
w = 0.0
for step in range(1, 4):
gradient = 2 * (w - 3)
w -= 0.1 * gradient
print(f"step {step}: w={w:.3f} loss={(w - 3) ** 2:.3f}")
posterior: 0.500 positive losses: 0.105, 2.303 step 1: w=0.600 loss=5.760 step 2: w=1.080 loss=3.686 step 3: w=1.464 loss=2.359
Run Python snippets locally with the prerequisites named above. The module coding lab runs directly in your browser.
Pause and reason
The escalation prevalence falls from 10 percent to 1 percent while sensitivity and false-positive rate stay at 90 and 10 percent. Compute precision among flagged requests and explain the operational effect.
Check your understanding
A training loss rises after a gradient step, although the derivative is correct. Which explanation is plausible?
Your notes
Explain the mechanism in your own words. Add a failure you want to test.