David
David

Reputation: 35

Newton - CG Optimization in python, problems with Jacobian

I'm trying to make Newton - CG Optimization in python. My function is f(x,y) =(1-x)^2 + 2(y-x^2)^2. Initial points: x = 3, y = 2. Here is my code:

from scipy.optimize import minimize 

def f(params): #definite function
    x, y = params #amount of params 
    return (1 - x) ** 2 + 2 * (y - x ** 2) ** 2

def jacobian(params): #definite function
    x, y = params #amount of params
    der = np.zeros_like(x)
    der[0] = -8 * x * (-x ** 2 + y) + 2 * x - 2 #derivative by x
    der[1] = -4 * x ** 2 + 4 * y #derivative by y
    return der

initial_guess = [3, 2] #initial points
result = minimize(f, initial_guess, jac = jacobian, method = 'Newton-CG')

I got an error "IndexError: too many indices for array".

As I made Nelder - mead optimization, BFGS and they work. So, problem is with Jacobian matrix. I feel somewhere in def jacobian is a mistake.

Upvotes: 2

Views: 819

Answers (1)

rinkert
rinkert

Reputation: 6863

The error is indeed in the jacobian function, you are defining der as zeros taking the size of x, which is a scalar. Instead use params:

def jacobian(params): #definite function
    x, y = params #amount of params
    der = np.zeros_like(params)
    der[0] = -8 * x * (-x ** 2 + y) + 2 * x - 2 #derivative by x
    der[1] = -4 * x ** 2 + 4 * y #derivative by y
    return der

Upvotes: 2

Related Questions