Least squares

From Tech
Jump to navigationJump to search

Leastsquares implementation in python:

def leastsquares(data):
   n=len(data)+0.0
   avx=sum([d[0] for d in data])/n
   avy=sum([d[1] for d in data])/n
   do=[(d[0]-avx, d[1]-avy) for d in data]
   slope=sum([d[0]*d[1] for d in do]) / sum([d[0]*d[0] for d in do])
   offset=avy-avx*slope
   return offset, slope
   #print "avx=", avx
   #print "avy=", avy
   #print "slope=", slope
   #print "offset=", offset