Logistic Regression › Formula

Michael BrenndoerferJune 24, 20259 min read
Machine Learning from ScratchAll chapters
Part 4 of 9Regression Models
Chapter 37Logistic Regression › Formula
Reading section 2 of 8 · Formula · 9 minSection 2 of 8 · 9 min

Book contents

Section 2 of 8 · 9 min read

Logistic Regression › FormulaOne section · Sections in this chapter2 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The core formula behind logistic regression involves transforming a linear combination of features into a probability using the logistic function. Let's break this down step by step.

The Linear Predictor

First, we start with a linear combination of features, just like in linear regression:

η=β0+β1x1+β2x2+⋯+βpxp\eta = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \cdots + \beta_p x_p

where:

  • η\eta is the linear predictor (a scalar value for a single observation that can take any real value from −∞-\infty to +∞+\infty)
  • β0\beta_0 is the intercept (the baseline log-odds when all features equal zero)
  • β1,β2,…,βp\beta_1, \beta_2, \ldots, \beta_p are the regression coefficients for each feature (quantify the effect of each feature on the log-odds)
  • x1,x2,…,xpx_1, x_2, \ldots, x_p are the feature values (predictor variables for a single observation)
  • pp is the number of features (total count of predictor variables in the model)

This linear predictor can take any real value from −∞-\infty to +∞+\infty.

The Logistic Function

The main idea of logistic regression is the logistic function (also called the sigmoid function), which maps the linear predictor into a probability:

p=11+e−η=eη1+eηp = \frac{1}{1 + e^{-\eta}} = \frac{e^{\eta}}{1 + e^{\eta}}

where:

  • pp is the predicted probability (bounded between 0 and 1, representing the probability that Y=1Y = 1)
  • ee is Euler's number (approximately 2.718, the base of the natural logarithm)
  • η\eta is the linear predictor from above (the linear combination β0+β1x1+⋯+βpxp\beta_0 + \beta_1 x_1 + \cdots + \beta_p x_p)

Let's understand why this transformation works:

When η\eta is very large (positive):

  • e−ηe^{-\eta} becomes very small (close to 0)
  • 1+e−η1 + e^{-\eta} becomes close to 1
  • p=11+e−η≈11+0=1p = \frac{1}{1 + e^{-\eta}} \approx \frac{1}{1 + 0} = 1

When η\eta is very small (negative):

  • e−ηe^{-\eta} becomes very large
  • 1+e−η1 + e^{-\eta} becomes very large
  • p=11+e−η≈1very large number≈0p = \frac{1}{1 + e^{-\eta}} \approx \frac{1}{\text{very large number}} \approx 0

When η=0\eta = 0:

  • e−η=e0=1e^{-\eta} = e^0 = 1
  • p=11+1=12=0.5p = \frac{1}{1 + 1} = \frac{1}{2} = 0.5

Let's visualize the logistic function to better understand its behavior:

Out[2]:
Visualization
S-shaped sigmoid curve from eta minus six to six with probability bounded between zero and one, reference lines at eta zero and probability 0.5, three annotated points, two limiting-behavior callouts, and a four-item key-properties box.
The logistic (sigmoid) function maps any real number into a probability between 0 and 1. The S-shaped curve is steepest at η = 0 (where p = 0.5) and flattens as it approaches the extremes.

This visualization shows the characteristic S-shaped curve of the logistic function. Notice how:

At η = 0: The probability is exactly 0.5, and the curve is steepest here

As η becomes very negative: The probability approaches 0 asymptotically

As η becomes very positive: The probability approaches 1 asymptotically

The curve is smooth: No sharp corners or discontinuities, making optimization tractable

The curve is symmetric: Around the point (0, 0.5)

The Logit Function (Inverse of Logistic)

The logit function is the inverse of the logistic function and represents the log-odds:

logit(p)=log⁡(p1−p)=η\text{logit}(p) = \log\left(\frac{p}{1-p}\right) = \eta

