Published on

Geometric Intuition of how sign(𝑤ⱼ) aligns with sign(z) in Lasso Regression

3498 words18 min read
Authors
  • avatar
    Name
    Vishal Mandrai
    Twitter

In this post, we'll see the geometric intuition of how sign(wj)\text{sign}(w_j) aligns with sign(z)\text{sign}(z) in Lasso Regression. This is an important detail that arise while deriving the optimal solution for Lasso Regression via Coordinate Descent based optimization. Their comes a defining point where solution turns to Soft Thresholding Operation for calculating optimal wjw_j, because sign(wj)\text{sign}(w_j) aligns with sign(z)\text{sign}(z).

For better understanding take a quick look at Lasso derivation. Click to read.

Let's refresh our memory and start with a quick recap of Lasso Regression derivation.


Quick Recap

The Loss Function for Lasso Regression is:

L(w)=12ni=1n(yiXi.w)2Prediction Error+λ.j=1pwjL1 Penalty\hspace{3.5cm} L(w) = \frac{1}{2n}\underbrace{\sum_{i=1}^{n}(y_i-X_i.w)^2}_{\text{Prediction Error}} + \lambda.\underbrace{\sum_{j=1}^{p}|w_j|}_{\text{L1 Penalty}}

L(w)=12ni=1n(yi(w0+j=1pXijwj))2+λj=1pwj\hspace{3.5cm} L(\mathbf{w}) = \frac{1}{2n} \sum_{i = 1}^{n} \left( y_i - \left( w_0 + \sum_{j = 1}^{p} X_{ij} w_j \right) \right)^2 + \lambda \sum_{j = 1}^{p} |w_j|

L(w)=12ni=1n(yi(w0+Xi1w1+Xi2w2+Xi3w3++Xipwp))2+λj=1pwj\hspace{0.5cm} L(\mathbf{w}) = \frac{1}{2n} \sum_{i = 1}^{n} \left( y_i - \left( w_0 + X_{i1} w_1 + X_{i2} w_2 + X_{i3} w_3 + \dots + X_{ip} w_p \right) \right)^2 + \lambda \sum_{j = 1}^{p} |w_j|

Now, differentiate L(w)L(w) w.r.t wjw_j keeping other coefficients fixed. Then, find the value of wjw_j for which the derivative is zero. The final equation be:

wj=1ni=1n(yiXijwj)Xijλsign(wj)\hspace{3.5cm} \boxed{w_j = \frac{1}{n} \sum_{i = 1}^{n} \left( y_i - X_{i-j} w_{-j} \right) X_{ij} - \lambda \operatorname{sign}(w_j)}

NOTE: Here we know, sign of wjw_j is the same as the sign of 1ni=1n(yiXijwj)Xij\frac{1}{n} \sum_{i = 1}^{n} \left( y_i - X_{i-j} w_{-j} \right) X_{ij} (when wj0w_j ≠ 0). Its time to see 'how'.

The above equation finally becomes a “Soft Thresholding Operation” due to this knowledge.

wj=Soft Thresholding(1ni=1n(yiXijwij)Xij,λ)\hspace{3.5cm} \boxed{ \quad w_j = \text{Soft Thresholding} \left( \frac{1}{n} \sum_{i = 1}^{n} \left( y_i - \mathbf{X}_{i-j} \mathbf{w}_{i-j} \right) X_{ij}, \, \lambda \right) \quad}

wj=Soft Thresholding(z,λ)\hspace{5.5cm} \boxed{ \quad w_j = \text{Soft Thresholding} \left( z, \, \lambda \right) \quad}


What Actually This zz Represents:

From the above discussed derivation, we can draw an important insight about zz - correlation between input feature XjX_j and the residuals.

Let's say we are in p+2p+2 dimensional space where p+1p+1 dimensions mark all coefficients of Lasso Regression and p+2th{p+2}^{th} dimension marks the Loss (without L1-penalty term). We plot our Loss Function in this space:

L(w)=12ni=1n(yi(w0+Xi1w1+Xi2w2+Xi3w3++Xipwp))2\hspace{0.2cm} \boxed{L(\mathbf{w}) = \frac{1}{2n} \sum_{i = 1}^{n} \left( y_i - \left( w_0 + X_{i1} w_1 + X_{i2} w_2 + X_{i3} w_3 + \dots + X_{ip} w_p \right) \right)^2}

What if I say, zz is nothing but negative of the slope of the tangent at point PP in this space, where:

P=(w0k,w1k,,wj1k,0,wj+1k,,wpk,Loss)\hspace{3.5cm} \boxed{ \quad P = (w_0^k, w_1^k, \dots, w_{j - 1}^k, 0, w_{j + 1}^k, \dots, w_p^k, \text{Loss}) \quad }

And, wj=0w_j = 0 keeping other coefficients fixed at values from earlier optimization iteration (say kthk^{th} iteration).

The unregularized slope at point PP is:

wj(L(w))at point P=XjT(yXjwj)=z\hspace{3.5cm} \frac{\partial}{\partial w_j} \left( L(w) \right) \bigg|_{\text{at point P}} = -X_j^T (y - X_{-j}w_{-j}) = -z

