pythonscipycurve-fittingsymfit

Setting a negative fixed parameter in the symfit python package


I need to fit two out of the three values in a parameter, say I have a,b, and c and I know a but I just want to fit b and c. The issue becomes if I try to fix a to a negative value I the following error ValueError: SLSQP Error: lb > ub in bounds True, False, False.. However, if I change my model by adding a negative (-) sign in front of the a so my a parameter is positive it will solve, but it returns all NaN's.

Here is a code that returns the error. This is how I would like to structure the model if possible.

#%% import modules
import IPython as IP
IP.get_ipython().magic('reset -sf')
import matplotlib.pyplot as plt
from symfit import Parameter, Variable
from symfit import Fit
import numpy as np

# data
Y = np.array([7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.50E-01 , 7.49E-01 , 7.49E-01 , 7.49E-01 , 7.49E-01 , 7.49E-01 , 7.48E-01 , 7.48E-01 , 7.48E-01 , 7.47E-01 , 7.47E-01 , 7.46E-01 , 7.46E-01 , 7.45E-01 , 7.44E-01 , 7.44E-01 , 7.43E-01 , 7.42E-01 , 7.41E-01 , 7.40E-01 , 7.39E-01 , 7.38E-01 , 7.37E-01 , 7.36E-01 , 7.35E-01 , 7.34E-01 , 7.32E-01 , 7.31E-01 , 7.30E-01 , 7.28E-01 , 7.27E-01 , 7.25E-01 , 7.24E-01 , 7.22E-01 , 7.21E-01 , 7.19E-01 , 7.17E-01 , 7.16E-01 , 7.14E-01 , 7.12E-01 , 7.10E-01 , 7.08E-01 , 7.06E-01 , 7.04E-01 , 7.02E-01 , 7.00E-01 , 6.98E-01 , 6.96E-01 , 6.94E-01 , 6.92E-01 , 6.90E-01 , 6.88E-01 , 6.86E-01 , 6.83E-01 , 6.81E-01 , 6.79E-01 , 6.77E-01 , 6.75E-01 , 6.72E-01 , 6.70E-01 , 6.68E-01 , 6.65E-01 , 6.63E-01 , 6.61E-01 , 6.58E-01 , 6.56E-01 , 6.54E-01 , 6.51E-01 , 6.49E-01 , 6.46E-01 , 6.44E-01 , 6.42E-01 , 6.39E-01 , 6.37E-01 , 6.34E-01 , 6.32E-01 , 6.30E-01 , 6.27E-01 , 6.25E-01 , 6.22E-01 , 6.20E-01 , 6.17E-01 , 6.15E-01 , 6.12E-01 , 6.10E-01 , 6.08E-01 , 6.05E-01 , 6.03E-01 , 6.00E-01 , 5.98E-01 , 5.95E-01 , 5.93E-01 , 5.91E-01 , 5.88E-01 , 5.86E-01 , 5.83E-01 , 5.81E-01 , 5.79E-01 , 5.76E-01 , 5.74E-01 , 5.71E-01 , 5.69E-01 , 5.67E-01 , 5.64E-01 , 5.62E-01 , 5.60E-01 , 5.57E-01 , 5.55E-01 , 5.53E-01 , 5.50E-01 , 5.48E-01 , 5.46E-01 , 5.43E-01 , 5.41E-01 , 5.39E-01 , 5.36E-01 , 5.34E-01 , 5.32E-01 , 5.29E-01 , 5.27E-01 , 5.25E-01])
X = np.arange(len(Y))


#%% solve the best fit line for the entire data set.

# define the parameters
a = Parameter(value = -0.25,fixed=True)
b = Parameter()
c = Parameter()
x = Variable()


# build the model
model = a + b * (1 - np.e**(-c/x))

# fit the model
fit = Fit(model, X, Y)
fit_result = fit.execute()
model_fit = model(x=X, a=fit_result.value(a), b=fit_result.value(b),c=fit_result.value(c))


#%% Plot the results
plt.figure()
plt.plot(X,Y,'ko',markersize=3,fillstyle='none',label='data')
plt.plot(X, model_fit,'--',label='best fit')
plt.legend()

Solution

  • This seems to be an issue with symfit, thanks for reporting it.

    As a quick solution to your problem; it seems that SLSQP is causing the NaN's. Therefore, switching from the Fit object to the NumericalLeastSquares object will solve your problem. (But it will still not allow fixed negative values)

    I will try to patch this as quickly as possible.

    As an additional improvement to your code, might I suggest replacing np.e**(-c/x) by

    from symfit import exp
    exp(-c/x)
    

    This is a symbolic exp so symfit (or you) can leverage all the power of symbolic computation on it as well.