where:

  • logit(p)\text{logit}(p) is the logit function (transforms probabilities to log-odds)
  • pp is the probability (bounded between 0 and 1)
  • p1−p\frac{p}{1-p} is the odds (the ratio of the probability of success to the probability of failure)
  • log⁡(⋅)\log(\cdot) is the natural logarithm (base ee)
  • η\eta is the linear predictor (the right-hand side: β0+β1x1+⋯+βpxp\beta_0 + \beta_1 x_1 + \cdots + \beta_p x_p)

This equation reads as: "The log-odds of the probability equals the linear predictor."

Let's visualize this relationship to understand why logistic regression models the log-odds as a linear function:

Out[3]:
Visualization
Increasing logit curve from probability 0.01 to 0.99 with log-odds from minus six to six, reference lines at probability 0.5 and log-odds zero, three numeric point annotations, two limiting-behavior callouts, and a five-item key-properties box.
The logit function maps probabilities (bounded between 0 and 1) into log-odds (unbounded real numbers). This mapping is needed because it lets us model probabilities using a linear function. Notice how probabilities near 0.5 map to log-odds near 0, while extreme probabilities (near 0 or 1) map to very negative or very positive log-odds.

This visualization shows why logistic regression models the log-odds rather than the probability directly. The logit transformation converts probabilities (which are bounded between 0 and 1) into log-odds (which can be any real number from −∞-\infty to +∞+\infty). This unbounded range makes it possible to use a linear model:

At p = 0.5: The log-odds are 0, representing equal odds for both outcomes

As p approaches 0: The log-odds approach −∞-\infty, representing very low odds

As p approaches 1: The log-odds approach +∞+\infty, representing very high odds

The relationship is smooth and monotonic: Higher probabilities correspond to higher log-odds

Complete Logistic Regression Model

Putting it all together, the complete logistic regression model is:

log⁡(p1−p)=β0+β1x1+β2x2+⋯+βpxp\log\left(\frac{p}{1-p}\right) = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \cdots + \beta_p x_p

where:

  • p=P(Y=1∣X)p = P(Y=1|X) is the probability that the response variable equals 1 given predictors XX (the parameter we're modeling)
  • p1−p\frac{p}{1-p} is the odds of success (the ratio of the probability of success to the probability of failure)
  • log⁡(p1−p)\log\left(\frac{p}{1-p}\right) is the log-odds or logit (the natural logarithm of the odds)
  • β0\beta_0 is the intercept (the log-odds when all features equal zero)
  • β1,β2,…,βp\beta_1, \beta_2, \ldots, \beta_p are the regression coefficients (represent the change in log-odds for a one-unit increase in each feature)
  • x1,x2,…,xpx_1, x_2, \ldots, x_p are the feature values (predictor variables for a single observation)
  • pp is the number of features (total count of predictor variables in the model)

This is read as: "The log-odds of the probability equals the linear combination of features."

Matrix Notation

In matrix form, logistic regression can be written as:

log⁡(p1−p)=Xβ\log\left(\frac{\mathbf{p}}{1-\mathbf{p}}\right) = \mathbf{X}\boldsymbol{\beta}

where:

  • p\mathbf{p} is the vector of predicted probabilities (an n×1n \times 1 vector where each element is the predicted probability for one observation)
  • X\mathbf{X} is the design matrix of features (an n×(p+1)n \times (p+1) matrix including a column of 1s for the intercept)
  • β\boldsymbol{\beta} is the vector of coefficients (a (p+1)×1(p+1) \times 1 vector containing the intercept and all feature coefficients)
  • nn is the number of observations (rows in the dataset)
  • pp is the number of features (excluding the intercept)
  • The logit function log⁡(p1−p)\log\left(\frac{\mathbf{p}}{1-\mathbf{p}}\right) is applied element-wise to p\mathbf{p}

Mathematical Properties

Bounded Output: The logistic function ensures probabilities remain between 0 and 1

Smooth and Continuous: The function is differentiable everywhere, making optimization tractable

Symmetric: The function is symmetric around 0.5 when η=0\eta = 0 (i.e., p(0)=0.5p(0) = 0.5)

Monotonic: As the linear predictor increases, the probability increases monotonically

Asymptotic: The function approaches 0 and 1 asymptotically without reaching them

Convex Log-likelihood: The negative log-likelihood is convex, so a unique global maximum