Last active
August 29, 2015 14:05
-
-
Save jesuscast/dbab7faa156638de4c77 to your computer and use it in GitHub Desktop.
Given a set of x values and y values, this functions returns another function with the best fit line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def LFIT(x,y): | |
N = len(x) | |
if N != len(y): | |
return 0 | |
Sx = sum(x) | |
Sy = sum(y) | |
Sx2 = sum([n**2 for n in x]) | |
Sxy = sum([x[i]*y[i] for i in range(N)]) | |
b = (N*Sxy-Sx*Sy)/(N*Sx2-Sx**2) | |
a = (Sy-b*Sx)/N | |
def bestFit(k): | |
if k=='a': | |
return a | |
elif k=='b': | |
return b | |
else: | |
return b*k+a | |
return bestFit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment