Chapter 11 of 25
Probability & Statistics Essentials
Making sense of uncertainty in data and predictions
Motivation
Here's a question worth sitting with: why do we train classifiers with "cross-entropy loss" and regressors with "mean squared error"? Most courses just hand you these formulas and say "use this one for classification, that one for regression," as if they were arbitrary conventions. They aren't. Both losses fall directly out of one unifying idea from probability theory, and once you see where they come from, you'll never again think of a loss function as an arbitrary design choice — you'll see it as a direct mathematical consequence of a modeling assumption you already made.
Probability and statistics are the language in which we express uncertainty — and every prediction a neural network makes is, whether we say so explicitly or not, secretly a statement about probability. A "90% confident it's spam" isn't just a nice user-facing number; it's the entire mathematical foundation the network was trained on.
A Real-Life Scenario
Think about a weather forecasting model predicting "70% chance of rain tomorrow." It isn't claiming certainty — it's expressing a calibrated belief under uncertainty, based on patterns in temperature, pressure, humidity, and historical outcomes. If it rains on 70 out of 100 days it predicted "70% chance of rain," the model is well-calibrated; if it only rains on 30 of those days, something is badly miscalibrated, even if the model still "gets the yes/no right" most of the time.
A neural network binary classifier works exactly the same way. Its sigmoid output isn't just an arbitrary number squeezed between 0 and 1 — it's explicitly interpreted as a probability, and the entire training procedure is built around making that probability as trustworthy as possible given the data. Understanding why requires exactly the tools in this chapter.
Building the Intuition
Before the formulas, here's the mental model:
- A random variable is just a variable whose value comes from some random process — a coin flip, tomorrow's rainfall, whether an email is spam.
- A probability distribution describes how likely each possible value is.
- Expectation is the long-run average value you'd see if you repeated the random process many times; variance is how spread out those values tend to be around that average.
- Maximum Likelihood Estimation asks: given the data we actually observed, which model parameters would have made that data most probable? This one question, taken seriously, is where cross-entropy loss comes from.
The Math Behind It
Three Distributions You'll See Constantly
Where: is the probability of "success" (outcome 1).
Why this matters: the output of a binary classifier's sigmoid activation is interpreted as exactly this — the probability an example belongs to class 1. Binary cross-entropy loss, covered below, is derived directly from the Bernoulli likelihood.
Why this matters: this is exactly what a softmax output layer produces — a vector of probabilities, one per class, that sum to 1. Categorical cross-entropy loss is derived from this distribution the same way binary cross-entropy is derived from the Bernoulli.
Where: is the mean (center) and is the variance (spread).
Why this matters: weight initialization schemes you'll meet later in this course draw initial weights from a Gaussian with a carefully chosen variance. The Gaussian distribution is also exactly what mean squared error loss assumes about regression noise, as the next section shows.
Expectation and Variance
Intuition: is the long-run average value of across many repetitions; measures how spread out 's values typically are around that average.
Why this matters: expectation and variance are central to understanding batch normalization, a technique you'll meet later that normalizes each layer's activations to have approximately zero mean and unit variance.
Descriptive Statistics From Real Data
Given a finite dataset , we estimate the population mean and variance with sample statistics:
Why this matters: these exact statistics are computed, per feature, during feature standardization (a common preprocessing step), and per batch, inside batch normalization layers.
Worked example: for a feature with values :
The standardized values are — mean 0, variance 1, exactly the goal of standardizing a feature before training.
Maximum Likelihood Estimation: Where Loss Functions Actually Come From
This is the payoff of the whole chapter — the answer to why we use the specific loss functions we use.
Given a model that defines a probability distribution over possible outputs , and a training dataset, Maximum Likelihood Estimation chooses the parameters that maximize the probability the model assigns to the data we actually observed.
Intuition: among all the possible settings of , pick the one under which the training data you actually collected would have been the least surprising outcome.
Deriving binary cross-entropy from scratch. Suppose the model outputs , interpreted as a Bernoulli probability. Given independent training examples, the likelihood of the entire training set is:
Multiplying together many small probabilities underflows to zero numerically, so instead we maximize the log-likelihood — the logarithm is monotonically increasing, so it has the same maximizer:
Finally, since optimization is conventionally framed as minimizing a loss, we negate this and average over examples — which produces exactly binary cross-entropy:
Minimizing cross-entropy loss is mathematically identical to maximizing the likelihood of the training data under a Bernoulli (or Categorical, for multi-class problems) model. This is not a coincidence or an arbitrary convention — cross-entropy is precisely the loss function that MLE derives, given the assumption that the model's output is a genuine probability. The same style of derivation, starting from a Gaussian noise assumption instead of a Bernoulli one, produces mean squared error for regression — you'll see this given full treatment in a dedicated loss-functions chapter later in this course.
"Derive cross-entropy loss from first principles." Reproduce the steps above: start from the Bernoulli likelihood of the whole dataset, take its logarithm, negate it, and average. Being able to walk through this from memory demonstrates real understanding, not just formula recall.
Implementing It
import numpy as np
# Sample mean and variance, matching the worked example above
x = np.array([2.0, 4.0, 4.0, 6.0])
mean = x.mean()
var = x.var() # population variance (divides by N, not N-1)
z = (x - mean) / np.sqrt(var)
print("mean =", mean, "variance =", var)
print("standardized =", z)
# Binary cross-entropy loss for a small batch of predictions
y_true = np.array([1, 0, 1, 1])
y_pred = np.array([0.9, 0.2, 0.6, 0.8]) # sigmoid outputs, interpreted as probabilities
bce = -np.mean(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))
print("binary cross-entropy =", bce)The standardization block reproduces the worked example by hand, confirming numpy's mean() and var() match the manual arithmetic above. The cross-entropy block implements the formula derived in this chapter directly: for each example, it takes when the true label is 1 and when it's 0, then averages across the batch — exactly the negated, averaged log-likelihood we derived.
If a predicted probability is ever exactly 0 or 1, np.log will produce -inf or a warning, and the loss can become nan. Production implementations clip predictions to a small epsilon away from 0 and 1 (e.g. np.clip(y_pred, 1e-7, 1-1e-7)) before taking the logarithm.
Key Takeaways
- The Bernoulli and Categorical distributions model binary and multi-class classification outputs respectively; the Gaussian distribution underlies weight initialization and mean squared error loss.
- Expectation measures a distribution's average value; variance measures its spread around that average.
- Sample mean and variance (computed from real data) are used in feature standardization and inside batch normalization layers.
- Cross-entropy loss is not an arbitrary choice — it is the direct mathematical consequence of Maximum Likelihood Estimation under a Bernoulli or Categorical output assumption.
- The same MLE reasoning, applied to a Gaussian noise assumption instead, is what produces mean squared error loss for regression.
Common Mistakes
It's easy to memorize "cross-entropy for classification, MSE for regression" as an unexplained rule of thumb. Both are consequences of a specific probabilistic assumption about the output (Bernoulli/Categorical vs. Gaussian) combined with Maximum Likelihood Estimation — understanding this lets you reason about which loss to use for distributions that don't fit either mold.
Dividing by (population variance) versus (the unbiased sample variance estimator, Bessel's correction) gives slightly different answers. NumPy's .var() defaults to dividing by unless you pass ddof=1. This rarely matters for large batches but is a common source of small numerical discrepancies.
Interview Corner
Where This Shows Up in Practice
Every classifier's confidence score, every use of batch normalization, and every choice of loss function in this entire course traces back to the ideas in this chapter. With vectors and matrices (describing structure), calculus (describing how to learn), and probability (describing what a loss function actually means) all in place, you now have the complete mathematical foundation needed to derive backpropagation rigorously, layer by layer — exactly where the course goes next.