<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import distutils . version <EOL> from scipy import __version__ as scipy_version <EOL> import numpy as np <EOL> from scipy import optimize <EOL> def _check_method ( method , methods ) : <EOL> if method not in methods : <EOL> message = "<STR_LIT>" % method <EOL> raise ValueError ( message ) <EOL> class Optimizer ( object ) : <EOL> def _fit ( self , objective , gradient , start_params , fargs , kwargs , <EOL> hessian = None , method = '<STR_LIT>' , maxiter = <NUM_LIT:100> , full_output = True , <EOL> disp = True , callback = None , retall = False ) : <EOL> """<STR_LIT>""" <EOL> extra_fit_funcs = kwargs . setdefault ( '<STR_LIT>' , dict ( ) ) <EOL> methods = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> methods += extra_fit_funcs . keys ( ) <EOL> method = method . lower ( ) <EOL> _check_method ( method , methods ) <EOL> fit_funcs = { <EOL> '<STR_LIT>' : _fit_newton , <EOL> '<STR_LIT>' : _fit_nm , <EOL> '<STR_LIT>' : _fit_bfgs , <EOL> '<STR_LIT>' : _fit_lbfgs , <EOL> '<STR_LIT>' : _fit_cg , <EOL> '<STR_LIT>' : _fit_ncg , <EOL> '<STR_LIT>' : _fit_powell , <EOL> '<STR_LIT>' : _fit_basinhopping , <EOL> } <EOL> if extra_fit_funcs : <EOL> fit_funcs . update ( extra_fit_funcs ) <EOL> func = fit_funcs [ method ] <EOL> xopt , retvals = func ( objective , gradient , start_params , fargs , kwargs , <EOL> disp = disp , maxiter = maxiter , callback = callback , <EOL> retall = retall , full_output = full_output , <EOL> hess = hessian ) <EOL> optim_settings = { '<STR_LIT>' : method , '<STR_LIT>' : start_params , <EOL> '<STR_LIT>' : maxiter , '<STR_LIT>' : full_output , <EOL> '<STR_LIT>' : disp , '<STR_LIT>' : fargs , '<STR_LIT>' : callback , <EOL> '<STR_LIT>' : retall } <EOL> optim_settings . update ( kwargs ) <EOL> return xopt , retvals , optim_settings <EOL> def _fit_constrained ( self , params ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _fit_regularized ( self , params ) : <EOL> pass <EOL> def _fit_newton ( f , score , start_params , fargs , kwargs , disp = True , <EOL> maxiter = <NUM_LIT:100> , callback = None , retall = False , <EOL> full_output = True , hess = None , ridge_factor = <NUM_LIT> ) : <EOL> tol = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> iterations = <NUM_LIT:0> <EOL> oldparams = np . inf <EOL> newparams = np . asarray ( start_params ) <EOL> if retall : <EOL> history = [ oldparams , newparams ] <EOL> while ( iterations < maxiter and np . any ( np . abs ( newparams - <EOL> oldparams ) > tol ) ) : <EOL> H = np . asarray ( hess ( newparams ) ) <EOL> if not np . all ( ridge_factor == <NUM_LIT:0> ) : <EOL> H [ np . diag_indices ( H . shape [ <NUM_LIT:0> ] ) ] += ridge_factor <EOL> oldparams = newparams <EOL> newparams = oldparams - np . dot ( np . linalg . inv ( H ) , <EOL> score ( oldparams ) ) <EOL> if retall : <EOL> history . append ( newparams ) <EOL> if callback is not None : <EOL> callback ( newparams ) <EOL> iterations += <NUM_LIT:1> <EOL> fval = f ( newparams , * fargs ) <EOL> if iterations == maxiter : <EOL> warnflag = <NUM_LIT:1> <EOL> if disp : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % fval ) <EOL> print ( "<STR_LIT>" % iterations ) <EOL> else : <EOL> warnflag = <NUM_LIT:0> <EOL> if disp : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % fval ) <EOL> print ( "<STR_LIT>" % iterations ) <EOL> if full_output : <EOL> ( xopt , fopt , niter , <EOL> gopt , hopt ) = ( newparams , f ( newparams , * fargs ) , <EOL> iterations , score ( newparams ) , <EOL> hess ( newparams ) ) <EOL> converged = not warnflag <EOL> retvals = { '<STR_LIT>' : fopt , '<STR_LIT>' : niter , '<STR_LIT>' : gopt , <EOL> '<STR_LIT>' : hopt , '<STR_LIT>' : warnflag , <EOL> '<STR_LIT>' : converged } <EOL> if retall : <EOL> retvals . update ( { '<STR_LIT>' : history } ) <EOL> else : <EOL> xopt = newparams <EOL> retvals = None <EOL> return xopt , retvals <EOL> def _fit_bfgs ( f , score , start_params , fargs , kwargs , disp = True , <EOL> maxiter = <NUM_LIT:100> , callback = None , retall = False , <EOL> full_output = True , hess = None ) : <EOL> gtol = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> norm = kwargs . setdefault ( '<STR_LIT>' , np . Inf ) <EOL> epsilon = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> retvals = optimize . fmin_bfgs ( f , start_params , score , args = fargs , <EOL> gtol = gtol , norm = norm , epsilon = epsilon , <EOL> maxiter = maxiter , full_output = full_output , <EOL> disp = disp , retall = retall , callback = callback ) <EOL> if full_output : <EOL> if not retall : <EOL> xopt , fopt , gopt , Hinv , fcalls , gcalls , warnflag = retvals <EOL> else : <EOL> ( xopt , fopt , gopt , Hinv , fcalls , <EOL> gcalls , warnflag , allvecs ) = retvals <EOL> converged = not warnflag <EOL> retvals = { '<STR_LIT>' : fopt , '<STR_LIT>' : gopt , '<STR_LIT>' : Hinv , <EOL> '<STR_LIT>' : fcalls , '<STR_LIT>' : gcalls , '<STR_LIT>' : <EOL> warnflag , '<STR_LIT>' : converged } <EOL> if retall : <EOL> retvals . update ( { '<STR_LIT>' : allvecs } ) <EOL> else : <EOL> xopt = retvals <EOL> retvals = None <EOL> return xopt , retvals <EOL> def _fit_lbfgs ( f , score , start_params , fargs , kwargs , disp = True , <EOL> maxiter = <NUM_LIT:100> , callback = None , retall = False , <EOL> full_output = True , hess = None ) : <EOL> """<STR_LIT>""" <EOL> bounds = kwargs . setdefault ( '<STR_LIT>' , [ ( None , None ) ] * len ( start_params ) ) <EOL> kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> names = ( '<STR_LIT:m>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> extra_kwargs = dict ( ( x , kwargs [ x ] ) for x in names if x in kwargs ) <EOL> approx_grad = kwargs . get ( '<STR_LIT>' , False ) <EOL> loglike_and_score = kwargs . get ( '<STR_LIT>' , None ) <EOL> epsilon = kwargs . get ( '<STR_LIT>' , None ) <EOL> if approx_grad : <EOL> score = None <EOL> if epsilon and not approx_grad : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if approx_grad and loglike_and_score : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if loglike_and_score : <EOL> func = lambda p , * a : tuple ( - x for x in loglike_and_score ( p , * a ) ) <EOL> elif score : <EOL> func = f <EOL> extra_kwargs [ '<STR_LIT>' ] = score <EOL> elif approx_grad : <EOL> func = f <EOL> scipy_version_curr = distutils . version . LooseVersion ( scipy_version ) <EOL> scipy_version_12 = distutils . version . LooseVersion ( '<STR_LIT>' ) <EOL> if scipy_version_curr < scipy_version_12 : <EOL> retvals = optimize . fmin_l_bfgs_b ( func , start_params , args = fargs , <EOL> bounds = bounds , disp = disp , <EOL> ** extra_kwargs ) <EOL> else : <EOL> retvals = optimize . fmin_l_bfgs_b ( func , start_params , maxiter = maxiter , <EOL> callback = callback , args = fargs , <EOL> bounds = bounds , disp = disp , <EOL> ** extra_kwargs ) <EOL> if full_output : <EOL> xopt , fopt , d = retvals <EOL> warnflag = d [ '<STR_LIT>' ] <EOL> converged = ( warnflag == <NUM_LIT:0> ) <EOL> gopt = d [ '<STR_LIT>' ] <EOL> fcalls = d [ '<STR_LIT>' ] <EOL> retvals = { '<STR_LIT>' : fopt , '<STR_LIT>' : gopt , '<STR_LIT>' : fcalls , <EOL> '<STR_LIT>' : warnflag , '<STR_LIT>' : converged } <EOL> else : <EOL> xopt = retvals [ <NUM_LIT:0> ] <EOL> retvals = None <EOL> return xopt , retvals <EOL> def _fit_nm ( f , score , start_params , fargs , kwargs , disp = True , <EOL> maxiter = <NUM_LIT:100> , callback = None , retall = False , <EOL> full_output = True , hess = None ) : <EOL> xtol = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> ftol = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> maxfun = kwargs . setdefault ( '<STR_LIT>' , None ) <EOL> retvals = optimize . fmin ( f , start_params , args = fargs , xtol = xtol , <EOL> ftol = ftol , maxiter = maxiter , maxfun = maxfun , <EOL> full_output = full_output , disp = disp , retall = retall , <EOL> callback = callback ) <EOL> if full_output : <EOL> if not retall : <EOL> xopt , fopt , niter , fcalls , warnflag = retvals <EOL> else : <EOL> xopt , fopt , niter , fcalls , warnflag , allvecs = retvals <EOL> converged = not warnflag <EOL> retvals = { '<STR_LIT>' : fopt , '<STR_LIT>' : niter , <EOL> '<STR_LIT>' : fcalls , '<STR_LIT>' : warnflag , <EOL> '<STR_LIT>' : converged } <EOL> if retall : <EOL> retvals . update ( { '<STR_LIT>' : allvecs } ) <EOL> else : <EOL> xopt = retvals <EOL> retvals = None <EOL> return xopt , retvals <EOL> def _fit_cg ( f , score , start_params , fargs , kwargs , disp = True , <EOL> maxiter = <NUM_LIT:100> , callback = None , retall = False , <EOL> full_output = True , hess = None ) : <EOL> gtol = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> norm = kwargs . setdefault ( '<STR_LIT>' , np . Inf ) <EOL> epsilon = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> retvals = optimize . fmin_cg ( f , start_params , score , gtol = gtol , norm = norm , <EOL> epsilon = epsilon , maxiter = maxiter , <EOL> full_output = full_output , disp = disp , <EOL> retall = retall , callback = callback ) <EOL> if full_output : <EOL> if not retall : <EOL> xopt , fopt , fcalls , gcalls , warnflag = retvals <EOL> else : <EOL> xopt , fopt , fcalls , gcalls , warnflag , allvecs = retvals <EOL> converged = not warnflag <EOL> retvals = { '<STR_LIT>' : fopt , '<STR_LIT>' : fcalls , '<STR_LIT>' : gcalls , <EOL> '<STR_LIT>' : warnflag , '<STR_LIT>' : converged } <EOL> if retall : <EOL> retvals . update ( { '<STR_LIT>' : allvecs } ) <EOL> else : <EOL> xopt = retvals <EOL> retvals = None <EOL> return xopt , retvals <EOL> def _fit_ncg ( f , score , start_params , fargs , kwargs , disp = True , <EOL> maxiter = <NUM_LIT:100> , callback = None , retall = False , <EOL> full_output = True , hess = None ) : <EOL> fhess_p = kwargs . setdefault ( '<STR_LIT>' , None ) <EOL> avextol = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> epsilon = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> retvals = optimize . fmin_ncg ( f , start_params , score , fhess_p = fhess_p , <EOL> fhess = hess , args = fargs , avextol = avextol , <EOL> epsilon = epsilon , maxiter = maxiter , <EOL> full_output = full_output , disp = disp , <EOL> retall = retall , callback = callback ) <EOL> if full_output : <EOL> if not retall : <EOL> xopt , fopt , fcalls , gcalls , hcalls , warnflag = retvals <EOL> else : <EOL> xopt , fopt , fcalls , gcalls , hcalls , warnflag , allvecs = retvals <EOL> converged = not warnflag <EOL> retvals = { '<STR_LIT>' : fopt , '<STR_LIT>' : fcalls , '<STR_LIT>' : gcalls , <EOL> '<STR_LIT>' : hcalls , '<STR_LIT>' : warnflag , <EOL> '<STR_LIT>' : converged } <EOL> if retall : <EOL> retvals . update ( { '<STR_LIT>' : allvecs } ) <EOL> else : <EOL> xopt = retvals <EOL> retvals = None <EOL> return xopt , retvals <EOL> def _fit_powell ( f , score , start_params , fargs , kwargs , disp = True , <EOL> maxiter = <NUM_LIT:100> , callback = None , retall = False , <EOL> full_output = True , hess = None ) : <EOL> xtol = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> ftol = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> maxfun = kwargs . setdefault ( '<STR_LIT>' , None ) <EOL> start_direc = kwargs . setdefault ( '<STR_LIT>' , None ) <EOL> retvals = optimize . fmin_powell ( f , start_params , args = fargs , xtol = xtol , <EOL> ftol = ftol , maxiter = maxiter , maxfun = maxfun , <EOL> full_output = full_output , disp = disp , <EOL> retall = retall , callback = callback , <EOL> direc = start_direc ) <EOL> if full_output : <EOL> if not retall : <EOL> xopt , fopt , direc , niter , fcalls , warnflag = retvals <EOL> else : <EOL> xopt , fopt , direc , niter , fcalls , warnflag , allvecs = retvals <EOL> converged = not warnflag <EOL> retvals = { '<STR_LIT>' : fopt , '<STR_LIT>' : direc , '<STR_LIT>' : niter , <EOL> '<STR_LIT>' : fcalls , '<STR_LIT>' : warnflag , <EOL> '<STR_LIT>' : converged } <EOL> if retall : <EOL> retvals . update ( { '<STR_LIT>' : allvecs } ) <EOL> else : <EOL> xopt = retvals <EOL> retvals = None <EOL> return xopt , retvals <EOL> def _fit_basinhopping ( f , score , start_params , fargs , kwargs , disp = True , <EOL> maxiter = <NUM_LIT:100> , callback = None , retall = False , <EOL> full_output = True , hess = None ) : <EOL> if not '<STR_LIT>' in vars ( optimize ) : <EOL> msg = '<STR_LIT>' <EOL> raise ValueError ( msg ) <EOL> from copy import copy <EOL> kwargs = copy ( kwargs ) <EOL> niter = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT:100> ) <EOL> niter_success = kwargs . setdefault ( '<STR_LIT>' , None ) <EOL> T = kwargs . setdefault ( '<STR_LIT:T>' , <NUM_LIT:1.0> ) <EOL> stepsize = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT:0.5> ) <EOL> interval = kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT:50> ) <EOL> minimizer_kwargs = kwargs . get ( '<STR_LIT>' , { } ) <EOL> minimizer_kwargs [ '<STR_LIT:args>' ] = fargs <EOL> minimizer_kwargs [ '<STR_LIT>' ] = score <EOL> method = minimizer_kwargs . get ( '<STR_LIT>' , None ) <EOL> if method and method != '<STR_LIT>' : <EOL> minimizer_kwargs [ '<STR_LIT>' ] = hess <EOL> retvals = optimize . basinhopping ( f , start_params , <EOL> minimizer_kwargs = minimizer_kwargs , <EOL> niter = niter , niter_success = niter_success , <EOL> T = T , stepsize = stepsize , disp = disp , <EOL> callback = callback , interval = interval ) <EOL> if full_output : <EOL> xopt , fopt , niter , fcalls = map ( lambda x : getattr ( retvals , x ) , <EOL> [ '<STR_LIT:x>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> converged = '<STR_LIT>' in retvals . message [ <NUM_LIT:0> ] <EOL> retvals = { '<STR_LIT>' : fopt , '<STR_LIT>' : niter , <EOL> '<STR_LIT>' : fcalls , '<STR_LIT>' : converged } <EOL> else : <EOL> xopt = retvals . x <EOL> retvals = None <EOL> return xopt , retvals </s>
