[SciPy-dev] parameter control in scipy.optimize.leastsq

Maximilian Fabricius mxhf@gmx....
Sun Oct 19 10:28:14 CDT 2008


On Sun, Oct 19, 2008 at 12:52 PM, Robin <robince@gmail.com> wrote:
> On Sun, Oct 19, 2008 at 11:28 AM, Maximilian Fabricius <mxhf@gmx.net> wrote:
>> Robin,
>>
>> thank you for the quick reply.
>> Indeed, but this would still involve to swap parameters around between
>> fixed and non-fixed and
>> changing the lambda function manually rather then just switching the
>> fissint on and off
>> with a single variable.
>>
>> But no doubt, a wrapper can be done.
>
> I couldn't think of a way to squeeze it into a lamdba single
> statement, but something like this would do:
>
> def fit_function(x, fit, y):
>    p0 = zeros(fit.size)
>    p0[fit] = x
>    p0[~fit] = y
>
> where fit is a boolean array, x is the ones to optimised and y are the
> fixed values.
> You have the extra overhead of the p0 array allocation for every
> iteration of the function - but perhaps that is worth it for the
> convenience.
>
> Robin
> _______________________________________________
> Scipy-dev mailing list
> Scipy-dev@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-dev
>


Robin,

great, nice idea! As this may be of use to others, my solution now is:


def fit_function(p_fit, fit, p_fixed, ... ):
        # assemble p_fit and p_fixed into a single array
        # while restoring the correct order of the original p0 array.
        p = zeros(fit.size)
        p[ where(fit == True) ] = p_fit
        p[ where(fit == False) ] = p_fixed
        # calls the original residuals routine
        return residuals(p, ... )

main():
        # best guesses
        p0 = zeros(10)
        p0[0] = vrot_d
        p0[1] = vsig_d
        ....

        # choose which parameters to fit
        fit = zeros(len(p0))
        fit[0:7] = True
        fit[8] = False
        fit[9] = False

        # store parameters which should be fit in p_fit
        #  all the others in p_fixed
        p_fit = p0[ where(fit == True) ]
        p_fixed = p0[ where(fit == False) ]
        plsq = leastsq(fit_function, p_fit, args=(fit, p_fixed,  ...) )

Thanks!

Cheers,

Maximilian


More information about the Scipy-dev mailing list