Katkat

Derive Linear Regression Solution

MetaMachine Learning Engineer · Technical Screen

Last updated

This problem tests your ability to derive the Ordinary Least Squares (OLS) closed-form solution for a simple linear regression model and analyze the underlying mathematical assumptions.

Derive Linear Regression Solution

Given a dataset of $n$ observations represented as pairs $(x_i, y_i)$, we assume a linear model $y_i = \beta_0 + \beta_1 x_i + \epsilon_i$, where $\epsilon_i$ is i.i.d. Gaussian noise with mean 0 and variance $\sigma^2$.

Your tasks are as follows:

  1. Define the Mean Squared Error (MSE) objective function $J(\beta_0, \beta_1)$ for this model.
  2. Derive the closed-form expressions for $\beta_0$ and $\beta_1$ that minimize $J$ by setting the partial derivatives with respect to $\beta_0$ and $\beta_1$ to zero.
  3. Express your final solution for $\beta_1$ in terms of the covariance and variance of the input data: $Cov(x, y)$ and $Var(x)$.

Constraints and Requirements

  • Provide the step-by-step calculus derivation.
  • Assume $n > 1$ and the variance of $x$ is non-zero.

Example

If our data is $(1, 2), (2, 3), (3, 5)$, we compute:

  • Mean of $x$: $\bar{x} = 2$
  • Mean of $y$: $\bar{y} = 3.33$
  • $\beta_1$ should represent the slope of the line that minimizes the sum of squared vertical residuals.

Edge Cases

  • What happens to the solution if all $x_i$ values are identical (i.e., $Var(x) = 0$)?

Hints

Hint 1

Start by writing the sum of squared residuals $J = \sum_{i=1}^n (y_i - (\beta_0 + \beta_1 x_i))^2$.

Hint 2

Differentiate $J$ with respect to $\beta_0$ first to show that the regression line must pass through the centroid $(\bar{x}, \bar{y})$.

Hint 3

Substitute $\beta_0 = \bar{y} - \beta_1\bar{x}$ back into the derivative of $J$ with respect to $\beta_1$ to solve for the slope.

Follow-up questions

  • How does the Maximum Likelihood Estimation (MLE) of the parameters under the assumption of Gaussian noise compare to the OLS solution?
  • Under what conditions would you prefer using Gradient Descent instead of this closed-form (Normal Equation) approach?
  • How would the derivation change if we added an L2 regularization term (Ridge Regression) to the objective function?