Last active
January 3, 2019 20:04
-
-
Save RyotaBannai/97aaeb99d2584a349630c9646376502f to your computer and use it in GitHub Desktop.
This file contains 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
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