Logistic regression
The question
Each seal passes or fails. How would you estimate the chance that a new seal passes at a given pressure?
- 1 SeeFit a curve to test results.
- 2 TryPredict how the odds change.
- 3 ProveFollow the score as slopes grow.
- 4 ApplyCompute probabilities, odds, and fit scores.
Before you start · 3 quick checks on complements, independence, and logarithms
Two ideas before you start
2 ideasTurn a line into probabilities
The sigmoid maps the linear score to a probability between zero and one.
Read the horizontal axis as the score and the vertical axis as the chance of a pass. The sigmoid increases with the score.
Exact definition
Let \(Y\) be the test outcome, with \(Y=1\) for a pass and \(Y=0\) for a fail. Write \(x\) for the input, \(a\) for the intercept and \(b\) for the slope. Logistic regression models the conditional probability of a pass by applying the sigmoid \(\sigma\) to the score \(z=a+bx\).
\[\begin{aligned} p(x)&=\sigma(a+bx),\\ \sigma(z)&=\frac{1}{1+e^{-z}}. \end{aligned}\]For every finite \(z\), \(0\lt\sigma(z)\lt1\). Its complement satisfies \(1-\sigma(z)=\sigma(-z)\). The score is also the log of the odds of a pass. The odds are \(p/(1-p)\), so substituting the sigmoid gives \(p/(1-p)=e^z\).
Score the outcome you saw
A probability bar shows the shares for a pass and a fail.
A trial contributes the probability of its actual outcome. A failure contributes the complement of the chance of a pass.
Exact definition
For a finite sample of \(n\) tests, \(x_i\) is the input and \(y_i\) the observed outcome of test \(i\). Write \(p_i=p(x_i)\). The probability \(q_i\) of the observed label is \(p_i\) for \(y_i=1\) and \(1-p_i\) for \(y_i=0\).
If outcomes are independent conditional on their input settings, the likelihood \(L\) is the product of the \(q_i\), viewed as a function of \(a\) and \(b\) for the observed data. Its natural logarithm is the log-likelihood \(\ell\).
\[\begin{aligned} L&=\prod_i q_i,\\ \ell&=\sum_i\log q_i. \end{aligned}\]The natural log is strictly increasing, so the two scores rank candidate fits in the same order. See maximum likelihood for the same fitting principle with count data.
What the model assumes
The seals and results in this lesson are invented. The model treats the pressure settings as given and uses the same pass probability for each test run at the same setting. Independence conditional on those settings would need checking in a real study. For example, seals from one batch may share a defect.
Least squares fits a straight line by minimizing the sum of squared vertical misses. With a binary outcome, a line with nonzero slope gives values outside the probability range somewhere on the real line. Logistic regression keeps the linear expression for the log-odds and uses likelihood to choose its coefficients.
Fit the pressure tests
- Drag the midpoint and slope sliders to fit the curve. You can also use the arrow keys or enter a value in either box.
- Try to raise the log-likelihood, then press Check my curve.
How to read the curve and score
The input \(x\) is pressure relative to a reference setting, in megapascals. A negative value means a pressure below that reference. The sliders set \(c\), the point where the pass probability is one half, and \(b\), the positive slope of the log-odds. The score at \(x\) is \(b(x-c)\), which is the same as \(a+bx\) with \(a=-bc\).
The graph shows the full study interval. The observed marks sit at the outcome levels. They show what happened on a test, while the curve shows a fitted probability. Conditional on the pressure settings, each separate seal contributes its own log term. A mark labeled with a count contributes that many copies of the same term.
A result the curve assigns a small probability makes a more negative contribution. When all settings are held fixed, raising the total score makes the recorded data more likely under the model. The score describes this set of tests. Its size alone gives no estimate of how well the curve predicts a fresh batch.
Why the benchmark is the best fit
At one pressure, suppose there are \(m\) passes out of \(N\) independent tests, with \(0\lt m\lt N\) and one shared pass probability \(p\). The log-likelihood for that group is \(m\log p+(N-m)\log(1-p)\). Its derivative is
\[\begin{aligned} &\frac{m}{p}-\frac{N-m}{1-p}\\ &=\frac{m-Np}{p(1-p)}. \end{aligned}\]The derivative changes from positive to negative as \(p\) crosses \(m/N\), so the group's best probability is the observed pass fraction. There are two distinct pressures here. Taking the log-odds of the two best probabilities gives two points on the log-odds line. A unique line goes through them, so one curve attains both group maxima at once. The sliders sample that family on a grid. The benchmark uses the unrestricted coefficients.
Compare two settings
Odds compare the probability of a pass with the probability of a fail. For example, if the two outcomes are equally likely, the odds are even. Use the odds form of the model from Start to find how a pressure change affects the odds in a new model.
Same pressure change, different starting odds
- Change the starting odds and the pressure increase. Compare the resulting odds factor with the change in probability.
Why the ratio has this form
Let \(O(x)\) denote the odds at pressure \(x\), and let \(d\) be a change in the pressure. Since \(O(x)=e^{a+bx}\), dividing the odds at \(x+d\) by the odds at \(x\) cancels the starting term \(e^{a+bx}\). That gives
\[\frac{O(x+d)}{O(x)}=e^{bd}.\]To recover probability from odds \(O\), solve \(O=p/(1-p)\) for \(p\), giving \(p=O/(1+O)\). Compare the probability changes at the two starting odds as you reach each goal.
Why strict separation rules out a finite maximum
Assumptions. Take a finite sample with both labels present and a real threshold \(c\) such that \(x_i\gt c\) for every pass and \(x_i\lt c\) for every fail. All inputs are finite. Outcomes are modeled as independent conditional on the inputs. Allow any finite real intercept \(a\) and slope \(b\) in the logistic model, with no penalty.
Claim. The log-likelihood has supremum zero, meaning zero is its least upper bound. No finite coefficient pair attains it.
For the builder, fix the threshold \(c\) and work along \(a=-kc,\ b=k\) with \(k\gt0\). Write \(q_i(k)\) for the probability of the observed label along that path, and \(t_i=(2y_i-1)(x_i-c)\) for the signed distance to the threshold. Here \(t_i\gt0\). Compare slopes \(k_2\gt k_1\gt0\).
Use the complement identity from Start, the fact that the sigmoid is strictly increasing and tends to one as its input tends to infinity, and continuity of the natural log at one.
Choose one statement per line under the assumptions in the theorem.
What to check when a fit keeps steepening
Compare the labels by pressure and look for a strict cutoff in either direction. This theorem treats the case with passes above the cutoff. Reversing the sign of the input brings the opposite case into the same proof. Ties across labels at the cutoff require a separate analysis.
A finite slope returned by software may come from a stopping rule or a constraint. For example, subtracting \(\lambda(a^2+b^2)\) with \(\lambda\gt0\) keeps the optimum finite. The penalized score is continuous and tends to minus infinity as the coefficient norm grows, since the unpenalized score is bounded above by zero. This changes the criterion used to choose the curve.
Ordinary least squares as a projection finds the closest fitted vector in a linear space of predictions. Here steepening the separating curve gives a sequence of probability vectors that approaches the observed labels. The limiting vector would give each recorded label probability one.
Read a fitted model and score the data
Use natural logarithms. Keep intermediate values unrounded and enter decimal answers. The scores in the last problems refer to the specified observations and their stated models.
Further reading
- Alan Agresti, Categorical Data Analysis, discussion of logistic regression.
- David W. Hosmer Jr., Stanley Lemeshow, and Rodney X. Sturdivant, Applied Logistic Regression, introduction to the logistic regression model.