# [SciPy-user] Bug(?) in optimize.leastsq when Jacobian matrix is provided (Complete) <Ignore my previous mail>

Abhiman Chatra abhiman at gmail.com
Thu Jun 23 09:33:14 CDT 2005

```(Sorry my previous mail was incomplete)
Hi,

I am new to Python, SciPy and optimization. So excuse me if I have make
mistakes.

My Problem :
-------------------
I want to use optimize.leastsq function of scipy to find a least square fit.
But my function in the parametric form is quite complicated. Hence I want to
provide the Jacobian matrix to the leastsquare optimizer to improve its
performance. But when I provide the function for calculating the Jacobian
matrix, I get a windows error saying "python.exe has generated errors and
will be closed by Windows. You will need to restart the program. An error
log is being created <OK>"

Since this error cannot be caught I cannot trace the cause of this error.
But my feeling is that this is some memory error. This error is not
reproducible if I do not provide the function for calculating the Jacobian
matrix.

My Configutration :
--------------------------
Machine : P4
OS : Windows 2000 Professional
Python : Python 2.3.3
Numeric : Numeric-23.5.win32-py2.3
Scipy : SciPy_complete-0.3.2.win32-py2.3-num23.5

( I have also tryed with the following configuration :
Python : Python 2.2.3
Numeric : Numeric-23.0.win32-py2.2
SciPy : SciPy_complete-0.3.2.win32-py2.2-num22.0
- Again my code fails)

Code :
---------------------------------------------------------------------------------------------------------------------------------
#This is not my actual code but a simplified version for simple linear
regression fit
from scipy import *
import random
import pprint

inputs = [
[ 1.0 , 2.0 ],
[ 2.0 , 2.0 ],
[ 3.0 , 1.0 ],
[ 2.0 , 4.0 ]]

answers = [ 4.05 , 6.1 , 6.9 , 8.0 ]

def residual_values(betas, inp , ans) :
ret = [0.0] * len(inp)
for i in range(len(inp)) :
ret[i] = 1.0 * betas[len(inp[i])] - ans[i]
for j in range(len(inp[0])) :
ret[i] += betas[j] * inp[i][j]
print "In residual_values"
#print "Betas : "
#pprint.pprint(betas)
#print "return value : "
#pprint.pprint(ret)
return ret

def jacobian(betas, inp , ans) :
print "In jacobian"
J = [[0.0] * len(betas)] * len(inp)
for i in range(len(inp)) :
J[i][0] = inp[i][0]
J[i][1] = inp[i][1]
J[i][2] = 1.0
return J

try:
for i in range(100):
print("---------------- %d -----------------" % (i))
initial = [0.0] * 3
#random.shuffle(initial)
for i in range(3) :
initial[i] = 1.0
opt = optimize.leastsq(residual_values, initial, args = (inputs, answers),
Dfun=jacobian, maxfev=10000)
#opt = optimize.leastsq(residual_values, initial, args = (inputs, answers),
maxfev=10000)

pprint.pprint(opt)

except:
print "Error regression fitting model..."

pprint.pprint(locals())

raise
---------------------------------------------------------------------------------------------------------------------------------

Can someone please let me know what is causing the crash? My feeling is that
this is a bug in SciPy. Any help is greatly appreciated.

Thanks,
Abhiman

--
Abhiman Chatra B,
Technical Analyst,
Trilogy India,
Mobile # : 9845358165
----------------------------------------------------
"If only your programs would be correct if you simply typed them three
times"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.scipy.net/pipermail/scipy-user/attachments/20050623/5198b123/attachment.htm
```