4

Is there any good library to calculate linear least squares OLS (Ordinary Least Squares) in python?

Thanks.

Edit:

Thanks for the SciKits and Scipy. @ars: Can X be a matrix? An example:

y(1) = a(1)*x(11) + a(2)*x(12) + a(3)*x(13)
y(2) = a(1)*x(21) + a(2)*x(22) + a(3)*x(23)
...........................................
y(n) = a(1)*x(n1) = a(2)*x(n2) + a(3)*x(n3)

Then how do I pass the parameters for Y and X matrices in your example?

Also, I don't have much background in algebra, I would appreciate if you guys can let me know a good tutorial for that kind of problems.

Thanks much.

2 Answers 2

9

Try the statsmodels package. Here's a quick example:

import pylab
import numpy as np
import statsmodels.api as sm

x = np.arange(-10, 10)
y = 2*x + np.random.normal(size=len(x))

# model matrix with intercept
X = sm.add_constant(x)

# least squares fit
model = sm.OLS(y, X)
fit = model.fit()

print fit.summary()

pylab.scatter(x, y)
pylab.plot(x, fit.fittedvalues)

Update In response to the updated question, yes it works with matrices. Note that the code above has the x data in array form, but we build a matrix X (capital X) to pass to OLS. The add_constant function simply builds the matrix with a first column initialized to ones for the intercept. In your case, you would simply pass your X matrix without needing that intermediate step and it would work.

2
  • (+1) Any chance to see you back on stats.stackexchange.com?
    – chl
    Jan 18, 2011 at 17:14
  • @chl: Definitely -- got busy with some programming work and check in here occasionally, but must find my way back to stats.SE soon. Good to hear from you. :)
    – ars
    Jan 22, 2011 at 16:13
4

Have you looked at SciPy? I don't know if it does that, but I would imagine it will.

2
  • 2
    +1: Scipy is the de facto standard for this kind of computation and many others. Oct 28, 2010 at 6:52
  • 2
    Don't discount the scikits ecosystem of packages being built around SciPy -- of which statsmodels is one.
    – ars
    Nov 4, 2010 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.