σ(z) = 1/(1+e-z)
BCE = -y log ŷ
∇w = ∂L/∂w
Classification
Sigmoid
Home / Study Lab / Guides / Logistic Regression
COMPLETE MASTER GUIDE

Logistic Regression

From historical intuition to mathematical mastery. A beginner-friendly, visually interactive deep dive into the most fundamental classification algorithm.

11 Sections
45 min read
Beginner to Advanced
Interactive Visuals
Begin Learning
Contents
Historical Intuition Intuition Math Formulation Loss Function Closed Form vs Numerical Optimization Interactive Lab Pros & Cons Beginner to Advanced
01

Historical Intuition

Understanding the journey from step functions to smooth probability curves.

The Classification Problem

Imagine you are a doctor in the 1800s. You have patient data - age, blood pressure, symptoms - and you need to predict a simple outcome: will the patient survive or not? This is a binary classification problem. The answer is either 0 (no) or 1 (yes).

Early statisticians faced exactly this challenge. They needed a mathematical function that could take continuous inputs and produce a binary output. Their first attempt was brilliantly simple.

The Step Function (Heaviside Function)

Named after Oliver Heaviside (1850-1925), the step function was the natural first choice. The idea is straightforward:

$$H(z) = \begin{cases} 0 & \text{if } z < 0 \\ 1 & \text{if } z \geq 0 \end{cases}$$

You compute a weighted sum of your inputs $z = w^Tx + b$, and if it is positive, you predict class 1. If negative, class 0. Simple, clean, intuitive.

Three Critical Problems with the Step Function

  1. Not Differentiable: The sharp jump at z=0 means the derivative is undefined at that point. This is a mathematical dead end for optimization.
  2. Cannot Use Gradient Descent: Without a well-defined gradient everywhere, we cannot use calculus-based optimization to learn the weights. The gradient is zero everywhere except at the jump, where it does not exist.
  3. No Probability Output: The function outputs only 0 or 1. It cannot tell us how confident we are. Is the patient 51% likely to survive or 99%? The step function cannot distinguish.

The Need for Smoothness

Think about it this way: if a patient has values just slightly on one side of the threshold versus the other, the step function gives completely different predictions with no nuance. What we really need is:

Smooth Probability

Outputs between 0 and 1, representing confidence levels

Differentiability

Smooth everywhere so calculus-based optimization works

Monotonicity

Higher inputs should give higher probability

Enter the Sigmoid Function

The solution came from the world of statistics and population growth models. The logistic function (sigmoid) was first studied by Pierre-Francois Verhulst in the 1830s-1840s while modeling population growth in Belgium.

$$\sigma(z) = \frac{1}{1 + e^{-z}}$$

This elegant function solved all three problems simultaneously:

  • Smooth and differentiable everywhere - its derivative has a beautiful form: $\sigma'(z) = \sigma(z)(1-\sigma(z))$
  • Outputs are bounded between 0 and 1 - perfect for representing probabilities
  • Monotonically increasing - higher inputs always produce higher probabilities
1838
Pierre-Francois Verhulst proposes the logistic function for population modeling
1850s
Heaviside popularizes the step function for electrical engineering
1944
Joseph Berkson coins the term "logit" and develops logistic regression
1958
David Cox formalizes logistic regression for statistical analysis
Today
Still one of the most widely used algorithms in machine learning and medicine
02

Intuition of Logistic Regression

Why linear regression fails for classification and how logistic regression fixes it.

Starting with Linear Regression

Linear regression predicts a continuous value:

$$\hat{y} = w^Tx + b$$

This works great for predicting house prices, temperatures, or stock values. But what happens when we try to use it for classification?

Why Linear Regression Fails for Classification

  • Predictions can go below 0 or above 1 - these are not valid probabilities
  • Outliers drastically shift the decision boundary
  • The relationship between features and class probability is not linear
  • Mean squared error loss is not convex for classification

The Probability Requirement

For classification, we need the output to be a valid probability: a number between 0 and 1. Instead of predicting the class directly, we predict the probability of belonging to class 1:

$$P(y = 1 | x) = ?$$

The key insight of logistic regression is to pass the linear combination through the sigmoid function:

$$P(y = 1 | x) = \sigma(w^Tx + b) = \frac{1}{1 + e^{-(w^Tx + b)}}$$

Odds and Log-Odds Intuition

The mathematical beauty of logistic regression comes from the concept of odds:

1

Probability

Ranges from 0 to 1. Example: 0.8 probability of passing an exam.

$$P = 0.8$$
2

Odds

Ratio of success to failure. Ranges from 0 to infinity. Same example: 4:1 odds of passing.

