Skip to content

Instantly share code, notes, and snippets.

@SwampThingPaul
Forked from RyotaBannai/ols.py
Created January 3, 2019 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SwampThingPaul/077f338f87a22dd12ccb01a523121596 to your computer and use it in GitHub Desktop.
Save SwampThingPaul/077f338f87a22dd12ccb01a523121596 to your computer and use it in GitHub Desktop.
import numpy as np
from numpy.linalg import inv
import statsmodels.api as sm
# from scratch
x = sm.add_constant(x) # add constant in the 0 index
b = inv(x.T.dot(x)).dot(x.T).dot(y)
yest_ols = np.array([b[2]*v**2 + b[1]*v + b[0] for v in x.T[0]])
# with using numpy.linalg.lstsq
b1, b2, c = np.linalg.lstsq(sm.add_constant(x).T[[1,2,0]].T, y_data, rcond=None)[0]
yest_ols_ = np.array([b2*v**2 + b1*v + c for v in data])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment