Sub topic 6
Fitting by maximum likelihood
This chapter assumes you know the sigmoid model from the previous chapters, basic probability (independent events, products of probabilities), and how to read NumPy code. The question here is how to choose the weights and bias.
The idea
The model gives each example a probability. For example i with features xi, the model says pi = sigmoid(w·xi + b) is the probability that yi = 1. Different weights give different p_i values, so some weights make the observed labels more probable than others.
Maximum likelihood estimation picks the weights under which the observed data is most probable.
One example, one formula
For a single example, the probability the model assigns to the true label is:
- pi when yi = 1
- 1 − pi when yi = 0
Both cases fit one expression, because y_i is either 0 or 1:
P(yi | xi) = pi^yi · (1 − pi)^(1 − yi)
If yi = 1, the second factor has exponent 0 and drops out. If yi = 0, the first one does.
The whole dataset
If the examples are independent, the probability of all labels is the product of the individual probabilities. This is the likelihood L(w, b):
L(w, b) = ∏ pi^yi · (1 − pi)^(1 − yi)
The independence assumption matters. Repeated measurements of the same patient, or time series with strong autocorrelation, break it.
Taking the log
Products of many numbers below 1 underflow floating-point arithmetic. A product also has a messy derivative. The logarithm increases monotonically, so the weights that maximize the likelihood also maximize its log, and the log turns the product into a sum:
log L = Σ [ yi · log(pi) + (1 − yi) · log(1 − pi) ]
Optimizers conventionally minimize, so we flip the sign. The negative log-likelihood, divided by n, is the log loss. The next chapter covers it.
Computing it
This code compares three candidate weights on a small one-feature dataset, with the bias fixed at 0.
import numpy as np
def sigmoid(z):
return 1.0 / (1.0 + np.exp(-z))
def log_likelihood(w, b, X, y):
p = sigmoid(X @ w + b)
return np.sum(y * np.log(p) + (1 - y) * np.log(1 - p))
X = np.array([[-2.0], [-1.0], [-0.5], [0.5], [1.0], [2.0]])
y = np.array([0, 0, 1, 0, 1, 1])
for w in (0.0, 1.0, 3.0):
print(w, round(log_likelihood(np.array([w]), 0.0, X, y), 3))At w = 0 every probability is 0.5, so the log-likelihood is 6 · log(0.5) ≈ −4.159. At w = 1 it rises to about −2.93, and at w = 3 it falls to about −3.50. The data prefers a moderate slope. A steep slope is punished by the two examples it classifies confidently and wrongly.
No closed-form answer
Linear regression has a formula for its best weights. Setting the gradient of the log-likelihood to zero in logistic regression gives equations that mix the weights inside the sigmoid, and these have no algebraic solution. We therefore search numerically, using gradient descent. The log-likelihood is concave in (w, b), so there are no local maxima to get stuck in.
Exercise
A model assigns probabilities 0.9, 0.2 and 0.7 to the positive class for three independent examples with labels 1, 0 and 1. What is the likelihood of these labels, and why do practitioners maximize the log-likelihood instead?
Show answer
The label probabilities are 0.9, 1 − 0.2 = 0.8, and 0.7, so the likelihood is 0.9 × 0.8 × 0.7 = 0.504. Practitioners use the log-likelihood because the log turns the product into a sum. This avoids numerical underflow with many examples and makes the gradient simpler. Since the log is monotonic, the same weights maximize both.