python - how can I do a maximum likelihood regression using scipy.optimize.minimize -


how can maximum likelihood regression using scipy.optimize.minimize? want use minimize function here, because have complex model , need add constraints. trying simple example using following:

from scipy.optimize import minimize  def lik(parameters):     m = parameters[0]     b = parameters[1]     sigma = parameters[2]     in np.arange(0, len(x)):         y_exp = m * x + b     l = sum(np.log(sigma) + 0.5 * np.log(2 * np.pi) + (y - y_exp) ** 2 / (2 * sigma ** 2))     return l  x = [1,2,3,4,5] y = [2,3,4,5,6] lik_model = minimize(lik, np.array([1,1,1]), method='l-bfgs-b', options={'disp': true}) 

when run this, convergence fails. know wrong code?

the message running 'abnormal_termination_in_lnsrch'. using same algorithm have working using optim in r.

thank aleksander. correct likelihood function wrong, not code. using formula found on wikipedia adjusted code to:

import numpy np scipy.optimize import minimize  def lik(parameters):     m = parameters[0]     b = parameters[1]     sigma = parameters[2]     in np.arange(0, len(x)):         y_exp = m * x + b     l = (len(x)/2 * np.log(2 * np.pi) + len(x)/2 * np.log(sigma ** 2) + 1 /          (2 * sigma ** 2) * sum((y - y_exp) ** 2))     return l  x = np.array([1,2,3,4,5]) y = np.array([2,5,8,11,14]) lik_model = minimize(lik, np.array([1,1,1]), method='l-bfgs-b') plt.scatter(x,y) plt.plot(x, lik_model['x'][0] * x + lik_model['x'][1]) plt.show() 

now seems working.

maximum likelihood regression

thanks help!


Comments

Popular posts from this blog

angularjs - Showing an empty as first option in select tag -

How to wrap some text in a rectangle in QML? -

python - argument 2 to map() must support iteration -