On this page
Suppose you want a model that says how likely a student is to pass, given hours studied. A straight line can predict 1.3 or −0.2, and neither is a probability. Logistic regression fixes this with one extra step, and the mathematics is small enough to work by hand.
What does the sigmoid function do in logistic regression?
The model first computes a linear score, z = w·x + b: a weighted sum of the features plus a bias. As Jurafsky and Martin explain in their textbook chapter Logistic Regression and Text Classification, z can be any real number, so it is passed through the sigmoid, σ(z) = 1/(1 + e^(−z)), which maps every real number into the range (0, 1), nearly linear around 0 and flat toward the ends.
Note that σ(0) = 0.5 and 1 − σ(z) = σ(−z): the probability of failing is the sigmoid of the negative score.
The problem is old. D. R. Cox's 1958 paper The Regression Analysis of Binary Sequences studied sequences of 0s and 1s where the chance of a 1 depends on other variables: the pass/fail setting.
Why is logistic regression linear in the log-odds?
Invert the sigmoid. If p = σ(z), then z = ln(p/(1 − p)). The ratio p/(1 − p) is the odds, and its logarithm is the logit; Jurafsky and Martin call z the logit because the logit function is the inverse of the sigmoid.
So logistic regression is linear, but in log-odds rather than in probability. One extra unit of a feature adds w to the log-odds, which multiplies the odds by e^w.
How are the weights found with maximum likelihood?
Each outcome y is 0 or 1, so the model's probability of what happened is p^y (1 − p)^(1 − y). Maximum likelihood chooses w and b to make the product of these over all students as large as possible. Logs turn the product into a sum; flipping the sign gives a loss to minimise, which for one student is:
L = −[y ln p + (1 − y) ln(1 − p)]
In section 4.5 of the chapter, Jurafsky and Martin derive this negative log-likelihood, called the cross-entropy loss; in section 4.6 they note it is convex for logistic regression, with no local minima. For background, read our explainer on maximum likelihood estimation; the entropy in the name comes from Shannon's measure of information.
What does the gradient (p − y)x mean?
Differentiate the loss for one example and the sigmoid's derivative, σ(1 − σ), cancels neatly. The result, given as equations 4.26 and 4.28 in Jurafsky and Martin's chapter, is ∂L/∂w = (p − y)x and ∂L/∂b = p − y.
In words: the error (prediction minus truth) times the input. A correct prediction gives zero gradient; an overestimate pushes the weight down, in proportion to the input. Gradient descent steps against it, w ← w − η(p − y)x, with learning rate η. For why the gradient points uphill, see the gradient and directional derivative.
A worked example: pass or fail against hours studied
Take one feature, x = hours studied, and suppose training gave w = 1.5 and b = −6 (round numbers for clean arithmetic, not a fit to real students).
Probability. At 2 hours, z = −3 and p = 1/(1 + e^3) ≈ 0.047. At 4 hours, z = 0 and p = 0.5. At 6 hours, z = 3 and p ≈ 0.953.
Odds. Each extra hour multiplies the odds of passing by e^1.5 ≈ 4.48.
Decision boundary. Predict pass when p > 0.5, the threshold the textbook calls the decision boundary. That means z > 0, so x > 4 hours. With two features z = 0 is a line, with three a plane: a linear classifier.
Likelihood. Students at 2 hours (failed), 4 hours (passed) and 6 hours (passed) give (1 − 0.047)(0.5)(0.953) ≈ 0.454, a total cross-entropy of −ln 0.454 ≈ 0.79.
One gradient step. A 5-hour student failed. The model says p = σ(1.5) ≈ 0.818, so the loss is −ln(0.182) ≈ 1.70. The gradient is 0.818 × 5 ≈ 4.09 for w and 0.818 for b. With η = 0.1, w becomes about 1.09 and b about −6.08, and a 5-hour student's predicted chance drops from 0.82 to roughly 0.35.
How should you practise this?
Derive σ'(z) = σ(z)(1 − σ(z)), then use the chain rule to reach (p − y)x.
Rework the example with b = −4.5 and find the new boundary (3 hours).
If logarithms or the chain rule feel shaky, the Fermi Concept Library covers every concept in physics, chemistry, biology, mathematics and computing. To check a derivation line by line, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.



