问题
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 properties of the fitted values.
For example, in the following code I fit my data to optimize values A, B and C. But I also want the ratio of A to B to be pi/4 times some integer. Is there a way to impose this restriction?
from lmfit import Model
import numpy
from numpy import cos, sin, pi, linspace
Upload data:
data = numpy.genfromtxt('data')
axis = numpy.genfromtxt('axis')
Define function:
def func(x, A, B, C):
return (A*cos(x)*cos(x) + B*sin(x)*sin(x) + 2*C*sin(x)*cos(x))**2
I must make an initial guess for my parameters:
a = 0.009
b = 0.3
c = 0.3
Then create a model to fit my function:
func_model = Model(func)
Fit the function to input data, with initial guesses (A = a, B = b, C = c):
result = func_model.fit(data, x=axis, A = a, B = b, C = c)
fitted_vals = result.best_values #dictionary structure
Afit = fitted_vals['A']
Bfit = fitted_vals['B']
Cfit = fitted_vals['C']
How can I make sure that the ratio of Afit to Bfit is pi/4 times some integer?
If it's not possible, is anyone aware of software that has this capability?
回答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 ) for x 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 ): #Derivative
return [
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
print int( 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
回答2:
I think the answer is No. The solvers in scipy.optimize that lmfit wraps do not support discrete variables,only continuous variables.
来源:https://stackoverflow.com/questions/50276206/python-lmfit-restriction-fit-parameters