Maximum Likelihood Estimation
In this Jupyter notebook, we will do the maximum likelihood estimation of the parameters, mean and standard deviation, of the normal distribution.
Likelihood Functionβ
PDF of normal distribution is given as follows:
where is mean and is standard deviation.
Assuming we have a dataset comprising randomly selected observations from a normal distribution with a mean of 155 and a standard deviation of 7, independently and identically distributed (IID). The likelihood function corresponding to these observations is given as follows:
Log Likelihood Functionβ
Taking the log of Likelihood Function, we get
Maximization Problemβ
Now we have solve the following maximization problem
The above maximization problem is equivalent to
therefore we will solve
Python Codeβ
import numpy as np
from scipy.optimize import minimize
# Generate some example data
np.random.seed(2609)
data = np.random.normal(loc=155, scale=7, size=100000)
# Define the likelihood function for a normal distribution
def likelihood(params, data):
mean, std_dev = params
likelihood_values = np.log(np.sqrt(2*np.pi*(std_dev**2))) + (0.5*(((data - mean) / std_dev)**2))
return np.sum(likelihood_values)
# Initial guess for the parameters
initial_params = [0, 1]
# Use scipy's minimize function to maximize the likelihood
result = minimize(likelihood, initial_params, args=(data,))
estimated_mean, estimated_std_dev = result.x
# Print the results
print("Estimated Mean:", estimated_mean)
print("Estimated Standard Deviation:", estimated_std_dev)
print("Convergence Status:", result.success)
print("Optimal Value of the Objective Function:", result.fun)
Estimated Mean: 154.99808505357024
Estimated Standard Deviation: 6.996996924429178
Convergence Status: True
Optimal Value of the Objective Function: 336441.9597128625