Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Logistic Function: Derivation and Properties for Probability Modeling

Hello! Welcome to your next lesson on logistic regression.

In our previous lesson, we mathematically formulated the binary classification problem. We concluded that our goal is to model the conditional probability . This presents a challenge: our familiar linear predictor, , can output any real number, but a probability must be confined to the interval .

Today, we will address this challenge head-on. Our learning outcome is to derive the logistic (sigmoid) function and explain its properties for probability modeling. We won't just introduce this function; true to your goal of building a deep theoretical understanding, we will derive it from first principles from three different but related perspectives. This will demonstrate why this specific functional form is not an arbitrary choice but a mathematically sound and natural one for classification.

1. The Need for a "Squashing" Function

Let's begin with a quick review of why we need a new function in the first place. Linear regression is unsuitable for classification because its output is unbounded. We need a way to link the linear predictor to a valid probability.

Logistic Regression - THE MATH YOU SHOULD KNOW!

This short video provides a concise recap of why linear regression fails for classification and introduces the core idea of logistic regression: modeling probability.

Watch the segment from 00:23 to 02:39. Pay attention to the argument against linear regression and the introduction of odds and log-odds as a way to model probability.

The video introduces the idea of modeling the log-odds of an event. This concept is central to our first derivation of the function we need.

The function used for this purpose is called the logistic function, more commonly known in machine learning as the sigmoid function. Its formula is:

Here, would be our linear predictor, .

Graph of the Sigmoid Function
The graph of the sigmoid function, \(\sigma(z)\). It takes any real-valued input \(z\) on the x-axis and maps it to a value between 0 and 1 on the y-axis, making it ideal for modeling probabilities.

The sigmoid function clearly "squashes" the entire real number line into the interval. But there are infinitely many functions that can do this. Why this one specifically? The answer lies in its elegant mathematical properties and its deep connection to probability theory.

2. Derivation 1: From Log-Odds (The Logit)

The most direct way to derive the sigmoid function is by making a simple, powerful assumption about the relationship between our features and the probability of the outcome.

Let be the probability of the positive class.

  1. Odds: The odds of an event is the ratio of the probability that it occurs to the probability that it does not:

    While probability is in , the odds are in .

  2. Log-Odds (Logit): By taking the natural logarithm of the odds, we get the logit function:

    The logit function transforms a probability from to the entire real number line . This is exactly the range of our linear predictor !

  3. The Logistic Regression Model Assumption: The core assumption of logistic regression is that the log-odds of the outcome are a linear function of the features:

  4. Solving for p: Now, we can algebraically solve for to find the function that gives us the probability.





    By dividing the numerator and denominator by , we get the familiar form:

This derivation shows that if we model the log-odds as a linear combination of features, the probability must follow the sigmoid function. The logit function and the sigmoid function are inverses of each other.

Logit and Inverse Logit (Sigmoid) Functions
This image illustrates the inverse relationship. The logit function (left) maps a probability \(p\) to a real number. The logistic (sigmoid) function (right) maps that real number back to the probability \(p\).

3. Derivation 2: From a Generative Perspective

The first derivation was based on a modeling assumption. But is that assumption justified? A deeper derivation shows that the sigmoid function naturally arises from specific, and quite reasonable, probabilistic assumptions about the data itself.

This approach involves building a generative model. Instead of modeling directly (a discriminative approach), we will model the class-conditional distributions and the class priors , and then use Bayes' theorem to find the posterior .

The following article provides a superb, rigorous walkthrough of this derivation. It's exactly the kind of fundamental reasoning you are seeking in this course.

Why Do We Use Sigmoid in Logistic Regression?

This article, titled 'Why Do We Use Sigmoid in Logistic Regression?', provides a detailed, from-scratch derivation of the sigmoid function by assuming Gaussian class-conditional distributions.

Read the article starting from the section 'Why Sigmoid?' down to (but not including) the subsection 'Discriminative and Generative models'. Follow the mathematical steps that start with two Gaussian distributions and end with the sigmoid function. This is the core of the lesson.

Let's summarize the key insight from the article:

  • Assumption: We assume the data for each class is generated from a multivariate Gaussian distribution with mean and, critically, a shared covariance matrix . That is, .
  • Bayes' Theorem: We use Bayes' theorem to find the posterior probability :
  • Derivation: By substituting the PDF of the Gaussian distributions into this formula and simplifying, many of the complex quadratic terms cancel out precisely because the covariance is shared. The resulting expression for simplifies to the sigmoid function, where the argument is a linear function of .

