All lessons in Machine Learning Engineering: Reliable Predictive Models

Probability, Sampling, and Predictive Risk

Calculate conditional probabilities, sampling variability, and expected prediction loss.

30 minutes · Free lesson

What you will learn

  • Compute conditional event probabilities and distinguish a population quantity from a sample estimate.
  • Calculate expected loss under a simple decision rule.
  • Explain how sampling variation and population shift affect a reported model score.

A late-delivery model returns a number such as 0.3, but that number makes sense only in relation to a population and an event. A probability is not a percentage of certainty about one fixed parcel, nor a promise that every group of ten similar parcels contains exactly three late deliveries. It is a model's statement about how outcomes vary under the information and conditions represented by the prediction.

This lesson establishes the language used throughout the course: distributions, conditional probabilities, expected loss and empirical estimates. The calculations are deliberately small enough to check without a library. The same distinctions matter when the dataset contains millions of rows.

From a table to a conditional probability

Consider a completed set of 200 parcels. The table records whether the weather-risk flag was high at dispatch and whether each parcel was late. All labels are mature; no unresolved deliveries have been silently counted as on time.

Dispatch weatherLateOn timeTotal
High risk302050
Ordinary30120150
Total60140200

The empirical late rate is 60/200 = 0.30. The empirical rate conditional on high-risk weather is 30/50 = 0.60. Conditioning changes the denominator to the cases satisfying the condition. It does not change the direction of the condition: the fraction of late parcels that had high-risk weather is 30/60 = 0.50, a different question.

The table is evidence about this sample. It does not prove that future parcels with high-risk forecasts have exactly a 0.60 late probability. The forecasts, routes, season and measurement process may differ, and a finite sample has random variation even when those conditions are stable.

Worked example

Recovering a joint and marginal probability

  1. A randomly selected row from the table has high-risk weather with probability 50/200 = 0.25.
  2. Its late probability given high-risk weather is 30/50 = 0.60. Multiplying gives the joint probability 0.25 × 0.60 = 0.15, matching 30/200.
  3. Ordinary-weather rows have probability 150/200 = 0.75 and a conditional late rate of 30/150 = 0.20.
  4. Summing the two disjoint cases gives the overall late rate: 0.25 × 0.60 + 0.75 × 0.20 = 0.30.
  5. If next month's high-risk share becomes 0.50 while the two conditional rates remain unchanged, the marginal late rate becomes 0.50 × 0.60 + 0.50 × 0.20 = 0.40. A changed base rate need not mean either conditional rate changed.

Expected loss makes the objective explicit

A loss function assigns a cost to a prediction and outcome. For a binary action, suppose reviewing an on-time parcel costs one unit and failing to review a late parcel costs four. Assume correct decisions have zero cost for this teaching calculation. These assumptions do not model every cost of real dispatcher review.

For a parcel with late probability p, reviewing has expected loss 1 × (1 − p). Not reviewing has expected loss 4 × p. Comparing these quantities yields a decision boundary: review when 1 − p is less than 4p, or p is greater than 0.20. At equality the actions tie under this cost model. A practical implementation needs an explicit tie rule.

This is an expectation over possible outcomes. A decision with lower expected loss can still incur a higher realized cost on one parcel. The assessment concerns the distribution of repeated decisions, not hindsight on a single example.

Worked example

Expected cost and realized cost are different

  1. A model assigns a parcel late probability 0.30. Reviewing has expected cost 0.70; not reviewing has expected cost 4 × 0.30 = 1.20.
  2. Reviewing has the lower expected cost by 0.50 units under the model and stated costs.
  3. Suppose the parcel arrives on time. Review then incurs an actual cost of one unit, while not reviewing would have cost zero in this simplified accounting.
  4. That single outcome does not invalidate the original expected-cost comparison. It does motivate checking whether the model's 0.30 predictions are calibrated across many comparable cases.
  5. If review capacity is exhausted or costs vary by parcel, the independent threshold calculation is insufficient; the decision problem must include those constraints.

Population risk and empirical risk

Population risk is the expected loss of a prediction rule on the target distribution. Empirical risk is the average observed loss on a finite dataset. Training minimizes or approximates an empirical objective, often with a penalty, because the population distribution is unknown.

A training score is affected by fitting to those very rows. A validation score estimates performance on held-out cases, but repeated model selection uses that information too. A final score is interpretable only with its sampling design and the decisions made before it was read. The labels “train,” “validation” and “test” describe how information is used, not a magical property of three files.

The familiar squared-error bias–variance decomposition assumes a particular loss and repeated training samples from a defined population. It helps explain why flexible models can fit noise, but it is not a universal arithmetic decomposition of every classification metric. Keep the chosen loss visible whenever using the words bias and variance.

Sampling variation is not population shift

If outcomes are independent Bernoulli events with common probability p, the sample proportion over n rows has variance p × (1 − p)/n. With p = 0.30 and n = 200, the standard deviation is the square root of 0.21/200, approximately 0.0324. Substituting an observed rate for unknown p gives a rough plug-in standard error. Small samples, rare events and dependence may require different methods.

Rows from the same depot-day may share weather or disruptions. Treating them as independent can make uncertainty appear smaller than it is. Separately, a changed route mix or weather process can alter the population itself. More rows from the old population reduce some sampling noise without solving that shift.

A random seed fixes an algorithm's pseudo-random choices for reproducibility. It does not make a small sample representative, remove dependencies, or create new evidence each time the script is rerun. Later uncertainty and validation lessons address these boundaries in more depth.

Exercise

Using the 200-parcel table, compute the probability of high-risk weather given an on-time delivery, and explain why it differs from the probability of an on-time delivery given high-risk weather.

Check your answer

High risk given on time is 20/140 = 1/7, approximately 0.143. On time given high risk is 20/50 = 0.40. The numerator is the same joint count, but the conditioning population changes the denominator.

Exercise

A parcel has late probability 0.10. Under false-positive cost 1, false-negative cost 4 and zero cost for correct decisions, compute the expected loss of reviewing and not reviewing. State the preferred action if there is no capacity constraint.

Check your answer

Reviewing has expected loss 1 × 0.90 = 0.90. Not reviewing has expected loss 4 × 0.10 = 0.40. Not reviewing has lower expected loss under these assumptions. The comparison does not establish the causal benefit of review in a real operation.

Exercise

For independent Bernoulli outcomes with probability 0.30, compare the standard deviation of the observed late rate at 200 and 800 rows. Would quadrupling the number of strongly correlated depot-day records necessarily have the same effect?

Check your answer

At 200 rows the standard deviation is sqrt(0.21/200), approximately 0.0324. At 800 it is sqrt(0.21/800), approximately 0.0162: doubling precision in this sense requires four times as many independent rows. Correlated records do not satisfy that calculation's independence assumption, so the same reduction is not guaranteed.

Further reading

Summary

Conditional probabilities depend on their denominators. Expected loss connects probabilities to a stated decision problem, while empirical risk is a sample estimate of population performance. Sampling noise, dependence and population shift are different problems; a fixed random seed resolves none of them.

Next action

Write down one population-risk claim for your prediction contract, the loss used to assess it, and two ways the observed sample could differ from the population where the model will be used.

Choose another lesson