Least squares

From Tech
Revision as of 15:23, 7 March 2013 by 77.72.146.250 (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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