Skip to content

Instantly share code, notes, and snippets.

@swinton
Last active December 21, 2015 19:58
Show Gist options
  • Select an option

  • Save swinton/6357540 to your computer and use it in GitHub Desktop.

Select an option

Save swinton/6357540 to your computer and use it in GitHub Desktop.
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)
@lroggendorff

Copy link
Copy Markdown

From the comments on that post, line 13 could be:

for x, y in zip(X, Y):

Oh, and line 22.

@swinton

swinton commented Aug 27, 2013

Copy link
Copy Markdown
Author

@lroggendorff, nice spot. Updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment