Algorithm · Two Sigma · Medium
Implement simple linear regression using the closed-form ordinary least squares (OLS) estimator. You are given n paired observations (x_i, y_i) and must return the coefficients of the line $$\hat{y} = a + b x$$ that minimizes the sum of squared residuals $$\sum_{i=1}^{n} \left(y_i - (a + b x_i)\right)^2.$$ Use the following formulas computed directly from the data: $$b = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{n} (x_i - \bar{x})^2}, \qquad a =…
Checking your access…