Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Hessian Matrix from python minimize function?

Is there any way that we could get the Hessian matrix (so as to calculate the standard error) after getting the optimization result through scipy.minimize function?

The parameter of hessian in the minimize function seems to be input instead of an output.

from scipy import minimize

opt = minimize(logitfn, args=df, x0=x_start, method='Nelder-Mead')
like image 962
Jacob2309 Avatar asked Feb 02 '26 15:02

Jacob2309


2 Answers

Use 'L-BFGS-B' method, and then:

opt.hess_inv.todense()
like image 144
Jacob2309 Avatar answered Feb 04 '26 06:02

Jacob2309


Given a function f and initial point x0, and assuming we use L-BFGS-B, then the following code works:

opt = minimize(f, x0=x0, method='L-BFGS-B')
B = opt.hess_inv  # LinearOperator object
B = B * np.identity(B.shape[1])  # numpy array
like image 45
Ben Rhodes Avatar answered Feb 04 '26 05:02

Ben Rhodes



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!