In statistical modeling and machine learning, maximum likelihood estimation (MLE) is a common method used to estimate the parameters of a statistical model. It involves finding the parameter values that maximize the likelihood function, which measures the probability of observing the given data.
In Python, the scipy
library provides a convenient way to perform maximum likelihood estimation. This blog post will walk you through an example of how to use scipy
for maximum likelihood estimation.
Installation
Before we proceed, make sure you have scipy
installed in your Python environment. If not, you can install it using pip:
pip install scipy
Now, let’s dive into an example to understand how to use scipy
for maximum likelihood estimation.
Example: Estimating the Parameters of a Normal Distribution
Let’s suppose we have a dataset that follows a normal distribution. Our goal is to estimate the mean and variance of this distribution using maximum likelihood estimation.
To start, import the required libraries:
import numpy as np
from scipy.stats import norm
from scipy.optimize import minimize
Next, generate some random data from a normal distribution:
np.random.seed(123)
data = np.random.normal(loc=5, scale=2, size=1000)
Now, define our likelihood function. We will assume that the data follows a normal distribution:
def likelihood(params, x):
mu, sigma = params
return -np.sum(norm.logpdf(x, loc=mu, scale=sigma))
The likelihood
function takes the parameter values (params
) and the data (x
) as inputs and returns the negative log-likelihood. We use the norm.logpdf
function from scipy.stats
to calculate the log-likelihood of each data point.
Finally, we can estimate the maximum likelihood parameters using the minimize
function from scipy.optimize
:
initial_guess = [0, 1]
result = minimize(likelihood, initial_guess, args=(data,))
mu_mle, sigma_mle = result.x
The minimize
function finds the values of the parameters that minimize the negative log-likelihood. The initial guess is defined as [0, 1]
, and the args
parameter is used to pass the data to the likelihood function.
The resulting mu_mle
and sigma_mle
are the estimated maximum likelihood parameters.
Conclusion
In this blog post, we have learned how to use the scipy
library in Python to perform maximum likelihood estimation. We walked through an example of estimating the parameters of a normal distribution.
Maximum likelihood estimation is a powerful tool for estimating the parameters of statistical models. With scipy
, it becomes easier to implement and perform maximum likelihood estimation in Python.
I hope this tutorial has been helpful in understanding how to use scipy
for maximum likelihood estimation. Keep exploring and experimenting with different models and datasets to gain more insights into your data!