# [Tutor] leastsquares.py

Alan Gauld alan.gauld at freenet.co.uk
Fri Dec 31 00:54:34 CET 2004

```> > def _leastSquare():
> >     # do the thing that might generate negatives
> >
> > def leastSquare():
> >     firstTry = _leastSquare()
> >     if firstTry < 0:
> >       return leastSquare() #  try again
> >     else:
> >       return firstTry
> >
> > Primitive but it works. If you want more a efficient/elegant
solution
> > show us what you have...
>
> How is this suppose to work ? Most of the least square algorithm
find
> the nearest local minimum of the distance function. So, if you call
many
> times the least square function without changing the initial values
it
> will do absolutely nothing !

The _leastSquare() function is whatever you do at the moment
that generates negative values. How it gets the data to produce
those values is up to you, they could be parameters, global
or from a file.

The leastSquare() function simply keeps on calling _leastSquare
until a positive value is returned. It assumes that _leastSquare
is somehow modifying its environment(eg by changing the data set)
to return different values...

> Or what do you suppose for your least square method ?

Its not mine, its yours! :-)
The point was that without knowing exactly how you have coded
your algorithm we can't give sensible solutions to avoiding
negatives!

Alan G.

```