I knew the answer but fumbled the derivation order a bit.
Start by clearly defining the model and the mean squared error (MSE) objective. Then derive the closed-form solution by taking the derivative of MSE with respect to w, setting it to zero, and solving for w. Finally, implement the formula in code, ensuring numerical stability and discussing edge cases.
Pro tip: Mention that the solution is equivalent to the dot product of x and y divided by the dot product of x with itself, and highlight that this is the projection of y onto the subspace spanned by x. Also, note that if all x values are zero, the denominator is zero, so w is undefined; in practice, handle this by returning 0 or raising an error.
State the model y_hat = w * x and the MSE loss function: L(w) = (1/n) * sum((y_i - w * x_i)^2).
Differentiate L(w) with respect to w: dL/dw = (-2/n) * sum(x_i * (y_i - w * x_i)). Set this derivative to zero.
Rearrange the equation to isolate w: w = sum(x_i * y_i) / sum(x_i^2). This is the closed-form least-squares solution.
Write a function that computes w using the formula, e.g., in Python: w = np.dot(x, y) / np.dot(x, x). Include handling for the case when sum(x_i^2) == 0.
Mention that if all x are zero, w is undefined; also note that this is a special case of linear regression without intercept, and the solution can be seen as the projection of y onto x.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.