scipy.optimize minimize: Two output variables in objective function?

4.5k views Asked by At

I am using the scipy minimize function to find the optimal value of some parameters, H and Q. My objective function, kalman, is evaluated on the variable log_likelihood. In a nutshell, I am trying to find the optimal values of H and Q that maximize the variable log_likelihood.

In order to do this, scipy's minimize function requires that log_likelihood be the sole output of my function kalman.

My code runs fine and I can find the optimal values of my two parameters H and Q. What I would like to do, however, is run kalman a final time (after optimization) using the optimal H and Q values and have kalman return another variable, A, back.

I can't do this because if I set

return log_likelihood, A

in my kalman function, the minimize function won't run, because minimize can only handle one output from the objective function.

Any thoughts?

Here is my code:

import numpy as np
from scipy.optimize import minimize

def kalman(x0):

    #set parameters values
    H = x0[0]
    Q = x0[1]

    #Do some operations
    #use H and Q to compute value for variable log_likelihood
    #use H and Q to compute value for variable A
    #...

    return log_likelihood

#initial parameter values
x0 = np.array([np.log(1),np.log(1)])

#optimizing function    
res = minimize(kalman, x0, method='BFGS', options={'disp': True})

#get optimal parameter values
param_values = res.x

#run kalman function final time with optimal values
kalman(param_values)     
0

There are 0 answers