$$\text{Odds} = \frac{P}{1-P} = \frac{0.8}{0.2} = 4$$
3

Log-Odds (Logit)

Take the logarithm of odds. Ranges from negative infinity to positive infinity. Now we can model it linearly!

$$\text{logit}(P) = \log\left(\frac{P}{1-P}\right) = w^Tx + b$$

The Key Insight

Logistic regression models the log-odds as a linear function of the features. The logit transformation converts a bounded probability (0,1) into an unbounded real number (-∞, +∞), which can be naturally expressed as a linear combination of features.

Decision Boundary

The decision boundary is the line (or hyperplane in higher dimensions) where the model is equally uncertain - where $P(y=1|x) = 0.5$.

Since $\sigma(z) = 0.5$ when $z = 0$, the decision boundary is defined by:

$$w^Tx + b = 0$$

This is a linear equation - which is why logistic regression produces linear decision boundaries despite being a nonlinear model.

Interactive Decision Boundary

Watch how the decision boundary separates two classes. Adjust the weight and bias sliders below.

03

Mathematical Formulation

The complete mathematical framework, derived step by step.

1. The Linear Model

We start with a linear combination of features:

$$z = w^Tx + b = w_1x_1 + w_2x_2 + \cdots + w_nx_n + b$$

Where $w = [w_1, w_2, \ldots, w_n]^T$ is the weight vector, $x = [x_1, x_2, \ldots, x_n]^T$ is the feature vector, and $b$ is the bias term.

2. The Sigmoid Function

We pass the linear combination through the sigmoid (logistic) function:

$$\sigma(z) = \frac{1}{1 + e^{-z}}$$

Interactive Sigmoid Curve

Adjust the slope parameter to see how the sigmoid changes shape.

Key Properties of Sigmoid:

$$\sigma(0) = 0.5$$

Midpoint is always at 0.5

$$\lim_{z \to \infty} \sigma(z) = 1$$

Approaches 1 for large positive inputs

$$\lim_{z \to -\infty} \sigma(z) = 0$$

Approaches 0 for large negative inputs

$$\sigma'(z) = \sigma(z)(1 - \sigma(z))$$

Beautiful derivative expressed in terms of itself

3. The Logistic Model

Combining the linear model with the sigmoid gives us the logistic regression model:

$$P(y = 1 | x) = \sigma(w^Tx + b) = \frac{1}{1 + e^{-(w^Tx + b)}}$$

And consequently:

$$P(y = 0 | x) = 1 - \sigma(w^Tx + b) = \frac{e^{-(w^Tx + b)}}{1 + e^{-(w^Tx + b)}}$$

4. Odds

The odds of an event is the ratio of the probability of the event occurring to the probability of it not occurring:

$$\text{Odds} = \frac{P(y=1|x)}{1 - P(y=1|x)} = \frac{P}{1-P}$$

If we substitute our logistic model, something beautiful happens:

$$\frac{P}{1-P} = \frac{\frac{1}{1+e^{-z}}}{\frac{e^{-z}}{1+e^{-z}}} = \frac{1}{e^{-z}} = e^z = e^{w^Tx + b}$$

5. Log-Odds (Logit Function)

Taking the natural logarithm of the odds:

$$\text{logit}(P) = \log\left(\frac{P}{1-P}\right) = w^Tx + b$$

Why the Logit Makes it Linear

The logit function is the inverse of the sigmoid function. By applying it to the probability, we transform the nonlinear relationship back into a linear one. This is why logistic regression is called a generalized linear model - the log-odds are a linear function of the features, even though the probabilities themselves are not.

04

Loss Function

Deriving the Binary Cross-Entropy loss from Maximum Likelihood Estimation.

Why MSE Fails for Classification

In linear regression, we minimize the Mean Squared Error (MSE):

$$\text{MSE} = \frac{1}{n}\sum_{i=1}^{n}(y_i - \hat{y}_i)^2$$

If we try using MSE with the sigmoid output, we get a non-convex loss surface with many local minima. Gradient descent would get stuck and fail to find the optimal solution.

Bernoulli Likelihood

Since our output is binary (0 or 1), each data point follows a Bernoulli distribution:

$$P(y | x) = p^y \cdot (1-p)^{1-y}$$

Where $p = \sigma(w^Tx + b)$ is the predicted probability. When $y = 1$, this gives $p$. When $y = 0$, this gives $1-p$.

Maximum Likelihood Estimation (MLE)

We want to find the weights that make our observed data most probable. The likelihood of the entire dataset (assuming independent samples) is:

$$L(w, b) = \prod_{i=1}^{n} p_i^{y_i} \cdot (1-p_i)^{1-y_i}$$

