1

I know the jacobian is the first derivative, but I don't know how to compute it for my simple function (I tried online derivative calculators) and pass it to my scipy minimize function.

In code, here is the objective function (guess arrays can contain thousands of variables):

def objective(current_guesses_array, first_guesses_array):
        return np.sum(np.divide(np.square(current_guesses_array - first_guesses_array), first_guesses_array))

I think the Jacobian is like this, but definitely may have messed up here:

dx = 2x/y - 2

dy = 1 - (x^2/y^2)

In code:

def jacobian_for_minimize(self, x,a):
    dx = (2*x)/a - 2
    dy = 1 - (np.square(x) / np.square(a))
    return np.array([dx,dy])

minimize(objective,initial_guesses,args=initial_guesses,jac=jacobian_for_minimize,method='SLSQP')

After calling minimize, I get an error: _slsqp.error: failed in converting 8th argument `g' of _slsqp.slsqp to C/Fortran array

According to another Stack overflow page, it means the objective function isn't returning a scalar, and it has to.

Can anyone tell where I went wrong with this?

Curiosity
  • 149
  • 1
  • 2
  • 11
  • Are you looking for an answer on how to compute a jacobian in python, or are you looking for one that specifically details what went wrong with *your* approach in computing the jacobian – Primusa Feb 12 '19 at 00:13
  • I'm looking for what went wrong with my approach, but I am open to other approaches. – Curiosity Feb 12 '19 at 00:39
  • Your Jacobian is at least correct, if y (a) is your first_guess_array and x is the current_guess. I don't know about the minimize function. – MinosIllyrien Feb 12 '19 at 07:48
  • Can you share the equations for the objective? I presume you have two variables only. Do we expect a function of the form `z = f(x, y)` ? – newkid Feb 12 '19 at 13:09
  • @newkid yes, we expect a function of that form where x and y are vectors. The equation of the objective function is basically just SUM(((current_guesses_array - first_guesses_array)^2)/first_guesses_array) – Curiosity Feb 12 '19 at 13:32
  • For what it's worth: your derivatives are OK – mikuszefski Feb 12 '19 at 16:29
  • Why not compute the Jacobian symbolically using, say, [SymPy](https://www.sympy.org)? – Rodrigo de Azevedo Feb 12 '19 at 23:00

0 Answers0