Search code examples
pythonscipyscipy-optimize

Jacobian of the solution from scipy.optimize is not equal to fjac of scipy.optimize.OptimizeResult


When finding the root of the function using scipy.optimize.root() with argument jac = True, the OptimizeResult.fjac returns the values of Jacobian which are incorrect with the Jacobian of the solution.

For example,

# objective function
def fun(x):
    return [x[0]  + 0.5 * (x[0] - x[1])**3 - 1.0, 0.5 * (x[1] - x[0])**3 + x[1]]

# function returns Jacobain at x
def jac(x):
    return np.array([[1 + 1.5 * (x[0] - x[1])**2, -1.5 * (x[0] - x[1])**2],
                [-1.5 * (x[1] - x[0])**2,
                1 + 1.5 * (x[1] - x[0])**2]])
from scipy import optimize
sol = optimize.root(fun, [0, 0], jac=jac)

After the solution is converged, sol.fjac is different from jac(sol.x)? I wouldn't able to understand what fjac denotes from OptimizeResult?

Any insights and corrections are much appreciated :)


Solution

  • I checked using the debugger, and under the hood the default algorithm scipy.optimize.root uses is the _minpack.hybrj function. If you look at the documentation for fsolve (which also uses hybrj), it specifies:

    fjac the orthogonal matrix, q, produced by the QR factorization of the final approximate Jacobian matrix, stored column wise

    It thus seems like scipy's documentation is incomplete.

    Source: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.fsolve.html