Hexa Scholar

Logistic regression

A bench test fixture holds a round seal, with spare seals in a tray beside it.
A bench fixture holds a seal for a pressure test.

The question

Each seal passes or fails. How would you estimate the chance that a new seal passes at a given pressure?

  1. 1 SeeFit a curve to test results.
  2. 2 TryPredict how the odds change.
  3. 3 ProveFollow the score as slopes grow.
  4. 4 ApplyCompute probabilities, odds, and fit scores.
Before you start · 3 quick checks on complements, independence, and logarithms

Two ideas before you start

2 ideas

Turn a line into probabilities

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.

1 See

Fit the pressure tests

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.

2 Try

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.

3 Prove

Why strict separation rules out a finite maximum

Theorem

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.

4 Apply

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.

Key point Convert a linear score to a probability with the sigmoid or to odds with the exponential. To evaluate a fit, add the log probability of each label actually observed.
Level Intermediate undergraduate Prerequisites Conditional probabilities, natural logs, and straight lines Time About min

Further reading