Skip to content Skip to sidebar Skip to footer

Python Lmfit Restriction Fit Parameters

I'm trying to fit a function to some data in Python using the LMFIT library for nonlinear functions. It's easy enough, but I want to know if there's a way to restrict some properti

Solution 1:

The problem with the standard fit is the estimate of the Jacobian. If a parameter is discrete the derivative is zero almost everywhere. A workaround might be that one uses leastsq with a self defined residual function and additionally providing the derivatives. One can set the parameter discrete in the residual function but let it be continuous in the derivative. I'm not saying that this is the general solution to this type of problem, but in case of the OP's function, it works quite OK.

Edit - Code would be:

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import leastsq

def f0( x, A, B, C ):
    return ( A * np.cos( x )**2 + B * np.sin( x )**2 + 2 * C * np.sin( x ) * np.cos( x ) )

def func(x, A, B, C):
    return f0( x, A, B, C )**2

a = 0.009
b = 0.3
c = 0.4

xList = np.linspace( -1, 6, 500 )
yList = np.fromiter( ( func( x, a, b, c ) forx in xList ), np.float )


def residuals( p, x, y ):
    return func(x, p[0], int(p[1]) * np.pi / 2. * p[0], p[2] ) - y

def dfunc( p, x, y ):     #Derivativereturn [ 
        f0( x, p[0], int( p[1] ) * np.pi / 2. * p[0] , p[2] ) * ( np.cos( x )**2 + p[1] * np.pi / 2. * np.sin( x )**2 ),
        f0( x, p[0], int( p[1] ) * np.pi / 2. * p[0] , p[2] ) * ( p[0] * np.pi / 2.* np.sin( x )**2 ),
        f0( x, p[0], int( p[1] ) * np.pi / 2. * p[0] , p[2] ) * ( 2 * np.sin( x ) * np.cos( x ) ),
     ]

plsq, cov, infodict, mesg, ier = leastsq( residuals, [ 0.009, .3/.01, .4 ], args=( xList, yList ), Dfun=dfunc, col_deriv=1, full_output=True )

fit = func(xList, plsq[0], int( plsq[1] ) * np.pi / 2. * plsq[0],  plsq[2] )
print plsq
printint( plsq[1] ) 
fig1 = plt.figure( 1, figsize=( 6, 4 ), dpi=80 )
ax = fig1.add_subplot( 1, 1, 1 )
ax.plot( xList, yList )
ax.plot( xList, fit, ls='--')
plt.show()

Providing:

>>[8.68421935e-03 2.22248626e+01 4.00032135e-01]>>22

data and fit

Solution 2:

I think the answer is No. The solvers in scipy.optimize that lmfit wraps do not support discrete variables,only continuous variables.

Post a Comment for "Python Lmfit Restriction Fit Parameters"