Maximum Likelihood Estimation (MLE) is an important mathematical tool used frequently in statistical modeling. IT also provides the foundation for Machine Learning (ML) and Deep Learning (DL) models. Solving for the MLE provides insights into how ML models learn and what the best it is for them to learn.
In this blog, we will explore MLE by breaking down the mathematical concepts behind it, and build a theoretical foundation for training ML models.
We will look at 3 models and analyze why and how the MLE guides us in parameter estimation:
Binomial Distribution
Linear Regression Model
Logistic Regression Model
What is MLE?
In probability theory, we use a probability distribution to make inferences about data. Often, we have large amounts of data but cannot make assumptions about its distribution. MLE provides a method to estimate the probability distribution from data.
What is the Goal of MLE?
The goal of MLE is to find the best parameters for a model. When the best parameters are found, it can be used to make accurate predictions.
The way MLE finds these parameters is by maximizing the likelihood function. The likelihood function changes depending on the model you use.
θ^=argθ∈ΘmaxLn(θ;y)
The equation above is say that we can find the best model parameters (θ^) by looking at all the possible model parameters in the set Θ and calculating likelihood of seen the given data.
MLE for Binomial Distribution
Lets look at an example to see how the MLE is calculated.
Consider an experiment where a coin is tossed 20 times, and we observe the number of heads. Our goal is to determine whether the coin is fair. The model for this task is the Binomial Distribution.
What is the Binomial Distribution?
The Binomial Distribution is a discrete distribution that describes the probability of obtaining an successful outcome from independent trials.
P(x=k;p)=(kn)pk(1−p)n−k
In our coin toss experiment, we have n=20 trials, and the observation k is the amount of times we see heads. The model parameter we want to find is p the probability of getting heads vs tails.
If we assume that the coin is fair then we could set p=0.5; however, this runs the risk of our assumption being wrong in the real world. With MLE we do not have to make this assumption, but instead use data to find it. The data may reveal a different probability distribution.
Steps to Finding the MLE
Since we do not know what p is we will set to a parameter, θ:
P(D∣p=θ)=(nHnH+nT)θnH(1−θ)nT
D is our data, i.e. the number of times we observed heads and tails.
θ^=argθmaxP(D∣θ)
Step 0: Setup the Loss Likelihood Function
Repeatedly multiplying numbers between 000 and 111 can lead to numerical instability. To address this, we apply the logarithm to the distribution. This transformation is mathematically justified, as the logarithmic function is monotonically increasing, preserving the relative order of values. This mean that if P(D∣θ) increases then so will logP(D∣θ).
In our experiment we saw that nH=10, and nH+nT=20.
Solving for θ=2010=0.5. This shows us that indeed the coin is fair.
MLE for Linear Regression
Linear Regression is a Machine Learning task where we estimate a linear equation to find a hyperplane in d dimensions.
We make the assumption that the data will fit the line y=wTx+ϵ where ϵ is normally distributed noise in the data. x are the features we observe, w is the weight matrix of the model, and y is the predicted output.
This is a Closed-form solution for w.
Computational speed ups can be made with the following equation:
w=(X⊤X)−1X⊤y
where X is a matrix of shape (n,d) for n data samples and d features.
Exercise: Prove the equation above.
Exercise: The inverse operation is numerically unstable. What would you do to fix this issue?
MLE for Logistic Regression
Logistic Regression leverages the Naive Bayes assumption while employing the sigmoid function as a probability density function. It performs better with large datasets because it does not rely on prior knowledge about the underlying data distribution unlike the Naive Bayes Classifier model.
The relationship is expressed as:
P(y∣x)=1+e−y(wTx+b)1
The sigmoid function has the domain of all real values and a range between 0 and 1, making it suitable for mapping outputs to probability values.
MLE with the sigmoid function
In our logistic regression model, the parameters ww and b need to be optimized. w, as with our linear model is the weights vector, and b is our bias scalar value.
The likelihood is the product of all probabilities across all n observations:
The key difference between the two derivatives is and extra xi term for w.
Obtaining a closed form for the MLE is not feasible due to the non-linearity of the sigmoid function. Setting it to 0 and solving for w and b respectively does not have a closed form solution.
Instead, Gradient Descent is used to iteratively calculate the optimal values for w and b.
What is Gradient Descent?
Gradient Descent is an optimization algorithm that iteratively finds the global minimum of a target function. It begins by making an initial guess for the model parameters and then updates the guess by moving in the opposite direction of the gradient.
For logistic regression, it can be mathematically proven that gradient descent will always converge to the global minimum. However, this is not guaranteed for all functions. The reason gradient descent works for linear and logistic regression is that these models are convex, meaning they have a single global minimum. In contrast, for non-convex models, the algorithm can get trapped in a local minimum instead of reaching the global one.
Exercise: Prove that the loss function l(w,b) for logistic regression is convex.
A function is convex if the following is true:
it is has a first derivative
it has a second derivative
the second derivative is non-negative in its entire domain.
Gradient Descent Algorithm
Input:
Time step T
learning rate α
Step 1:
initialize the model parameters (vector w, and scalar b) randomly
Step 2:
for each time step t from 0…(T−1):
compute the gradient of the loss function w.r.t each model parameter:
g(w)=−∂w∂l(w,b) and g(b)=db−dl(w,b)
Update the model parameters by moving in the direction opposite to the gradients, scaled by the learning rate:
w=w−αg(w) and b=b−αg(b)
Output:
model parameters w and b
Why do we scale the gradients by the learning rate?
The learning rate α is used to scale the gradients to control the step size taken toward the optimal solution. It is necessary for many reasons.
Control the Size of Updates: The update gradients, to the model parameters, could be too large or too small, making the optimization unstable or inefficient.
If the step size is too large, you might overshoot the optimal values, missing the minimum.
If it's too small, the optimization process will be slow, requiring many iterations to converge.
Normalization across model Parameters: The gradients could have different magnitudes. Scaling by the learning rate helps balance the updates for each parameter.
This ensures that each parameter is updated proportionally, leading to better optimization across all parameters.
Conclusion
In this blog, we explored the concept of Maximum Likelihood Estimation (MLE) and its role in finding the global minimum for convex functions. We applied this technique to three different models, illustrating its effectiveness in optimizing model parameters.
In the next blog, we will dive deeper into the practical application of the Gradient Descent algorithm, examining how it is used to find the optimal parameters for the logistic regression model and how it can be fine-tuned for real-world problems.