fmin with a threshold function
-
21-06-2021 - |
Question
I have some experimental data I would like to fit by fmin function. I have already used fmin with another sample and everything was ok.
This time I would to represent data by a function like :
def f3(c,t):
resultat=0
test=t-c[0]
if (test<0):
resultat = c[1]
else:
resultat = c[1]+c[2]*(t-c[0])+c[3]*(t-c[0])*(t-c[0])+c[4]*(t-c[0])*(t-c[0])*(t-c[0])
return resultat
t is a time vector and c[0] is a threshold (c[i] for the polynomial coefficients) (by the way the data can be represented by this function because I used it to generate the data example!)
The function error is given by :
def e3(p,temps,y):
error = 0
i=0
for t in temps:
error = error + (f3(p,t)-y[i])**2
i=i+1
#cont = min(0,p[0])
#error = error +10000*(cont*cont)*(cont*cont*cont*cont)
return error
p3=[10,250,0,0,0] and T_out is a vector
The fitting operation is :
p3_min = fmin(e3,p3[:], args=(temps,T_out),xtol=0.0001,ftol=0.0001)
This instruction works very well for "classic" polynomial function but for the f3 function (with a 'if' in it), p3_min is not as optimized as expected.
I tried to implement constraint like p[0]>0 or increase the number of iterations but I cannot manage to get the expected value.
Has anyone ever tried to fit data with a "threshold" function? I would be grateful to get some help.
Solution
The threshold function you are trying to optimize has a higher degree of non-linearity and is thus much harder to optimize using functions like fmin.
You may have better luck trying to do a classification step to estimate the value of your threshold. And only then to perform an optimization but knowing the threshold value.
The scikit learn is a wonderful tool for such tasks :
http://scikit-learn.org/stable/modules/classes.html#module-sklearn.svm