Log-Likelihood

Products are numerically unstable and hard to optimize. Taking the logarithm converts the product to a sum:

$$\ell(w, b) = \log L(w, b) = \sum_{i=1}^{n} \left[ y_i \log(p_i) + (1-y_i)\log(1-p_i) \right]$$

Negative Log-Likelihood (Binary Cross-Entropy)

Since we want to minimize a loss (convention in optimization), we negate the log-likelihood:

$$\mathcal{L}(w, b) = -\frac{1}{n}\sum_{i=1}^{n} \left[ y_i \log(p_i) + (1-y_i)\log(1-p_i) \right]$$

This is the Binary Cross-Entropy (BCE) Loss, also known as the Log Loss.

Understanding the Loss Intuitively

When y = 1:
$$\mathcal{L} = -\log(p)$$

If we predict $p = 0.99$ (very confident and correct), loss = 0.01 (small). If we predict $p = 0.01$ (very confident but wrong), loss = 4.6 (huge penalty!).

When y = 0:
$$\mathcal{L} = -\log(1-p)$$

If we predict $p = 0.01$ (correctly predicting class 0), loss is small. If we predict $p = 0.99$ (wrongly predicting class 1), loss is huge.

Convexity of BCE Loss

The Binary Cross-Entropy loss is convex with respect to the weights. This means there is a single global minimum, and gradient descent is guaranteed to find it (with proper learning rate).

3D Loss Surface Visualization

Drag to rotate. The bowl shape confirms convexity - there is only one minimum.

05

Closed Form vs Numerical Optimization

Why logistic regression requires iterative optimization.

No Closed-Form Solution

Closed-Form Solution

In linear regression, we can solve directly:

$$w = (X^TX)^{-1}X^Ty$$

But in logistic regression, the sigmoid function makes the equation nonlinear in the weights. When we try to set the gradient to zero:

$$\nabla \mathcal{L} = 0$$

We cannot isolate w analytically because the sigmoid introduces exponential terms that cannot be algebraically inverted when combined with the sum over all data points.

Numerical Optimization

Instead, we use iterative methods that take small steps toward the optimal solution:

Gradient Descent
$$w := w - \eta \nabla \mathcal{L}$$

Simple, works well for large datasets

Stochastic Gradient Descent (SGD)

Uses one sample at a time, faster per iteration

Newton's Method
$$w := w - H^{-1}\nabla \mathcal{L}$$

Uses second-order info (Hessian), converges faster

IRLS (Iteratively Reweighted Least Squares)

Reformulates as weighted least squares at each step

06

Optimization Working

Step-by-step gradient derivation and convergence analysis.

Gradient Derivation

Let us derive the gradient of the Binary Cross-Entropy loss with respect to the weights. This is where the beautiful derivative of the sigmoid pays off.

Step-by-Step Derivation

Step 1: Start with the loss for one sample:
$$\mathcal{L}_i = -[y_i \log(p_i) + (1-y_i)\log(1-p_i)]$$
Step 2: where $p_i = \sigma(z_i)$ and $z_i = w^Tx_i + b$
Step 3: Apply chain rule: $\frac{\partial \mathcal{L}_i}{\partial w} = \frac{\partial \mathcal{L}_i}{\partial p_i} \cdot \frac{\partial p_i}{\partial z_i} \cdot \frac{\partial z_i}{\partial w}$
Step 4: Compute each part:
$$\frac{\partial \mathcal{L}_i}{\partial p_i} = -\frac{y_i}{p_i} + \frac{1-y_i}{1-p_i}$$
$$\frac{\partial p_i}{\partial z_i} = p_i(1 - p_i)$$
$$\frac{\partial z_i}{\partial w} = x_i$$
Step 5: Multiply and simplify:
$$\frac{\partial \mathcal{L}_i}{\partial w} = \left(-\frac{y_i}{p_i} + \frac{1-y_i}{1-p_i}\right) \cdot p_i(1-p_i) \cdot x_i$$
$$= (-y_i(1-p_i) + (1-y_i)p_i) \cdot x_i$$
$$= (p_i - y_i) \cdot x_i$$
Final Result:
$$\nabla_w \mathcal{L} = \frac{1}{n}\sum_{i=1}^{n}(p_i - y_i) \cdot x_i$$

Elegance of the Result

The gradient has an incredibly simple form: it is the prediction error $(p_i - y_i)$ multiplied by the input features $x_i$, averaged over all samples. Larger errors lead to larger weight updates, and the update direction is determined by the features.

The Update Rule

