Recommended resources
The freely available resources this lesson will direct you to.
Hello! Let's continue our exploration of logistic regression.
In our last lesson, we established the probabilistic foundation of the model. We saw that logistic regression assumes each binary label follows a Bernoulli distribution, and we constructed the overall likelihood function by taking the product of the probabilities for each data point. This function, , represents the probability of observing our entire dataset for a given set of parameters .
Today, we will address the learning outcome: to derive the log-likelihood function (cross-entropy loss) for logistic regression. We will transform the likelihood function into a more mathematically tractable form, the log-likelihood, and then reframe our optimization goal from maximization to minimization, which will lead us directly to one of the most important cost functions in machine learning.
1. From Products to Sums: The Need for Logarithms
Our current objective is to find the parameters that maximize the likelihood function:
Attempting to maximize this function directly presents two major challenges:
- Computational Instability: The term is a probability, a value between 0 and 1. Multiplying many such numbers together results in an extremely small value that can easily become zero due to floating-point precision limitations in computers (a problem known as numerical underflow). This makes comparing the likelihood of different values impossible.
- Mathematical Inconvenience: Finding the maximum of a function typically involves taking its derivative and setting it to zero. The derivative of a long product is very complex to compute (requiring repeated application of the product rule). In contrast, the derivative of a sum is simple.
To solve both problems, we can work with the logarithm of the likelihood instead. The natural logarithm, , is a strictly monotonically increasing function. This means that if , then . Consequently, the value of that maximizes will also be the value that maximizes .
The following video provides an excellent intuitive explanation of this transition.
Logistic Regression Part 4 | Loss Function | Maximum Likelihood | Binary Cross Entropy
In the video 'Logistic Regression Part 4 | Loss Function', the presenter clearly explains the practical limitations of maximizing the likelihood directly and motivates the use of logarithms to transform the problem into a more stable and manageable form.
Watch from 10:32 to 18:00. Pay close attention to the discussion on why multiplying many small probabilities is problematic and how taking the logarithm converts the product into a sum. Also, note the introduction of the negative sign to frame the problem as a minimization task.
2. Formal Derivation of the Log-Likelihood Function
Now, let's perform the formal derivation. We define the log-likelihood function, denoted , as the natural logarithm of the likelihood function .
Using the fundamental properties of logarithms, specifically and , we can simplify this expression.
-
The logarithm of a product becomes the sum of the logarithms:
-
The logarithm of a product within the sum becomes a sum of logarithms:
-
The exponents can be brought down as multipliers:
This final expression is the log-likelihood function for logistic regression. Our goal is now to find the parameters that maximize this sum, a much more manageable task.
For a rigorous, academic presentation of this derivation, please refer to the following text.
The document 'Logistic Regression' from Carnegie Mellon University provides a concise and formal derivation of the log-likelihood function. This will solidify your understanding of the mathematical steps we just outlined.
Read the beginning of section 12.2.1, 'Likelihood Function for Logistic Regression'. Focus on the transformation from the likelihood function in Equation 12.6 to the log-likelihood function in Equation 12.7. This maps directly to the steps we just performed.
3. From Maximization to Minimization: The Binary Cross-Entropy Loss
By convention, most optimization algorithms in machine learning, including gradient descent, are formulated as minimizing a cost function (also called a loss or error function).
Maximizing a function is equivalent to minimizing its negative. Therefore, we can reframe our goal of maximizing the log-likelihood as minimizing the negative log-likelihood, .
We define our cost function as the average negative log-likelihood over the training set. We average by dividing by the number of training examples, , which makes the cost independent of the dataset size but does not change the location of the minimum.
This function, , is the canonical cost function for logistic regression. It has a specific name: Binary Cross-Entropy Loss, or sometimes Log Loss.
Let's examine the contribution of a single data point to this cost:Cost(h_θ(x), y) = -[y log(h_θ(x)) + (1-y) log(1-h_θ(x))]
- If the true label is , the cost becomes
-log(h_θ(x)). As our predictionh_θ(x)approaches 1 (correct), the cost-log(h_θ(x))approaches 0. Ash_θ(x)approaches 0 (incorrect), the cost-log(h_θ(x))approaches . The model is heavily penalized for being confidently wrong. - If the true label is , the cost becomes
-log(1 - h_θ(x)). As our predictionh_θ(x)approaches 0 (correct),1-h_θ(x)approaches 1, and the cost-log(1-h_θ(x))approaches 0. Ash_θ(x)approaches 1 (incorrect),1-h_θ(x)approaches 0, and the cost-log(1-h_θ(x))approaches .
The following resource provides a clear derivation of this final cost function and its interpretation.
The Stanford CS109 handout, 'Logistic Regression', offers another clean derivation of the log-likelihood and connects it directly to the optimization objective.
Read the short section titled 'Log Likelihood'. It shows the derivation of the same log-likelihood equation we found, confirming our result from a second source. This reinforces the core derivation.
Test your understanding!
Given a single training example where the true label is , you have two models, A and B.
- Model A predicts .
- Model B predicts .
Calculate the cross-entropy loss for both models for this single example. Which model is better according to this loss function, and why does this make sense? Use as the natural logarithm.
Show answer
The formula for the loss of a single example is: -[y log(h_θ(x)) + (1-y) log(1-h_θ(x))].
Since , this simplifies to -log(h_θ(x)).
-
Loss for Model A:
Loss_A = -log(0.9) ≈ -(-0.105) = 0.105 -
Loss for Model B:
Loss_B = -log(0.6) ≈ -(-0.511) = 0.511
The loss for Model A is significantly lower than for Model B. This makes perfect sense because the true label is 1, and Model A was more confident in the correct outcome (0.9) than Model B (0.6). The cross-entropy loss function correctly penalizes the less confident model more heavily.
Conclusion
Today we made the crucial transition from a probabilistic statement (the likelihood) to a concrete optimization objective (the cost function).
Key Takeaways:
- Maximizing the likelihood function is problematic due to numerical underflow and mathematical complexity.
- Since the logarithm is a monotonic function, maximizing is equivalent to maximizing the log-likelihood function .
- Taking the logarithm transforms the product over data points into a more manageable sum.
- By convention, we frame optimization as a minimization problem. Therefore, we minimize the negative log-likelihood.
- The final cost function for logistic regression is the average negative log-likelihood, known as the Binary Cross-Entropy Loss:
Preview of the Next Lesson:
We have successfully derived the cost function that we need to minimize. The next logical step is to find the values of that achieve this minimum. In the next lesson, we will derive the gradient of this objective function, . This gradient will be the engine that powers the gradient descent algorithm, allowing us to iteratively update our parameters and "learn" the optimal logistic regression model.