0

Lets say, I have a function z = x^2 + y^2. Now, I want to implement hessian of this function z in python. Till now, I have calculated the derivative using finite-difference method as given below -

def derivative(x, y, f, h):
    return [(f(x + h, y) - f(x - h, y )) / (2*h), (f(x , y + h) - f(x , y - h)) / (2*h)]

I'm not sure how to reuse this to calculate the Hessian.

Avijit Dasgupta
  • 2,055
  • 3
  • 22
  • 36
  • For the diagonal terms you can use the second order finite difference `f_xx = (f(x+h) - 2*f(x) + f(x-h))/(h^2)`. For the off diagonal terms you can derive the expression by the `x` derivative followed by the `y` derivative. – Steve Sep 25 '19 at 13:09

1 Answers1

0

There is a simpler method: use the numdifftools package. It has the Hessian function, which does what you want. Documentation

Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34