wj(L(w))P=(w0k,w1k,,wj1k,0,wj+1k,,wpk,Loss)=XjT(yXjwj)=z\hspace{3.5cm} \frac{\partial}{\partial w_j} \left( L(w) \right) \bigg|_{P = (w_0^k, w_1^k, \dots, w_{j - 1}^k, 0, w_{j + 1}^k, \dots, w_p^k, \text{Loss})} = -X_j^T (y - X_{-j}w_{-j}) = -z

Now we know, what exactly zz represents. Knowing the sign of zz we can predict the sign of updated wjw_j on k+1thk+1^{th} iteration. Let’s see how!


How zz Determines sign(𝑤ⱼ)

Let’s say we have completed kk optimization iterations and w=[w0k,w1k,,wjk,,wpk]w = [w_0^k, w_1^k, \dots, w_{j}^k, \dots, w_p^k]. Now we are going for k+1thk + 1^{th} optimization iteration in which we are updating the coefficient wjw_j.

Let's say the slope of the tangent at point P(w0k,w1k,,wj1k,0,wj+1k,,wpk)P (w_0^k, w_1^k, \dots, w_{j - 1}^k, 0, w_{j + 1}^k, \dots, w_p^k) on the Loss curve in p+2p+2 dimensional space is some negative slope.

wj(L(w))P=(w0k,w1k,,wj1k,0,wj+1k,,wpk,Loss)=some negative value\hspace{3.5cm} \frac{\partial}{\partial w_j} \left( L(w) \right) \bigg|_{P = (w_0^k, w_1^k, \dots, w_{j - 1}^k, 0, w_{j + 1}^k, \dots, w_p^k, \text{Loss})} = \text{some negative value}

That means, z-z is ve-ve and hence zz is +ve+ve. The optimal value of wjw_j for which loss be minimum is some positive value at the right-side of 00.

wj(L(w))P=(w0k,w1k,,wj1k,0,wj+1k,,wpk,Loss)=z=some negative value\hspace{1.5cm} \frac{\partial}{\partial w_j} \left( L(w) \right) \bigg|_{P = (w_0^k, w_1^k, \dots, w_{j - 1}^k, 0, w_{j + 1}^k, \dots, w_p^k, \text{Loss})} = -z = \text{some negative value}

z=some positive valuesign(z)=+ve \hspace{4.5cm} z = \text{some positive value} \rarr \text{sign}(z) = +ve

Check the below graph. Showing relationship between wjw_j and Loss in 2-D plot:

Explainer

Similarly, if the slope of the tangent at point P(w0k,w1k,,wj1k,0,wj+1k,,wpk)P (w_0^k, w_1^k, \dots, w_{j - 1}^k, 0, w_{j + 1}^k, \dots, w_p^k) on the Loss curve in p+2p+2 dimensional space is some positive slope.

wj(L(w))P=(w0k,w1k,,wj1k,0,wj+1k,,wpk,Loss)=some positive value\hspace{3.5cm} \frac{\partial}{\partial w_j} \left( L(w) \right) \bigg|_{P = (w_0^k, w_1^k, \dots, w_{j - 1}^k, 0, w_{j + 1}^k, \dots, w_p^k, \text{Loss})} = \text{some positive value}

That means, z-z is +ve+ve and hence zz is ve-ve. The optimal value of wjw_j for which loss be minimum is some negative value at the left-side of 00.

wj(L(w))P=(w0k,w1k,,wj1k,0,wj+1k,,wpk,Loss)=z=some positive value\hspace{1.5cm} \frac{\partial}{\partial w_j} \left( L(w) \right) \bigg|_{P = (w_0^k, w_1^k, \dots, w_{j - 1}^k, 0, w_{j + 1}^k, \dots, w_p^k, \text{Loss})} = -z = \text{some positive value}

z=some negative valuesign(z)=ve \hspace{4.5cm} z = \text{some negative value} \rarr \text{sign}(z) = -ve

NOTE: Irrespective of sign(wj)\text{sign}(w_j) from kth optimization iteration, sign(wj)\text{sign}(w_j) after k+1thk + 1^{th} iteration can be determined by signz\text{sign}{z}.

You see, in both the cases sign(z)\text{sign}(z) aligns with the sign of updated wjw_j.

Conclusion: Sign of zz dictates the sign of updated wjw_j.

Hence, we can use the sign of zz and simplify the equation:

wj=1ni=1n(yiXijwj)Xijλsign(wj)\hspace{3.5cm} \boxed{w_j = \frac{1}{n} \sum_{i = 1}^{n} \left( y_i - X_{i-j} w_{-j} \right) X_{ij} - \lambda \operatorname{sign}(w_j)}

This simplification naturally leads to Soft Thresholding Operation. That is:

wj=Soft Thresholding(1ni=1n(yiXijwij)Xij,λ)\hspace{3.5cm} \boxed{ \quad w_j = \text{Soft Thresholding} \left( \frac{1}{n} \sum_{i = 1}^{n} \left( y_i - \mathbf{X}_{i-j} \mathbf{w}_{i-j} \right) X_{ij}, \, \lambda \right) \quad}

wj=Soft Thresholding(z,λ)\hspace{5.5cm} \boxed{ \quad w_j = \text{Soft Thresholding} \left( z, \, \lambda \right) \quad}

Hence, shrinkage of coefficient would happen in Lasso Regression.


Thanks for reading! Do check out my other posts. Happy Learing.