Ask Your Question
1

How to apply the negative log-likelihood function using Python?

asked 2023-05-22 16:18:43 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-22 16:22:02 +0000

huitzilopochtli gravatar image

To apply the negative log-likelihood function using Python, you can follow the below steps:

  1. Import necessary libraries.

    import numpy as np
    import scipy.stats as stats
    
  2. 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))
    
  3. Define the negative log-likelihood function.

    def neg_log_likelihood(params, x):
       return -1 * likelihood(params, x)
    
  4. 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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-05-22 16:18:43 +0000

Seen: 16 times

Last updated: May 22 '23