Question 1
In Ordinary Least Squares (OLS) Linear Regression with design matrix X (n x d) and target y (n x 1), what is the closed-form expression for the weight vector w?
The correct answer is A \(w = (X^T X)^{-1} X^T y\).
Step-by-Step Derivation
1. Formulate the Cost Function:
In Ordinary Least Squares (OLS) regression, the goal is to find a weight vector \(w\) that minimizes the sum of squared errors between the actual targets \(y\) and the model's predictions \(\hat{y} = Xw\). The loss function \(J(w)\) is expressed in matrix form as:
\(J(w)=\frac{1}{2}\|{}Xw-y\|{}^{2}=\frac{1}{2}(Xw-y)^{T}(Xw-y)\)
2. Expand the Matrix Expression:
Distribute the matrix transpose operations inside the parenthesis:
\(J(w)=\frac{1}{2}(w^{T}X^{T}-y^{T})(Xw-y)\)
\(J(w)=\frac{1}{2}(w^{T}X^{T}Xw-w^{T}X^{T}y-y^{T}Xw+y^{T}y)\)Since \(w^{T}X^{T}y\) is a scalar value, it is equal to its own transpose \((w^T X^T y)^T = y^T X w\). Simplifying the middle terms gives:
\(J(w)=\frac{1}{2}(w^{T}X^{T}Xw-2w^{T}X^{T}y+y^{T}y)\)
3. Compute the Gradient:
To find the value of \(w\) that minimizes the error, calculate the matrix derivative (gradient) of the scalar function \(J(w)\) with respect to the vector \(w\):
\(\nabla _{w}J(w)=X^{T}Xw-X^{T}y\)
4. Solve the Normal Equation:
Set the gradient equal to zero to identify the stationary point:
\(X^{T}Xw-X^{T}y=0\)
\(X^{T}Xw=X^{T}y\)Assuming that the cross-product matrix \(X^{T}X\) is non-singular and invertible, multiply both sides from the left by \((X^T X)^{-1}\) to isolate \(w\):
\(w=(X^{T}X)^{-1}X^{T}y\)