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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-22 16:18:43 +0000
Seen: 16 times
Last updated: May 22 '23
How to calibrate two correlated functions simultaneously using Python's scipy.optimize?
Can you explain the functioning of the {m,n}? regex in the Python re module?
Is it possible to open a video file in Python with OpenCV without any delay?
How can I create a Python quiz using Json with only one question and the quiz stops after it?
How can one ensure that sub-classes have uniform method parameters in TypeScript?
How can the calculation of matrix determinant be performed using CUDA?