2

I have a little problem using the curve_fit function included in Scipy. Here is the function I would like to fit :

def funclog(x, a, b, c, d):
   return a * np.log(b * x + c) + d

The problem I have is that I would like the fit function to have a specific value on some points (y(min)=0 and y(max)=1). How can I force these points with curve_fit ?

Thank you

Vlad
  • 23
  • 5

2 Answers2

1

The requirement of the fit having specific values at x=0, x=1, implies that the parameters a, b, c, d are constrained according to the set of two equations:

funclog(0, a, b, c, d) = 0, funclog(1, a, b, c, d) = 1

For the form of funclog you are considering, you can solve this system of equations with respect to a and d resulting in the (unique) solution

a = 1/(-log(c) + log(b + c)) and d=log(c)/(log(c) - log(b + c))

(assuming that b and c are such that the denominators are not equal to zero).

Replacing these expressions for a and d in funclog results in a new fitting function, namely,

(log(c) - log(b*x + c))/(log(c) - log(b + c)),

which by default satisfies the constraints. The values of b and c can be found by curve_fit.

Stelios
  • 5,271
  • 1
  • 18
  • 32
0

You may try use bounds:

bounds = ([amin, bmin, cmin, dmin], [amax, bmax, cmax, dmax])
(or np.inf  -np.inf if limes of param is in infininty)

next

popt1, pcov1 = curve_fit(funclog, x, y, bounds=bounds)
fafnir1990
  • 185
  • 2
  • 16
  • The problem is that I don't want to bound my parameters but the result of the function itself – Vlad Nov 03 '16 at 13:38
  • maybe you should try to "normalize" results after fitting? You, know from max Y as 1 and min Y as zero? – fafnir1990 Nov 04 '16 at 12:28