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>