A courier wants the probability that a parcel will arrive late, given the distance it has to travel. A least-squares line fitted to late and on-time labels coded and can predict numbers below or above , and no probability can take those values. Logistic regression keeps a linear score in the features and passes it through the sigmoid function, so every output lies strictly between and . This lesson converts between scores, odds and probabilities, reads each coefficient as a multiplier of the odds, and keeps the probability apart from the decision that a threshold makes.
Key terms
Binary classification
Definition Binary classification is a supervised learning task in which the target takes exactly two values, coded for the positive class and for the negative class, and the input is a feature vector in known at prediction time. A classifier either outputs a predicted label in directly or estimates the conditional probability , and a decision rule then turns that estimate into a label. Which class is called positive is a coding convention, usually the event of interest. Swapping the codes replaces every probability of the positive class by .
In plain words Each case has a yes or no answer. A probabilistic classifier says how likely yes is for cases with these features, and a separate rule decides what to do with that number. Positive means coded , not good or desirable.
Example In the delivery data a late parcel is coded and an on-time parcel , and the only feature is the distance in kilometers. Eight constructed training parcels at distances have labels . The least-squares line through these labels is
which gives about at and about at . Neither number can be a probability, so a probability model needs a different form.
Sigmoid function
Definition The sigmoid function, also called the logistic function, is
for every real number . Its values satisfy . It is strictly increasing, with , as and as . It has the symmetry , and its derivative is , which is largest at , where it equals .
In plain words The sigmoid squeezes the whole number line into the interval from to along an S-shaped curve. A score of means even chances, large positive scores give probabilities near and large negative scores give probabilities near , but no finite score gives exactly or . The curve bends, so equal steps in the score do not give equal steps in probability.
Example Take the declared score , with coefficients chosen to show the conversion rather than fitted to data.
| Distance in km | ||||
|---|---|---|---|---|
| Score | ||||
| Probability |
Each column adds to the score, yet the probability rises by about , then , then . The last two steps are equal because of the symmetry around .
Odds and log-odds
Definition For a probability with , the odds of the event are , a number in . For the log-odds, also called the logit, are
which can be any real number. Solving for gives . If the log-odds equal , then , so and . The logit and the sigmoid therefore undo each other, with for every real and for .
In plain words Odds compare the chance of the event with the chance of its absence. Odds of mean three late parcels for every on-time parcel among cases like this one, in the long run and if the model is right. Probability, odds and log-odds carry the same information on three different scales, and logistic regression is linear on the last of them.
Example A probability of has odds and log-odds . Odds of correspond to probability and log-odds . Doubling the odds from to moves the probability from to , not from to . A multiple of the odds is not the same multiple of the probability.
Logistic regression
Definition Logistic regression is a model for a binary target. For a feature vector in , an intercept and a weight vector in , it states
so the log-odds of the positive class are the linear score . Linearity of the log-odds in the chosen features is an assumption of the model, and the features may include engineered ones such as squares or products. The log loss of one example with label and predicted probability of class is
which equals when and when . Given training examples for , the parameters are usually estimated by minimizing the mean log loss, the empirical risk
where . The next lesson derives this loss from the likelihood. The function is convex in , and its gradient with respect to is
The empirical risk has exactly one minimizer when no nonzero gives for every example with and for every example with . If a hyperplane separates the two training classes in this way, with at least one score not zero, the loss keeps decreasing as those separating coefficients are multiplied by larger and larger numbers, and it has no minimizer.
In plain words The model is a line or flat plane on the log-odds scale and an S-shaped curve on the probability scale. In the gradient each example contributes its error , weighted by its features, so a gradient descent step tends to raise the probabilities of late parcels and lower those of on-time parcels. The outputs are model probabilities. Whether parcels given probability are late about percent of the time is a separate property, calibration, and it is checked on held-out data rather than assumed.
Example Fit the model to the eight training parcels. Gradient descent on the standardized distance reaches a point where the gradient is zero to machine precision, and converting back to kilometers gives the estimates and per kilometer. A finite minimizer exists because the classes overlap. A late parcel at km lies below an on-time parcel at km and a late parcel at km lies above it, so no cut point separates the classes in either direction. The mean training log loss is about , below the value of predicting for every parcel, and the fitted probabilities rise from about at km to about at km. These are training results on constructed data and say nothing yet about performance on new parcels.
Odds ratio
Definition The odds ratio comparing two feature vectors is the odds at the first divided by the odds at the second. In logistic regression the odds at are . If feature enters the model only through the term , then raising by units while every other feature stays fixed multiplies the modeled odds by
at every starting point , where has in position and elsewhere. The odds ratio depends on the unit of , since measuring the same quantity in a unit times smaller divides its coefficient by . If also appears in a squared or product term, the odds ratio for a change in depends on the starting values.
In plain words A coefficient is a fixed amount added to the log-odds, and its exponential is a fixed multiplier of the odds. It is not a fixed change in probability and not a fixed ratio of probabilities. With one feature, the probability changes at the rate per unit at a point where it equals , which is fastest near and slow near or . An odds ratio from a prediction model describes an association given the other modeled features. It can be read as the effect of changing the feature only when the way the data were collected supports that causal reading.
Example In the declared model , each extra kilometer multiplies the odds by , while the probability moves by different amounts.
| Step | Probability | Change | Ratio of probabilities |
|---|---|---|---|
| to km | to | ||
| to km | to | ||
| to km | to |
The odds ratio is in every row. Written per meters, the same model has coefficient and odds ratio per meters, with exactly the same probabilities. The fitted model above has odds ratio per kilometer.
Decision boundary
Definition A decision rule with threshold , where , predicts class when the estimated probability satisfies and class otherwise. The sigmoid is strictly increasing, so for logistic regression holds exactly when . The decision boundary is the set of feature vectors with
When it is a hyperplane in feature space, a point for one feature and a line for two. When every case gets the same probability and the same decision. For the boundary is the set where . Points on the boundary are assigned to class under this rule. If the features include engineered terms such as , the boundary is a hyperplane in those features and can be curved in the original inputs.
In plain words The model gives probabilities, and the threshold turns them into decisions. Moving the threshold moves the boundary and leaves every probability where it was. The S-shape of the sigmoid relates the score to the probability, while the shape of the boundary comes from the features, so a logistic model that uses only the raw features has a straight or flat boundary. A threshold of is a convention, and a good threshold depends on the costs of the two kinds of error.
Example For the declared model with , the boundary solves , so parcels at km or more are predicted late. With the boundary solves and moves to km. A parcel at km keeps its probability under both thresholds, but its decision changes from late to on time. For the fitted model the boundary at is km.
Check yourself
- A declared model for late delivery has score , where is the distance in kilometers. Find the score, the odds and the probability of lateness for a parcel that travels km.
Show the answer
The score is , and the odds are . The probability is
As a check, as well. Under the model, parcels like this one are late about times for every on-time arrivals in the long run.
- For the same model, a colleague says that one more kilometer multiplies the probability of lateness by , so a parcel that travels km has probability . What goes wrong, and what are the correct probability and odds ratio?
Show the answer
The product is about , larger than , which no probability can be. The factor multiplies the odds, not the probability. The odds rise from to , so the probability is
The probability rises by about , a ratio of about , while the odds ratio is for every one-kilometer step. The odds ratio also describes the model’s association with distance, not by itself what would happen if a route were made longer.
- A model with two features has score and predicts class when . Describe the decision boundary for and for , and classify the case under each threshold. Did raising the threshold change its probability?
Show the answer
For the boundary is the line where the score is , that is . For the score must equal , which gives the parallel line . The case has score and probability exactly . It lies on the first boundary, so the rule assigns it to class , and under it goes to class . Its probability is in both cases, because the threshold changes only the decision.