Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log-likelihood function generated by scipy.stats.rv_continuous.fit

The method scipy.stats.rv_continuous.fit finds the parameters that maximise a log likelihood function which is determined by the input data and the specification of the distribution rv_continuous. For instance, this could be normal or gamma.

The documentation for scipy.stats.rv_continuous.fit doesn't explain how the log-likelihood function is generated and I would like to know how. I need it so I can calculate the value of the log-likelihood at the parameters estimated by fit (i.e. the maximum value).

like image 848
jcm Avatar asked Nov 05 '25 07:11

jcm


1 Answers

While Mark's answer is technically correct, I can only re-iterate nikosd's concern from the comment - taking the product first and then logging it, will in many practical scenarios make your results unusable. If have many (thousands/millions) observations in data, each has a probability of <=1, so your product np.product(norm.pdf(data,m,s)) will be very small and often smaller than numerical precision, making your results unstable/wrong.

Thus the better way - and the reason why log-likelihood is used in the first place - is to first log the individual probabilities np.log(norm.pdf(data,m,s)) and then sum over the resulting vector.

import numpy as np
from scipy.stats import norm

data = [1,2,3,4,5]
m,s = norm.fit(data)
log_likelihood = np.sum(np.log(norm.pdf(data,m,s)))

I thought this was important enough to warrant a separate answer.

like image 190
seulberg1 Avatar answered Nov 06 '25 23:11

seulberg1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!