Skip to main content

Maximum Likelihood Estimation

GitHub repository

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:

fX(x)=12πσ2β‹…eβˆ’12(xβˆ’ΞΌΟƒ)2,f_X(x)=\frac{1}{\sqrt{2\pi\sigma^2}}\cdot e^{-\frac{1}{2} (\frac{x-\mu}{\sigma})^2},

where ΞΌ\mu is mean and Οƒ\sigma is standard deviation.

Assuming we have a dataset comprising nn 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 nn observations is given as follows:

L(ΞΌ,Οƒ,x1,x2,⋯ ,xn)=∏i=1nfX(xi)=∏i=1n12πσ2β‹…eβˆ’12(xiβˆ’ΞΌΟƒ)2=(12πσ2)nβ‹…eβˆ’12Οƒ2βˆ‘i=1n(xiβˆ’ΞΌ)2.\begin{align*} L(\mu,\sigma, x_1, x_2,\cdots,x_{n})&=\prod_{i=1}^{n}f_X(x_i)\\ &=\prod_{i=1}^{n}\frac{1}{\sqrt{2\pi\sigma^2}}\cdot e^{-\frac{1}{2} (\frac{x_i-\mu}{\sigma})^2}\\ &=\Bigg(\frac{1}{\sqrt{2\pi\sigma^2}}\Bigg)^n\cdot e^{-\frac{1}{2\sigma^2} \sum_{i=1}^{n} (x_i-\mu)^2}.\\ \end{align*}

Log Likelihood Function​

Taking the log of Likelihood Function, we get

l(ΞΌ,Οƒ,x1,x2,⋯ ,xn)=log⁑[L(ΞΌ,Οƒ,x1,x2,⋯ ,xn)]=log⁑[(12πσ2)nβ‹…eβˆ’12Οƒ2βˆ‘i=1n(xiβˆ’ΞΌ)2]=log⁑[(12πσ2)n]β‹…log⁑[eβˆ’12Οƒ2βˆ‘i=1n(xiβˆ’ΞΌ)2]=βˆ’nlog⁑(2πσ2)βˆ’12Οƒ2βˆ‘i=1n(xiβˆ’ΞΌ)2\begin{align*} l(\mu,\sigma, x_1, x_2,\cdots,x_{n})&=\log{[L(\mu,\sigma, x_1, x_2,\cdots,x_{n})]}\\ &=\log{\Bigg[\Big(\frac{1}{\sqrt{2\pi\sigma^2}}\Big)^n\cdot e^{-\frac{1}{2\sigma^2} \sum_{i=1}^{n} (x_i-\mu)^2}\Bigg]}\\ &=\log{\Bigg[\Big(\frac{1}{\sqrt{2\pi\sigma^2}}\Big)^n\Bigg]}\cdot \log{\Bigg[e^{-\frac{1}{2\sigma^2} \sum_{i=1}^{n} (x_i-\mu)^2}\Bigg]}\\ &=-n\log{(\sqrt{2\pi\sigma^2})}-\frac{1}{2\sigma^2} \sum_{i=1}^{n} (x_i-\mu)^2\\ \end{align*}

Maximization Problem​

Now we have solve the following maximization problem

arg max⁑μ,Οƒ{l(ΞΌ,Οƒ,x1,x2,⋯ ,xn)}.\argmax_{\mu,\sigma}\{l(\mu,\sigma, x_1, x_2,\cdots,x_{n})\}.

The above maximization problem is equivalent to

arg min⁑μ,Οƒ{βˆ’l(ΞΌ,Οƒ,x1,x2,⋯ ,xn)},\argmin_{\mu,\sigma}\{-l(\mu,\sigma, x_1, x_2,\cdots,x_{n})\},

therefore we will solve

arg min⁑μ,Οƒ{βˆ’(βˆ’nlog⁑(2πσ2)βˆ’12Οƒ2βˆ‘i=1n(xiβˆ’ΞΌ)2)}β€…β€ŠβŸΉβ€…β€Šarg min⁑μ,Οƒ{nlog⁑(2πσ2)+12Οƒ2βˆ‘i=1n(xiβˆ’ΞΌ)2}.\begin{align*} &\argmin_{\mu,\sigma}\{-(-n\log{(\sqrt{2\pi\sigma^2})}-\frac{1}{2\sigma^2} \sum_{i=1}^{n} (x_i-\mu)^2)\}\\ \implies &\argmin_{\mu,\sigma}\{n\log{(\sqrt{2\pi\sigma^2})}+\frac{1}{2\sigma^2} \sum_{i=1}^{n} (x_i-\mu)^2\}.\\ \end{align*}

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