def fmin_lm_log_params(m, params, *args, **kwargs): """ Minimize the cost of a model using Levenberg-Marquadt in terms of log parameters. The *args and **kwargs represent additional parmeters that will be passed to the optimization algorithm. For your convenience, the docstring of that function is appended below: """ Nres = len(m.residuals) def func(log_params): try: return m.res_log_params(log_params) except Utility.SloppyCellException: logger.warn('Exception in cost evaluation. Cost set to inf.') return [scipy.inf] * Nres jac = lambda lp: scipy.asarray(m.jacobian_log_params_sens(lp)) sln = lmopt.fmin_lm(f=func, x0=scipy.log(params), fprime=jac, *args, **kwargs) if isinstance(params, KeyedList): pout = params.copy() pout.update(scipy.exp(sln)) return pout else: return scipy.exp(sln)
def fmin_lm(m, params, *args, **kwargs): """ Minimize the cost of a model using Levenberg-Marquardt. """ jac = lambda p: scipy.asarray(m.jacobian_sens(p)) sln = lmopt.fmin_lm(f=m.res, x0=params, fprime=jac, *args, **kwargs) return sln
def fmin_lm_log_params_fd(m, params, relativeScale=False, stepSizeCutoff=None, *args, **kwargs): """ Minimize the cost of a model using Levenberg-Marquadt in terms of log parameters. This methods uses finite differences instead of sensitivity integration convinient when using ad hoc residuals like period and amplitud check The *args and **kwargs represent additional parmeters that will be passed to the optimization algorithm. For your convenience, the docstring of that function is appended below: """ Nres = len(m.residuals) def func(log_params): try: return m.res_log_params(log_params) except Utility.SloppyCellException: logger.warn('Exception in cost evaluation. Cost set to inf.') return [scipy.inf] * Nres sln = lmopt.fmin_lm(f=func, x0=scipy.log(params), *args, **kwargs) if isinstance(params, KeyedList): pout = params.copy() pout.update(scipy.exp(sln)) return pout else: return scipy.exp(sln)
def test_lm_fmin(self): """Test that Levenberg-Marquardt algortihm runs correctly""" p = m.params.__copy__() p.update([1.0, 1.0]) res_func = m.res_log_params def res_func_prime(x): j, jtj = m.GetJandJtJInLogParameters(x) jarray = np.zeros((len(list(j.keys())), len(m.params)), scipy.float_) for resind, resname in enumerate(m.residuals.keys()): jarray[resind, :] = j.get(resname) return jarray #print "\n Initial cost", m.cost(m.params) pbest = lmopt.fmin_lm(res_func, np.log(p), res_func_prime, maxiter=20, disp=0, full_output=1) m.params.update(np.exp(pbest[0])) newcost = m.cost(m.params) #print "Cost after 20 iterations", newcost # The intial cost is about 25, the final cost should be # about 1.0e-6 self.assertAlmostEqual(newcost, 0.0, 4, 'Failed on lm minimum')
def fmin_lm_log_params(m, params, *args, **kwargs): """ Minimize the cost of a model using Levenberg-Marquardt in terms of log parameters. """ jac = lambda lp: scipy.asarray(m.jacobian_log_params_sens(lp)) sln = lmopt.fmin_lm(f=m.res_log_params, x0=scipy.log(params), fprime=jac, *args, **kwargs) return ( scipy.exp(sln[0]), ) + sln[1:]
def fit_lm_sloppycell(self, p0=None, in_logp=True, *args, **kwargs): """Get the best fit using Levenberg-Marquardt algorithm. Input: p0: initial guess in_logp: optimizing in log parameters *args and **kwargs: additional parmeters to be passed to SloppyCell.lm_opt.fmin_lm, whose docstring is appended below: Output: out: a Series """ if p0 is None: p0 = self.p0 else: p0 = Series(p0, self.pids) if in_logp: res = self.get_in_logp() p0 = p0.log() else: res = self keys = ['args', 'avegtol', 'epsilon', 'maxiter', 'full_output', 'disp', 'retall', 'lambdainit', 'jinit', 'trustradius'] kwargs_lm = butil.get_submapping(kwargs, f_key=lambda k: k in keys) kwargs_lm['full_output'] = True kwargs_lm['retall'] = True p, cost, nfcall, nDfcall, convergence, lamb, Df, ps =\ lmopt.fmin_lm(f=res.r, x0=p0, fprime=res.Dr, *args, **kwargs_lm) #lmopt.fmin_lm(f=res.r, x0=p0, fprime=None, *args, **kwargs_lm) if in_logp: p = np.exp(p) ps = np.exp(np.array(ps)) p = Series(p, self.pids) ps = DF(ps, columns=self.pids) ps.index.name = 'step' out = Series(OD([('p', p), ('cost', cost)])) if kwargs.get('full_output', False): out.nfcall = nfcall out.nDfcall = nDfcall out.convergence = convergence out.lamb = lamb out.Df = Df if kwargs.get('retall', False): out.ps = ps return out
def fit_lm_sloppycell(res, p0=None, in_logp=True, **kwargs): """Get the best fit using Levenberg-Marquardt algorithm. Input: p0: initial guess in_logp: optimizing in log parameters *args and **kwargs: additional parameters to be passed to SloppyCell.lm_opt.fmin_lm, whose docstring is appended below: Output: out: a Series """ if p0 is None: p0 = res.p0 else: p0 = Series(p0, res.pids) if in_logp: res = res.get_in_logp() p0 = p0.log() else: res = res keys = [ 'args', 'avegtol', 'epsilon', 'maxiter', 'full_output', 'disp', 'retall', 'lambdainit', 'jinit', 'trustradius' ] kwargs_lm = butil.get_submapping(kwargs, f_key=lambda k: k in keys) kwargs_lm['full_output'] = True kwargs_lm['retall'] = True p, cost, nfcall, nDfcall, convergence, lamb, Df, ps =\ lmopt.fmin_lm(f=res.r, x0=p0, fprime=res.Dr, **kwargs_lm) if in_logp: p = np.exp(p) ps = np.exp(ps) fit = Fit(p=p, ps=ps, cost=cost, pids=res.pids, nfcall=nfcall, nDfcall=nDfcall, convergence=convergence, lamb=lamb, Df=Df) return fit