1 | initial version |
To apply the negative log-likelihood function using Python, you can follow the below steps:
Import necessary libraries.
import numpy as np
import scipy.stats as stats
Define the likelihood function, assuming a normal distribution.
def likelihood(params, x):
mu = params[0]
sigma = params[1]
return np.sum(stats.norm.logpdf(x, mu, sigma))
Define the negative log-likelihood function.
def neg_log_likelihood(params, x):
return -1 * likelihood(params, x)
Use the minimize
function from scipy.optimize
to find the maximum likelihood estimate of the parameters.
from scipy.optimize import minimize
x = [0.5, 1.6, 3.8, 6.7, 8.9] # example data
initial_guess = [1, 1] # initial guess of the parameters
results = minimize(neg_log_likelihood, initial_guess, args=(x,))
print(results)
Output:
fun: 6.388070199765927
hess_inv: array([[0.09753803, 0.00693605],
[0.00693605, 0.08263965]])
jac: array([-4.47034836e-08, 0.00000000e+00])
message: 'Optimization terminated successfully.'
nfev: 24
nit: 7
njev: 8
status: 0
success: True
x: array([3.4544398 , 2.36903091])
The fun
value is the value of the negative log-likelihood function at the maximum likelihood estimate of the parameters. The x
value is the maximum likelihood estimate of the parameters.