python - Why is scipy.optimize.minimize trying to pass in weird arguments to my objective function? -
i have class helps instantiate statistical model. of data members parameters. trying write method optimizes these parameters. objection function based on negative of likelihood function. likelihood function implemented class method, uses values of class data members in calculation.
i know bad style, every time objective function gets called scipy.optimize.minimize()
, changes objects data members better ones. less concerned why bad, , more concerned why isn't working. below code full traceback.
it seems works partially. runs few seconds on test data, triggers assertion. seems minimize()
weird when it's nearing end of optimizations. why try pass in different types of arguments objective function obj_fun()
? in ipython interpreter check object's parameters afterwards, , seems arriving @ expected result.
i tried looking through of source of scipy. it's confusing, though. there lot of ambiguous variable naming , function wrapping. can give me color on why happening, , how fix it? again keep optimization stuff inside class.
class mything(object): . . . def mle_fit(self, y, inpt, optim_these): #step 1: figure out want optimize self.optimize_these = optim_these #step 2: inital flat parameter vector self._make_list_optimizable_hyp_pars() init_guess = self.flat_hyper_params #step 3: run minimize def obj_fun(pars): # returns negative log likelihood assert len(pars) == len(init_guess) # here # self.flat_hyper_params = pars self._unflatten_new_hps() self.like(y, inpt) return .5 * self.neg_2_log_like res = minimize(obj_fun, init_guess, method = 'bfgs')
traceback:
file "/home/taylor/anaconda/lib/python2.7/site-packages/scipy/optimize/_minimize.py", line 419, in minimize return _minimize_bfgs(fun, x0, args, jac, callback, **options) file "/home/taylor/anaconda/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 850, in _minimize_bfgs old_fval, old_old_fval) file "/home/taylor/anaconda/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 690, in _line_search_wolfe12 old_fval, old_old_fval) file "/home/taylor/anaconda/lib/python2.7/site-packages/scipy/optimize/linesearch.py", line 263, in line_search_wolfe2 derphi0, c1, c2, amax) file "/home/taylor/anaconda/lib/python2.7/site-packages/scipy/optimize/linesearch.py", line 363, in scalar_search_wolfe2 phi0, derphi0, c1, c2) file "/home/taylor/anaconda/lib/python2.7/site-packages/scipy/optimize/linesearch.py", line 498, in _zoom phi_aj = phi(a_j) file "/home/taylor/anaconda/lib/python2.7/site-packages/scipy/optimize/linesearch.py", line 239, in phi return f(xk + alpha * pk, *args) file "/home/taylor/anaconda/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 281, in function_wrapper return function(*(wrapper_args + args)) file "mything.py", line 222, in obj_fun assert len(pars) == len(init_guess)
if remember correctly, scipy, when optimizing, can send both scalars , arrays function minimized. thus, if optimize f(x)
, get, say,x = 3.14
, x = array([1, 4])
. (this speeding calculations, objective functions f
use numpy array functions.) if indeed case, having code handle solve problem.
now, can check situation check print pars, type(pars), len(pars)
in obj_fun()
.
Comments
Post a Comment