Last active
December 21, 2015 19:58
-
-
Save swinton/6357540 to your computer and use it in GitHub Desktop.
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 linregress(X, Y): | |
| """ | |
| Summary | |
| Linear regression of y = ax + b | |
| Usage | |
| real, real, real = linreg(list, list) | |
| Returns coefficients to the regression line "y=ax+b" from x[] and y[], and R^2 Value | |
| """ | |
| if len(X) != len(Y): | |
| raise ValueError('unequal length') | |
| N = len(X) | |
| Sx = Sy = Sxx = Syy = Sxy = 0.0 | |
| for x, y in zip(X, Y): | |
| Sx = Sx + x | |
| Sy = Sy + y | |
| Sxx = Sxx + x*x | |
| Syy = Syy + y*y | |
| Sxy = Sxy + x*y | |
| det = Sxx * N - Sx * Sx | |
| a, b = (Sxy * N - Sy * Sx)/det, (Sxx * Sy - Sx * Sxy)/det | |
| meanerror = residual = 0.0 | |
| for x, y in zip(X, Y): | |
| meanerror = meanerror + (y - Sy/N)**2 | |
| residual = residual + (y - a * x - b)**2 | |
| RR = 1 - residual/meanerror | |
| ss = residual / (N-2) | |
| Var_a, Var_b = ss * N / det, ss * Sxx / det | |
| return a, b, RR | |
| if __name__ == '__main__': | |
| X = range(0, 5) | |
| Y = [3, 33, 45, 88, 3] | |
| print linregress(X, Y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From the comments on that post, line 13 could be:
Oh, and line 22.