$$w := w - \eta \cdot \frac{1}{n}\sum_{i=1}^{n}(\sigma(w^Tx_i + b) - y_i) \cdot x_i$$
$$b := b - \eta \cdot \frac{1}{n}\sum_{i=1}^{n}(\sigma(w^Tx_i + b) - y_i)$$

Where $\eta$ is the learning rate.

Convexity and Global Minimum

The BCE loss is convex because the Hessian matrix (matrix of second derivatives) is positive semi-definite:

$$H = \frac{1}{n}X^TSX$$

Where $S$ is a diagonal matrix with $S_{ii} = p_i(1-p_i) > 0$. Since $S$ is positive definite and $X^TSX$ is a product of the form $A^TDA$, the Hessian is positive semi-definite.

This guarantees a unique global minimum that gradient descent will find.

Learning Rate Effects

Gradient Descent Animation

Watch gradient descent optimize the loss. Adjust the learning rate to see how it affects convergence.

Too Small (0.001)

Very slow convergence. May take thousands of iterations to reach the minimum.

Just Right (0.01 - 0.1)

Smooth, efficient convergence. The sweet spot for most problems.

Too Large (1.0+)

Overshoots the minimum, may oscillate or diverge entirely.

07

Interactive Visualization Lab

Hands-on interactive components to build deep intuition.

2D Classification Playground

Click on the canvas to add data points (left-click for class 0, right-click for class 1). The decision boundary updates in real-time as the model trains.

Weights: w1=0.00, w2=0.00 Bias: 0.00 Loss: -- Accuracy: --

3D Decision Boundary

Visualize the logistic regression surface in 3D. The sigmoid creates a smooth probability surface over the 2D feature space.

Animated Loss Curve

Watch how the loss decreases during training iterations.

08

Advantages & Disadvantages

When to use logistic regression and when to look for alternatives.

Advantages

Probabilistic Interpretation

Unlike many classifiers, logistic regression directly outputs calibrated probabilities. You know not just the prediction but how confident the model is.

Convex Optimization

The loss function is convex, guaranteeing that gradient descent finds the global optimum. No worrying about local minima.

Interpretable Coefficients

Each weight directly tells you how much that feature contributes to the log-odds. A one-unit increase in feature $x_j$ changes the log-odds by $w_j$.

Works Well for Linearly Separable Data

When the classes can be separated by a hyperplane, logistic regression performs excellently and efficiently.

Disadvantages

Cannot Model Non-Linear Boundaries

Without manual feature engineering (polynomial features, interactions), logistic regression can only create linear decision boundaries.

Sensitive to Outliers

Extreme values in the features can significantly influence the learned weights and decision boundary placement.

Assumes Linear Log-Odds

The fundamental assumption that log-odds are linear in features may not hold for complex real-world relationships.

Feature Independence Assumption

While it can handle correlated features, multicollinearity can make coefficient estimates unstable and hard to interpret.

09

From Beginner to Advanced

Extending logistic regression to more powerful frameworks.

Regularization (L1 & L2)

To prevent overfitting and improve generalization, we add a penalty term to the loss function:

L2 Regularization (Ridge)

$$\mathcal{L}_{L2} = \mathcal{L} + \frac{\lambda}{2}\sum_{j=1}^{n}w_j^2$$

Shrinks all weights toward zero but never exactly to zero. Good when all features are potentially relevant.

L1 Regularization (Lasso)

$$\mathcal{L}_{L1} = \mathcal{L} + \lambda\sum_{j=1}^{n}|w_j|$$

Can drive weights exactly to zero, performing automatic feature selection. Good for sparse models.

Multiclass Extension (Softmax Regression)

When we have more than two classes, we extend logistic regression using the softmax function:

$$P(y = k | x) = \frac{e^{w_k^Tx + b_k}}{\sum_{j=1}^{K} e^{w_j^Tx + b_j}}$$

This is also called Multinomial Logistic Regression. The loss function becomes the Categorical Cross-Entropy:

$$\mathcal{L} = -\sum_{i=1}^{n}\sum_{k=1}^{K} y_{ik}\log(p_{ik})$$

Connection to Neural Networks

Logistic regression is literally a single-layer neural network with a sigmoid activation:

Input Layer
x1
x2
x3
...
xn
w1, w2, ..., wn
Output
σ(wTx + b)

When we stack multiple layers of logistic units, we get a deep neural network. The building blocks are the same - logistic regression is the foundation upon which all of deep learning is built.

Connection to Maximum Entropy Models

Logistic regression can also be derived from the principle of maximum entropy. Among all probability distributions that satisfy the constraints imposed by the data, logistic regression chooses the one with the highest entropy (the least additional assumptions).

This is why logistic regression is sometimes called a Maximum Entropy Classifier (MaxEnt) in natural language processing.

Continue Your Journey