Recommended resources
The freely available resources this lesson will direct you to.
Introduction
In our previous lesson, we explored Bayesian inference and saw how it provides a complete posterior probability distribution for a parameter, such as a coin's bias . This distribution represents our full belief, including our uncertainty. However, in many practical scenarios, we need to commit to a single best estimate for that parameter.
This lesson introduces a powerful and widely used method for finding such an estimate: Maximum Likelihood Estimation (MLE). Instead of the Bayesian approach of finding the posterior , MLE asks a more direct question: "What value of the parameter makes the data we observed, , the most probable?" We will answer this question from first principles, culminating in a formal derivation of the MLE for the parameter of a Bernoulli distribution—the mathematical model for a single coin flip. This result is not just a statistical curiosity; it forms the bedrock of how we define and optimize many machine learning models.
The Principle of Maximum Likelihood
The core idea of MLE is to find the parameter values that maximize the likelihood of the observed data. To formalize this, we first define the likelihood function.
Given a dataset of independent and identically distributed (i.i.d.) samples drawn from a distribution with parameter(s) , the likelihood of the data is the joint probability of observing all these samples:
When we view this expression as a function of the parameter for a fixed dataset , we call it the likelihood function, often denoted as . The goal of MLE is to find the value of that maximizes this function. We use the notation to express this:
The notation ("theta-hat") signifies an estimate of the true parameter .
The following resource provides a clear introduction to this core concept.
These notes from Stanford's CS109 course clearly define the likelihood function and explain the logic behind maximizing it.
Please read the sections titled "Maximum Likelihood" and "Maximization". Pay close attention to the distinction between probability (a function of data) and likelihood (a function of parameters), and why the log function doesn't change the argmax.
From Likelihood to Log-Likelihood
Let's apply this to a concrete example: a sequence of coin flips, where each flip is a Bernoulli trial. Let's denote a "heads" outcome as and "tails" as . The parameter of our Bernoulli distribution is , the probability of getting heads, so .
The probability mass function (PMF) for a single Bernoulli trial can be written compactly as:
Verify for yourself that this expression gives for and for .
The likelihood function for i.i.d. trials is the product of the individual probabilities:
This product can be simplified by grouping the terms. If we have heads (where ) and tails (where ) in our dataset, the likelihood becomes:
Now, we face a practical challenge. Maximizing this function requires taking its derivative. Differentiating a product is cumbersome, and for large , the product of probabilities (which are less than 1) can become vanishingly small, leading to numerical underflow on a computer.
Here is a question for you: What common mathematical function transforms products into sums, and how might that help us here?
Answer
The logarithm. By taking the logarithm of the likelihood, we convert the product into a sum, which is much easier to differentiate. Also, sums of logarithms are numerically more stable than products of small numbers.This is the standard practice in MLE. We work with the log-likelihood function, denoted . Since the logarithm is a strictly monotonically increasing function, the value of that maximizes the likelihood also maximizes the log-likelihood.
Let's derive the log-likelihood for our Bernoulli case:
Using the properties of logarithms ( and ), we get:
This expression is much more tractable.
Deriving the Maximum Likelihood Estimate
We can now find the MLE by taking the derivative of the log-likelihood function with respect to , setting it to zero, and solving for .
Step 1: Differentiate with respect to .
Using the chain rule, we know that .
Step 2: Set the derivative to zero.
To find the value of that maximizes the function, we find the critical point where the gradient is zero.
Step 3: Solve for .
Finally, isolating gives us the Maximum Likelihood Estimate:
This elegant result confirms our intuition: the most likely value for the probability of heads is simply the proportion of heads observed in the data—the sample mean.
The following video and reading provide excellent, step-by-step walkthroughs of this exact derivation. Reviewing them will help solidify your understanding.
Maximum Likelihood Estimate for Bernoulli Distribution | Derivation and Implementation
This video provides a concise and clear derivation of the MLE for the Bernoulli distribution, mirroring the steps we just took.
Watch from the beginning up to the end of the derivation. The presenter clearly explains the setup, the use of log-likelihood, and the calculus involved. The final segment, which you can watch optionally, demonstrates this concept using TensorFlow, connecting the theory to a practical framework.
10-315 Notes Maximum Likelihood Estimation
These lecture notes from Carnegie Mellon University offer a rigorous yet clear presentation of MLE, including the reasons for using log-likelihood and a detailed derivation for the Bernoulli case.
First, quickly review the section on log-likelihood to reinforce the reasons for this crucial step. Then, study the detailed derivation in section 3.5.2, titled "Bernoulli". The notation might be slightly different (using indicator functions), but the logic and the final result are identical.
Conclusion
In this lesson, we established a cornerstone of statistical estimation and machine learning. We moved from the Bayesian concept of a full posterior distribution to the frequentist-style goal of finding a single best point estimate for a model parameter.
Key Takeaways:
- Maximum Likelihood Estimation (MLE) seeks the parameter value that maximizes the likelihood function .
- We use the log-likelihood for computational stability (avoiding underflow) and mathematical convenience (turning products into sums).
- Maximizing the log-likelihood is equivalent to maximizing the likelihood because the logarithm is a monotonically increasing function.
- For a Bernoulli distribution, the MLE of the success probability is the sample mean: the number of successes divided by the total number of trials ().
Preview of the Next Lesson:
The process of MLE involves defining and then optimizing an objective function—the log-likelihood. In machine learning, we more commonly frame optimization as minimizing a loss function. This is just a simple transformation: maximizing the log-likelihood is equivalent to minimizing the negative log-likelihood.
In our next lesson, we will make a critical connection: we will show that minimizing the negative log-likelihood for a Bernoulli model is mathematically identical to minimizing the binary cross-entropy loss, a function you will encounter constantly in training neural networks for classification tasks. This will be our first major bridge from a classical statistical principle to a core component of modern deep learning.