This generative story provides a powerful justification for the logistic regression model. It shows that if you believe your classes are roughly Gaussian-shaped clusters with similar spread, then the resulting posterior probability of class membership will be logistic.

4. Derivation 3: The Generalized Linear Model (GLM) Framework

Our final derivation is the most abstract and places logistic regression within a broader family of statistical models called Generalized Linear Models (GLMs). This perspective is invaluable for understanding the deep connections between many models you've seen (like linear regression) and those you will see in the future.

A GLM has three components:

  1. Random Component: A probability distribution for the response variable from the exponential family (e.g., Normal, Bernoulli, Poisson).
  2. Systematic Component: A linear predictor .
  3. Link Function: A function that links the expected value of the response, , to the linear predictor: .

For logistic regression, the response is binary (0 or 1), so the natural choice for the random component is the Bernoulli distribution, where .

The following document provides the formal derivation.

logistic regression and generalized linear models

These lecture notes from a Harvard course on data science formally define Generalized Linear Models and show how logistic regression is a specific case.

Please read 'Proposition 6.4' and its proof on page 7. This proof demonstrates that the logit function is the 'canonical' link function for the Bernoulli distribution.

The proof in Proposition 6.4 shows that when the Bernoulli probability mass function is written in the standard form for the exponential family:

The term multiplying is called the natural parameter, . Here, we see that , which is precisely the logit function.

The canonical link function is the link function that equates the natural parameter with the linear predictor: . Therefore, for a Bernoulli response, the canonical (most "natural") link is:

This brings us back to the exact same starting point as our first derivation, but this time it's justified by the powerful and general theory of GLMs.

5. Properties for Probability Modeling

Having rigorously derived the sigmoid function, let's summarize its key properties that make it perfect for modeling :

  • Range [0, 1]: It maps any real-valued input to a value between 0 and 1, ensuring the output can be interpreted as a probability.
  • Monotonicity: The function is strictly increasing. This means that if the linear predictor increases, the predicted probability also increases. This is an intuitive and desirable property.
  • Linear Log-Odds: The inverse of the sigmoid is the logit function. This means the linear part of the model, , has a direct interpretation as the log-odds of the event occurring.
  • Differentiability: The sigmoid function is differentiable everywhere. Its derivative is simple and can be expressed in terms of the function's output itself:

    This property is not just a mathematical curiosity; it will be absolutely crucial when we derive the gradient descent algorithm to train our logistic regression model in a future lesson.
Test your understanding!

In the generative derivation, we assumed the class-conditional distributions were Gaussian with a shared covariance matrix . What do you think would happen to the form of the posterior probability if we allowed each class to have its own, different covariance matrix ? Would the decision boundary still be linear?

Show answer

If the covariance matrices are different (), the quadratic terms in the exponents of the Gaussian PDFs would no longer cancel out when you form the log-odds ratio. The argument of the function that determines the posterior probability would now contain quadratic terms in (e.g., terms like and ).

As a result, the decision boundary, where , would become a quadratic surface instead of a linear one (a hyperplane). This method is known as Quadratic Discriminant Analysis (QDA), as opposed to Linear Discriminant Analysis (LDA), which arises from the equal-covariance assumption.

Conclusion

In this lesson, we have moved beyond simply stating the sigmoid function and have rigorously derived it from multiple viewpoints.

Key Takeaways:

  • The logistic (sigmoid) function, , provides the necessary link between a linear predictor and a probabilistic outcome.
  • It can be derived algebraically by assuming the log-odds of the event are linear in the features.
  • It can be derived probabilistically by assuming the data for each class comes from a Gaussian distribution with a shared covariance matrix and applying Bayes' theorem.
  • It can be derived formally from the Generalized Linear Model framework, where it emerges as the canonical link function for the Bernoulli distribution.
  • Its properties (range, monotonicity, differentiability, and interpretability via log-odds) make it the ideal function for binary probability modeling in this context.

Preview of the Next Lesson:
Now that we have a function to model the probability of a single observation, , our next task is to define an objective function for the entire dataset. In the next lesson, we will use the principle of Maximum Likelihood Estimation to derive the log-likelihood function for logistic regression, which you will recognize as the cross-entropy loss function. This will be the function we aim to optimize to find the best model parameters .

Can't find a good explanation? Sign up and we'll make it for you

Sign up