Python scipy.optimize.fmin_ncg() Examples
The following are 11
code examples of scipy.optimize.fmin_ncg().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
scipy.optimize
, or try the search function
.
Example #1
Source File: optimizer.py From vnpy_crypto with MIT License | 5 votes |
def _fit_ncg(f, score, start_params, fargs, kwargs, disp=True, maxiter=100, callback=None, retall=False, full_output=True, hess=None): fhess_p = kwargs.setdefault('fhess_p', None) avextol = kwargs.setdefault('avextol', 1.0000000000000001e-05) epsilon = kwargs.setdefault('epsilon', 1.4901161193847656e-08) retvals = optimize.fmin_ncg(f, start_params, score, fhess_p=fhess_p, fhess=hess, args=fargs, avextol=avextol, epsilon=epsilon, maxiter=maxiter, full_output=full_output, disp=disp, retall=retall, callback=callback) if full_output: if not retall: xopt, fopt, fcalls, gcalls, hcalls, warnflag = retvals else: xopt, fopt, fcalls, gcalls, hcalls, warnflag, allvecs =\ retvals converged = not warnflag retvals = {'fopt': fopt, 'fcalls': fcalls, 'gcalls': gcalls, 'hcalls': hcalls, 'warnflag': warnflag, 'converged': converged} if retall: retvals.update({'allvecs': allvecs}) else: xopt = retvals retvals = None return xopt, retvals
Example #2
Source File: gmm.py From vnpy_crypto with MIT License | 5 votes |
def fitgmm_cu(self, start, optim_method='bfgs', optim_args=None): '''estimate parameters using continuously updating GMM Parameters ---------- start : array_like starting values for minimization Returns ------- paramest : array estimated parameters Notes ----- todo: add fixed parameter option, not here ??? uses scipy.optimize.fmin ''' ## if not fixed is None: #fixed not defined in this version ## raise NotImplementedError if optim_args is None: optim_args = {} if optim_method == 'nm': optimizer = optimize.fmin elif optim_method == 'bfgs': optimizer = optimize.fmin_bfgs optim_args['fprime'] = self.score_cu elif optim_method == 'ncg': optimizer = optimize.fmin_ncg else: raise ValueError('optimizer method not available') #TODO: add other optimization options and results return optimizer(self.gmmobjective_cu, start, args=(), **optim_args)
Example #3
Source File: test_optimize.py From Computable with MIT License | 5 votes |
def test_ncg(self, use_wrapper=False): """ line-search Newton conjugate gradient optimization routine """ if use_wrapper: opts = {'maxiter': self.maxiter, 'disp': False, 'return_all': False} retval = optimize.minimize(self.func, self.startparams, method='Newton-CG', jac=self.grad, args=(), options=opts)['x'] else: retval = optimize.fmin_ncg(self.func, self.startparams, self.grad, args=(), maxiter=self.maxiter, full_output=False, disp=False, retall=False) params = retval assert_allclose(self.func(params), self.func(self.solution), atol=1e-6) # Ensure that function call counts are 'known good'; these are from # Scipy 0.7.0. Don't allow them to increase. assert_(self.funccalls == 7, self.funccalls) assert_(self.gradcalls <= 22, self.gradcalls) # 0.13.0 #assert_(self.gradcalls <= 18, self.gradcalls) # 0.9.0 #assert_(self.gradcalls == 18, self.gradcalls) # 0.8.0 #assert_(self.gradcalls == 22, self.gradcalls) # 0.7.0 # Ensure that the function behaves the same; this is from Scipy 0.7.0 assert_allclose(self.trace[3:5], [[-4.35700753e-07, -5.24869435e-01, 4.87527480e-01], [-4.35700753e-07, -5.24869401e-01, 4.87527774e-01]], atol=1e-6, rtol=1e-7)
Example #4
Source File: test_optimize.py From Computable with MIT License | 5 votes |
def test_ncg_hess(self, use_wrapper=False): """ Newton conjugate gradient with Hessian """ if use_wrapper: opts = {'maxiter': self.maxiter, 'disp': False, 'return_all': False} retval = optimize.minimize(self.func, self.startparams, method='Newton-CG', jac=self.grad, hess=self.hess, args=(), options=opts)['x'] else: retval = optimize.fmin_ncg(self.func, self.startparams, self.grad, fhess=self.hess, args=(), maxiter=self.maxiter, full_output=False, disp=False, retall=False) params = retval assert_allclose(self.func(params), self.func(self.solution), atol=1e-6) # Ensure that function call counts are 'known good'; these are from # Scipy 0.7.0. Don't allow them to increase. assert_(self.funccalls == 7, self.funccalls) assert_(self.gradcalls <= 18, self.gradcalls) # 0.9.0 # assert_(self.gradcalls == 18, self.gradcalls) # 0.8.0 # assert_(self.gradcalls == 22, self.gradcalls) # 0.7.0 # Ensure that the function behaves the same; this is from Scipy 0.7.0 assert_allclose(self.trace[3:5], [[-4.35700753e-07, -5.24869435e-01, 4.87527480e-01], [-4.35700753e-07, -5.24869401e-01, 4.87527774e-01]], atol=1e-6, rtol=1e-7)
Example #5
Source File: test_optimize.py From Computable with MIT License | 5 votes |
def test_ncg_hessp(self, use_wrapper=False): """ Newton conjugate gradient with Hessian times a vector p """ if use_wrapper: opts = {'maxiter': self.maxiter, 'disp': False, 'return_all': False} retval = optimize.minimize(self.func, self.startparams, method='Newton-CG', jac=self.grad, hessp=self.hessp, args=(), options=opts)['x'] else: retval = optimize.fmin_ncg(self.func, self.startparams, self.grad, fhess_p=self.hessp, args=(), maxiter=self.maxiter, full_output=False, disp=False, retall=False) params = retval assert_allclose(self.func(params), self.func(self.solution), atol=1e-6) # Ensure that function call counts are 'known good'; these are from # Scipy 0.7.0. Don't allow them to increase. assert_(self.funccalls == 7, self.funccalls) assert_(self.gradcalls <= 18, self.gradcalls) # 0.9.0 # assert_(self.gradcalls == 18, self.gradcalls) # 0.8.0 # assert_(self.gradcalls == 22, self.gradcalls) # 0.7.0 # Ensure that the function behaves the same; this is from Scipy 0.7.0 assert_allclose(self.trace[3:5], [[-4.35700753e-07, -5.24869435e-01, 4.87527480e-01], [-4.35700753e-07, -5.24869401e-01, 4.87527774e-01]], atol=1e-6, rtol=1e-7)
Example #6
Source File: test_optimize.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_ncg(self): # line-search Newton conjugate gradient optimization routine if self.use_wrapper: opts = {'maxiter': self.maxiter, 'disp': self.disp, 'return_all': False} retval = optimize.minimize(self.func, self.startparams, method='Newton-CG', jac=self.grad, args=(), options=opts)['x'] else: retval = optimize.fmin_ncg(self.func, self.startparams, self.grad, args=(), maxiter=self.maxiter, full_output=False, disp=self.disp, retall=False) params = retval assert_allclose(self.func(params), self.func(self.solution), atol=1e-6) # Ensure that function call counts are 'known good'; these are from # Scipy 0.7.0. Don't allow them to increase. assert_(self.funccalls == 7, self.funccalls) assert_(self.gradcalls <= 22, self.gradcalls) # 0.13.0 #assert_(self.gradcalls <= 18, self.gradcalls) # 0.9.0 #assert_(self.gradcalls == 18, self.gradcalls) # 0.8.0 #assert_(self.gradcalls == 22, self.gradcalls) # 0.7.0 # Ensure that the function behaves the same; this is from Scipy 0.7.0 assert_allclose(self.trace[3:5], [[-4.35700753e-07, -5.24869435e-01, 4.87527480e-01], [-4.35700753e-07, -5.24869401e-01, 4.87527774e-01]], atol=1e-6, rtol=1e-7)
Example #7
Source File: test_optimize.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_ncg_hess(self): # Newton conjugate gradient with Hessian if self.use_wrapper: opts = {'maxiter': self.maxiter, 'disp': self.disp, 'return_all': False} retval = optimize.minimize(self.func, self.startparams, method='Newton-CG', jac=self.grad, hess=self.hess, args=(), options=opts)['x'] else: retval = optimize.fmin_ncg(self.func, self.startparams, self.grad, fhess=self.hess, args=(), maxiter=self.maxiter, full_output=False, disp=self.disp, retall=False) params = retval assert_allclose(self.func(params), self.func(self.solution), atol=1e-6) # Ensure that function call counts are 'known good'; these are from # Scipy 0.7.0. Don't allow them to increase. assert_(self.funccalls == 7, self.funccalls) assert_(self.gradcalls <= 18, self.gradcalls) # 0.9.0 # assert_(self.gradcalls == 18, self.gradcalls) # 0.8.0 # assert_(self.gradcalls == 22, self.gradcalls) # 0.7.0 # Ensure that the function behaves the same; this is from Scipy 0.7.0 assert_allclose(self.trace[3:5], [[-4.35700753e-07, -5.24869435e-01, 4.87527480e-01], [-4.35700753e-07, -5.24869401e-01, 4.87527774e-01]], atol=1e-6, rtol=1e-7)
Example #8
Source File: test_optimize.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_ncg_hessp(self): # Newton conjugate gradient with Hessian times a vector p. if self.use_wrapper: opts = {'maxiter': self.maxiter, 'disp': self.disp, 'return_all': False} retval = optimize.minimize(self.func, self.startparams, method='Newton-CG', jac=self.grad, hessp=self.hessp, args=(), options=opts)['x'] else: retval = optimize.fmin_ncg(self.func, self.startparams, self.grad, fhess_p=self.hessp, args=(), maxiter=self.maxiter, full_output=False, disp=self.disp, retall=False) params = retval assert_allclose(self.func(params), self.func(self.solution), atol=1e-6) # Ensure that function call counts are 'known good'; these are from # Scipy 0.7.0. Don't allow them to increase. assert_(self.funccalls == 7, self.funccalls) assert_(self.gradcalls <= 18, self.gradcalls) # 0.9.0 # assert_(self.gradcalls == 18, self.gradcalls) # 0.8.0 # assert_(self.gradcalls == 22, self.gradcalls) # 0.7.0 # Ensure that the function behaves the same; this is from Scipy 0.7.0 assert_allclose(self.trace[3:5], [[-4.35700753e-07, -5.24869435e-01, 4.87527480e-01], [-4.35700753e-07, -5.24869401e-01, 4.87527774e-01]], atol=1e-6, rtol=1e-7)
Example #9
Source File: optimizer.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _fit_ncg(f, score, start_params, fargs, kwargs, disp=True, maxiter=100, callback=None, retall=False, full_output=True, hess=None): fhess_p = kwargs.setdefault('fhess_p', None) avextol = kwargs.setdefault('avextol', 1.0000000000000001e-05) epsilon = kwargs.setdefault('epsilon', 1.4901161193847656e-08) retvals = optimize.fmin_ncg(f, start_params, score, fhess_p=fhess_p, fhess=hess, args=fargs, avextol=avextol, epsilon=epsilon, maxiter=maxiter, full_output=full_output, disp=disp, retall=retall, callback=callback) if full_output: if not retall: xopt, fopt, fcalls, gcalls, hcalls, warnflag = retvals else: xopt, fopt, fcalls, gcalls, hcalls, warnflag, allvecs =\ retvals converged = not warnflag retvals = {'fopt': fopt, 'fcalls': fcalls, 'gcalls': gcalls, 'hcalls': hcalls, 'warnflag': warnflag, 'converged': converged} if retall: retvals.update({'allvecs': allvecs}) else: xopt = retvals retvals = None return xopt, retvals
Example #10
Source File: gmm.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def fitgmm_cu(self, start, optim_method='bfgs', optim_args=None): '''estimate parameters using continuously updating GMM Parameters ---------- start : array_like starting values for minimization Returns ------- paramest : array estimated parameters Notes ----- todo: add fixed parameter option, not here ??? uses scipy.optimize.fmin ''' ## if not fixed is None: #fixed not defined in this version ## raise NotImplementedError if optim_args is None: optim_args = {} if optim_method == 'nm': optimizer = optimize.fmin elif optim_method == 'bfgs': optimizer = optimize.fmin_bfgs optim_args['fprime'] = self.score_cu elif optim_method == 'ncg': optimizer = optimize.fmin_ncg else: raise ValueError('optimizer method not available') #TODO: add other optimization options and results return optimizer(self.gmmobjective_cu, start, args=(), **optim_args)
Example #11
Source File: test_optimize.py From GraphicDesignPatternByPython with MIT License | 4 votes |
def test_minimize_callback_copies_array(self, method): # Check that arrays passed to callbacks are not modified # inplace by the optimizer afterward if method in ('fmin_tnc', 'fmin_l_bfgs_b'): func = lambda x: (optimize.rosen(x), optimize.rosen_der(x)) else: func = optimize.rosen jac = optimize.rosen_der hess = optimize.rosen_hess x0 = np.zeros(10) # Set options kwargs = {} if method.startswith('fmin'): routine = getattr(optimize, method) if method == 'fmin_slsqp': kwargs['iter'] = 5 elif method == 'fmin_tnc': kwargs['maxfun'] = 100 else: kwargs['maxiter'] = 5 else: def routine(*a, **kw): kw['method'] = method return optimize.minimize(*a, **kw) if method == 'TNC': kwargs['options'] = dict(maxiter=100) else: kwargs['options'] = dict(maxiter=5) if method in ('fmin_ncg',): kwargs['fprime'] = jac elif method in ('Newton-CG',): kwargs['jac'] = jac elif method in ('trust-krylov', 'trust-exact', 'trust-ncg', 'dogleg', 'trust-constr'): kwargs['jac'] = jac kwargs['hess'] = hess # Run with callback results = [] def callback(x, *args, **kwargs): results.append((x, np.copy(x))) sol = routine(func, x0, callback=callback, **kwargs) # Check returned arrays coincide with their copies and have no memory overlap assert_(len(results) > 2) assert_(all(np.all(x == y) for x, y in results)) assert_(not any(np.may_share_memory(x[0], y[0]) for x, y in itertools.combinations(results, 2)))