Understanding Maximum Likelihood Estimation
Tags: machinelearning, statistics, maximumlikelihood, mathematics
Published: 2021-10-06 19:46:29

---
title: Understanding Maximum Likelihood Estimation
published: true
date: 2020-08-10 02:59:30 UTC
tags: machinelearning,statistics,maximumlikelihood,mathematics
canonical_url: https://medium.com/@Polaris000/understanding-maximum-likelihood-estimation-e63dff65e5b1
---

![](https://cdn-images-1.medium.com/max/1024/1*PFPNg3jngAjU8j_RQnO9aw.jpeg)

**Maximum Likelihood Estimation** , or MLE, for short, is the process of estimating the parameters of a distribution that maximize the likelihood of the observed data belonging to that distribution.

Simply put, when we perform MLE, we are trying to **find the distribution that best fits our data.** The resulting value of the distribution’s parameter is called the **maximum likelihood estimate.**

MLE is a very prominent frequentist technique. Many conventional machine learning algorithms work with the principles of MLE. For example, the best-fit line in linear regression calculated using least squares is identical to the result of MLE.

### The likelihood function

Before we move forward, we need to understand the likelihood function.

The likelihood function helps us find the best parameters for our distribution. It can be defined as shown:

![](https://cdn-images-1.medium.com/max/598/1*r90eozWc7pjdxOhjgH52mw.png)

where **_θ_** is the parameter to maximize, **_x\_1, x\_2, … x\_n_** are observations for **n** random variables from a distribution and **_f_** is the joint density function of our distribution with the parameter _θ_.

The pipe (“ | “) is often replaced by a semi-colon, since _θ_ isn’t a random variable, but an unknown parameter.

Of course, _θ_ could also be a set of parameters.

![](https://cdn-images-1.medium.com/max/481/1*Z7D0XQ0L7N0NYZa_hswerg.png)

For example, in the case of a normal distribution, we would have   
_θ = (μ,σ)_, with μ and σ representing the two parameters of our distribution.

### Intuition

**Likelihood** is often interchangeably used with probability, but they are not the same. Likelihood is not a probability density function, meaning that integrating over a specific interval would not result in a “probability” over that interval. Rather, it talks about how likely a distribution with certain values for its parameters fits our data.

![](https://cdn-images-1.medium.com/max/1024/1*OfxcaYeexRFT9SiOSoeB4Q.jpeg)<figcaption><em>θ_MLE is the value that maximizes the likelihood of our data x</em></figcaption>

Looking at it this way, we can say that likelihood is how likely the distribution fits of given data for variable values of its parameters. So, if _L(θ\_1|x)_ is greater than _L(θ\_2|x)_, the distribution with parameter value as _θ\_1_ fits our data better than the one with a parameter value of _θ\_2._

### Process

To re-iterate, we’re looking for the parameter (or parameters, as the case may be) that maximize our likelihood function. How do we do that?

To simplify our calculations, lets assume that our data is **independently and identically distributed** , or i.i.d, for short, meaning that observations are independent of each other and that they can be quantified in the same way, which basically means that all points are from the same distribution.

The i.i.d assumption allows us to easily calculate the cumulative likelihood considering all data points as a product of individual likelihoods.

Also, most likelihood functions have a single maxima, allowing us to simply equate the derivate to 0 to get the value of our parameter. If multiple maxima exist, we would need to look at the global maxima to get our answer.

In general, more complex numerical methods would be required to find the maximum likelihood estimate.

### An Example

To understand the math behind MLE, let’s try a simple example. We’ll derive the value of the **exponential distribution** ’s parameter corresponding to the maximum likelihood value.

#### The Exponential Distribution

The exponential distribution is a continuous probability distribution used to measure inter-event time.

It has a single parameter, called **λ** by convention. λ is called  **rate.**

Its **mean** and **variance** is _1/ λ_ and _1 / λ²,_ respectively.

The **probability density function** for the exponential distribution is as shown below.

![](https://cdn-images-1.medium.com/max/639/0*PSmW16GwuFfn2-Tf.jpg)

![](https://cdn-images-1.medium.com/max/382/1*i74ZYpgdxhwFed5eH9_DKw.png)

![](https://cdn-images-1.medium.com/max/752/1*v4F-0RLZGil-do2IiXxSwA.png)<figcaption>PDF plots with variable λ</figcaption>

There’s a single parameter _λ_. Let’s calculate its value, given n random points x\_1 to x\_n.

As discussed earlier, we know that the **likelihood** for a given point xi is given by the following:

![](https://cdn-images-1.medium.com/max/592/1*2qEje_VyqWgYBmNxj-v5XA.png)

We calculate the likelihood for each of our n points.

![](https://cdn-images-1.medium.com/max/327/1*KNphGWrNpSNGBk0cnbkzhA.png)

The combined likelihood for all n points would just be the product of their individual likelihoods, since we are considering independent and identically distributed points.

![](https://cdn-images-1.medium.com/max/789/1*AMda9GmZskFl9tMD-_6_uQ.png)

#### The log-likelihood

Our next step would be to find the derivative of our likelihood function and set it to 0, since we want to find the value of our distribution parameter (in this case, _λ_) which gives the maximum likelihood.

Since the derivatives of both functions and those of their logarithms have the same **stationary points** (derivative equates to 0), we can simplify our calculations by considering the logarithm of our likelihood function.

Lets plot a simple graph that represents the following equations when a and b are both set to 1. The rate parameter, _λ,_ has been replaced by x.

![](https://cdn-images-1.medium.com/max/541/1*LcfDP_g3D-E6kipohOH3WQ.png)

The terms a and b represent two datapoints, say, x\_1 and x\_2. Our likelihood function is represented by the orange curve. It is the product of likelihoods of the two individual datapoints.

The logarithm of the likelihood function, or the **log-likelihood** , is represented by the pink curve.

![](https://cdn-images-1.medium.com/max/968/1*LhrfOJtNI-OQbhj3195XgA.png)<figcaption>The likelihood and the log-likelihood function for our points x_1 and x_2.</figcaption>

The blue-dotted line will be covered later.

Two things to notice:

- Both the likelihood function (orange) and its logarithm (pink) have the same stationary point (the derivative is 0).
- That common stationary point occurs at the x=1 (blue curve), which may not make much sense at the moment, is basically a simple proof of our result. We’ll revisit this point after obtaining our result.

![](https://cdn-images-1.medium.com/max/767/1*8-YAJaYLrDbeYkd-VmIj0w.png)

The product of small probabilities, as is the case in calculating the likelihood over several data points, can also lead to numerical underflow due to very small probabilities, giving us another reason to prefer working with the “sum of logs” rather than “products”.

![](https://cdn-images-1.medium.com/max/869/1*v6qJVLQl2YiJ5Ub3g9d_Dw.png)

Simplifying our result, we get:

![](https://cdn-images-1.medium.com/max/419/1*8G_2Lq3hteWzvbkXlZ-t5g.png)

This is the log-likelihood for the exponential distribution.

#### The derivative

Now that we have our log-likelihood function, lets find its maxima. To do this, we simply find its first derivative with respect to λ.

Differentiating _log (L(λ)),_ we get:

![](https://cdn-images-1.medium.com/max/536/1*iEIaN1h6An6NwYnirF13nA.png)

Following that, we end up with:

![](https://cdn-images-1.medium.com/max/344/1*46ZwD4CG9u_jC5v53KoWeA.png)

Simplying this further, we get the following relation for λ:

![](https://cdn-images-1.medium.com/max/494/1*p8qUyW83UdUgXpgKUCuJmw.png)

So the value of λ that maximizes likelihood can be calculated using the above relation. Similar calculations can be done for other continuous and even discrete distributions.

![](https://cdn-images-1.medium.com/max/968/1*LhrfOJtNI-OQbhj3195XgA.png)

Now, coming back to our graph example, the blue dotted line, with the equation: _x = 2 / (a + b),_ is our value for λ when n is 2.

Remember, the value of λ we obtained represents the maximum value of likelihood function (orange curve). For a = 1 and b = 1, we get x = 1 and hence λ = 1, which is shown in the graph as the maxima of the orange curve.

In distributions with multiple parameters, like the normal distribution, we consider each one in turn, keeping the others constant.

### Conclusion

MLE isn’t the only technique which helps us do this. Other techniques include

- **Maximum A Priori Estimation (MAP),** which uses prior data as well, unlike MLE, which considers a uniform prior; and
- **Expectation Maximization,** which handles latent variables (those unobservable variables which have an effect on present variables), something that MLE struggles with.

There’s a lot more to Maximum Likelihood Estimation — and for that matter, other parameter estimation techniques. This post focuses more on the underlying math behind MLE using the exponential distribution as an example.

Hopefully, this article gets you started with other, more complex techniques!

Of course, please let me know in the comments if anything’s unclear and I’ll update the post. Thanks for reading!