Optimization on the subspace of a domain
Hello there, As I am learning how to do minimization using SciPy, I wonder if I can do the minimization just on a (proper) subspace of the domain of a function. For instance, if I have a multivariate function f(x,y,z), can I ask SciPy to find a minimum by changing only x and y and keeping z fixed? It would work if I define a new function: f(x,y,z_0) = h(x,y) for a given z_0, but I wonder if there is an option for doing that without defining another function. Thanks.
On 23 August 2013 22:49, Cy. T. <dragontse@hotmail.com> wrote:
It would work if I define a new function: f(x,y,z_0) = h(x,y) for a given z_0, but I wonder if there is an option for doing that without defining another function.
A lambda function will do the trick avoiding having to give it a new name: minimize(lambda x, y: f(x, y, z0), ...) This only works for very simple subdomains of the function, though. David.
Hi, On Fri, Aug 23, 2013 at 3:49 PM, Cy. T. <dragontse@hotmail.com> wrote:
Hello there,
As I am learning how to do minimization using SciPy, I wonder if I can do the minimization just on a (proper) subspace of the domain of a function.
For instance, if I have a multivariate function f(x,y,z), can I ask SciPy to find a minimum by changing only x and y and keeping z fixed?
It would work if I define a new function: f(x,y,z_0) = h(x,y) for a given z_0, but I wonder if there is an option for doing that without defining another function.
Thanks.
Depending on your needs, you might find the lmfit-py package ( https://github.com/newville/lmfit-py) useful. With this approach, you would write your function once in terms of a set of Parameters: import lmfit def f(params, *args, **kwargs): x = params['x'].value y = params['y'].value z = params['z'].value # do calculation ... return value_to_minimize params = lmfit.Parameters() params.add('x', value= 10.0) params.add('y', value=2.0, min=0) params.add('z', value=0, vary=False) lmfit.minimize(f, params) print( lmfit.report_fit(params)) Each Parameter can be varied or fixed (vary=False), have upper and/or lower bounds placed on its value, or have its value evaluated as a function of other Parameters. All of these parameter settings can be changed independently of the implementation of the objective function. By default, the least-squares minimization from scipy.optimize.leastsq() is used (and so value_to_minimize should be an array that will be minimized in the least-squares sense), but the scalar minimization methods like Nelder-Mead can also be used. For leastsq(), uncertainties and correlations in the variables are reported from the covariance matrix, and confidence levels can also be determined more explicitly. Cheers, --Matt Newville <newville at cars.uchicago.edu>
Thank you both for these great solutions! From: newville@cars.uchicago.edu Date: Fri, 23 Aug 2013 17:03:39 -0500 To: scipy-user@scipy.org Subject: Re: [SciPy-User] Optimization on the subspace of a domain Hi, On Fri, Aug 23, 2013 at 3:49 PM, Cy. T. <dragontse@hotmail.com> wrote: Hello there, As I am learning how to do minimization using SciPy, I wonder if I can do the minimization just on a (proper) subspace of the domain of a function. For instance, if I have a multivariate function f(x,y,z), can I ask SciPy to find a minimum by changing only x and y and keeping z fixed? It would work if I define a new function: f(x,y,z_0) = h(x,y) for a given z_0, but I wonder if there is an option for doing that without defining another function. Thanks. Depending on your needs, you might find the lmfit-py package (https://github.com/newville/lmfit-py) useful. With this approach, you would write your function once in terms of a set of Parameters: import lmfit def f(params, *args, **kwargs): x = params['x'].value y = params['y'].value z = params['z'].value # do calculation ... return value_to_minimize params = lmfit.Parameters() params.add('x', value= 10.0) params.add('y', value=2.0, min=0) params.add('z', value=0, vary=False) lmfit.minimize(f, params) print( lmfit.report_fit(params)) Each Parameter can be varied or fixed (vary=False), have upper and/or lower bounds placed on its value, or have its value evaluated as a function of other Parameters. All of these parameter settings can be changed independently of the implementation of the objective function. By default, the least-squares minimization from scipy.optimize.leastsq() is used (and so value_to_minimize should be an array that will be minimized in the least-squares sense), but the scalar minimization methods like Nelder-Mead can also be used. For leastsq(), uncertainties and correlations in the variables are reported from the covariance matrix, and confidence levels can also be determined more explicitly. Cheers, --Matt Newville <newville at cars.uchicago.edu> _______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
participants (3)
-
Cy. T. -
Daπid -
Matt Newville