Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome to your next lesson on logistic regression.
In our last session, we conducted a deep dive into the logistic (or sigmoid) function, . We rigorously derived it from three different perspectives, concluding that it is the ideal function to model the conditional probability of a positive outcome, , by mapping the unbounded linear predictor to the required interval.
Today, we will build directly on that foundation. Our learning outcome is to formulate the logistic regression model from a probabilistic (Bernoulli) perspective. We will formalize the relationship between our binary outcome variable, , and the sigmoid function. This will allow us to construct a complete probabilistic model for a single data point and, from there, for our entire dataset. This formulation is the theoretical cornerstone upon which the optimization process, which we will study next, is built.
1. The Probabilistic Assumption: A Bernoulli Outcome
In binary classification, the target variable can only take on one of two values, which we label as 0 (for the negative class) and 1 (for the positive class). A random variable that can only result in one of two outcomes is perfectly described by the Bernoulli distribution.
Recall from your foundational studies in probability that a Bernoulli random variable has a single parameter, , which represents the probability of "success" (i.e., the outcome being 1). The probability mass function (PMF) is:
In the context of logistic regression, we are not dealing with a single, fixed probability . Instead, we hypothesize that the probability of the outcome depends on the input features . This is where our work from the previous lesson comes into play. We model the parameter of the Bernoulli distribution as the output of our logistic function:
Therefore, the core of the logistic regression model is the assumption that the response variable , conditioned on the features , follows a Bernoulli distribution whose success parameter is given by . This leads to the following two conditional probabilities for a single data point :
- The probability of the class being 1 is:
- The probability of the class being 0 is:
2. A Compact Formulation for the PMF
While the two separate equations above are correct, they are cumbersome to work with mathematically, especially when we want to write a function for the entire dataset. Fortunately, there is an elegant way to combine them into a single expression.
Locally Weighted & Logistic Regression | Stanford CS229: Machine Learning - Lecture 3 (Autumn 2018)
In this classic lecture segment from Stanford's CS229 course, Andrew Ng explains exactly how to formulate the logistic regression model probabilistically and derive the compact one-line equation for the probability of an outcome.
Watch the video from 50:22 to 55:53. Focus on how the assumptions about P(y=1) and P(y=0) are combined into a single, powerful equation.
As explained in the video, because our label can only be 0 or 1, we can write a single equation for the probability of observing a specific outcome given :
This equation is a concise representation of the Bernoulli PMF where the parameter is . Let's verify this for ourselves, which is a crucial step in ensuring rigorous understanding:
-
Case 1: The true label is
Plugging into the equation gives:
This correctly retrieves our first probability rule. -
Case 2: The true label is
Plugging into the equation gives:
This correctly retrieves our second probability rule.
This compact form is not just a notational convenience; it is the mathematical object we will use to build the objective function for training the entire model.
Test your understanding!
Suppose you have a single training example where (including a bias term) and the true label is . Your current model parameters are .
- Calculate the linear predictor .
- Calculate the predicted probability . Use .
- Using the compact formula , calculate the probability of observing the label given and .
Show answer
-
Linear predictor:
-
Predicted probability:
The model is currently completely uncertain for this input. -
Probability of observing :
Using the formula with and :
The probability of observing the outcome given the features and the current (untrained) parameters is 0.5.
3. From a Single Point to the Entire Dataset: The Likelihood Function
We have successfully formulated the probability for a single data point. The goal of training a model, however, is to find parameters that are "best" for the entire training dataset .
The principle we will use is Maximum Likelihood Estimation (MLE), which you have encountered in Module 8. The core idea of MLE is to choose the parameters that maximize the probability of observing the data that we have actually observed. This probability is called the likelihood function, .
To construct this function, we make the standard assumption that our training examples are independent and identically distributed (i.i.d.). The "independent" part is key here: it means the probability of the entire set of outcomes is the product of the probabilities of each individual outcome.
The following resource provides a formal, academic description of this step.
This document from Carnegie Mellon University, titled 'Logistic Regression', offers a formal derivation. We will focus on the section that constructs the likelihood function from the individual Bernoulli trial probabilities.
Read the second half of section 12.1, starting from 'Assume that Pr(Y = 1|X = x) = p(x;θ)...' to the end of the section. Focus on how Equation 12.1 is constructed by taking the product of the probabilities for each independent observation.
Following the logic from the text, the likelihood of the parameters given the dataset is the product of the individual probabilities for each data point :
Now, we substitute our compact Bernoulli PMF into this product:
This equation, the likelihood function, is the complete probabilistic formulation of the logistic regression model for a given dataset. It quantifies how "likely" our observed labels are for a given set of parameters . Our goal in training will be to find the that makes this likelihood as large as possible.
Conclusion
In this lesson, we have formally defined the logistic regression model from a rigorous probabilistic standpoint.
Key Takeaways:
- Logistic regression models the binary outcome as a Bernoulli random variable.
- The parameter of this Bernoulli distribution (the probability of success, ) is not constant; it is modeled by the sigmoid function applied to a linear combination of features: .
- The probability mass function for a single observation can be written in the compact form: .
- Assuming i.i.d. data, the likelihood function for the entire dataset is the product of these individual probabilities, giving us a complete objective function to be maximized.
Preview of the Next Lesson:
We have successfully derived the likelihood function . In our next lesson, we will tackle the problem of maximizing it. You will find that working with a product of many terms is computationally difficult and numerically unstable. We will see that maximizing the likelihood is equivalent to maximizing the log-likelihood, which converts the product into a sum. This will lead us directly to the derivation of the cross-entropy loss function, the standard cost function for logistic regression.