# find_fit possible bug

find_fit() seems a bit broken to me in sage-6.5 (sage-6.5-x86_64-Linux-Ubuntu_14.04_x86_64.tar.lzma), though it worked in sage-4.8 it has better results then (mean square error 7.8 then, and 12.2 now)

how it works in sage-6.5 (sorry, not enough karma to post images)

how it works in sage-4.8

data = [(0,1),(1,2),(2,3),(3,3),(7,2),(8,5),(9,2),(11,1),(12,2),(13,4),(14,6),(15,11),(16,13),(17,14),(18,13),(19,15),(20,10),(21,3),(22,4),(23,12),(24,17),(25,4),(26,2),(27,5),(28,6),(29,6),(30,5),(31,7),(32,6),(33,2),(34,9),(35,5),(36,1),(37,4),(38,5),(39,1)]
var('a1,b1,c1,a2,b2,c2,x')
model(x) = a1*exp(-((x-b1)/c1)^2) + a2*exp(-((x-b2)/c2)^2)
sol = find_fit(data,model)
show(sol)
f(x) = model(a1=sol[0].rhs(),a2=sol[1].rhs(),b1=sol[2].rhs(),b2=sol[3].rhs(),c1=sol[4].rhs(),c2=sol[5].rhs())
er = sum([pow(f(p[0])-p[1],2) for p in data])/len(data)
show(er)
p2 = scatter_plot(data)
p3 = plot(f(x),(0,40))
p3 + p2


code

sagecloud gives same result as sage-6.5

magic is broken, god must be angry

edit retag close merge delete

Sort by ยป oldest newest most voted
sage: find_fit?
<snip>
ALGORITHM:

Uses scipy.optimize.leastsq which in turn uses MINPACK's lmdif and
lmder algorithms.


So my best guess is that something changed with Scipy. I do find it odd that there is that much of a regression, and that the second exp is so tiny in the 6.5 version.

I tried it in Sage 5.13, and not only did I get a strange error

scipy/optimize/minpack.py:402: RuntimeWarning: Number of calls to function has reached maxfev = 1400.


but also I got another picture, this one with error 9.8. In Sage 5.2, though, the solution is the same as the one you get with Sage 6.5! That makes no sense but at any rate it seems like perhaps Scipy optimize might be a little wonky.

more