Linear Regression

Important: Please read the installation page for details about how to install the toolboxes. $\newcommand{\dotp}[2]{\langle #1, #2 \rangle}$ $\newcommand{\enscond}[2]{\lbrace #1, #2 \rbrace}$ $\newcommand{\pd}[2]{ \frac{ \partial #1}{\partial #2} }$ $\newcommand{\umin}[1]{\underset{#1}{\min}\;}$ $\newcommand{\umax}[1]{\underset{#1}{\max}\;}$ $\newcommand{\umin}[1]{\underset{#1}{\min}\;}$ $\newcommand{\uargmin}[1]{\underset{#1}{argmin}\;}$ $\newcommand{\norm}[1]{\|#1\|}$ $\newcommand{\abs}[1]{\left|#1\right|}$ $\newcommand{\choice}[1]{ \left\{ \begin{array}{l} #1 \end{array} \right. }$ $\newcommand{\pa}[1]{\left(#1\right)}$ $\newcommand{\diag}[1]{{diag}\left( #1 \right)}$ $\newcommand{\qandq}{\quad\text{and}\quad}$ $\newcommand{\qwhereq}{\quad\text{where}\quad}$ $\newcommand{\qifq}{ \quad \text{if} \quad }$ $\newcommand{\qarrq}{ \quad \Longrightarrow \quad }$ $\newcommand{\ZZ}{\mathbb{Z}}$ $\newcommand{\CC}{\mathbb{C}}$ $\newcommand{\RR}{\mathbb{R}}$ $\newcommand{\EE}{\mathbb{E}}$ $\newcommand{\Zz}{\mathcal{Z}}$ $\newcommand{\Ww}{\mathcal{W}}$ $\newcommand{\Vv}{\mathcal{V}}$ $\newcommand{\Nn}{\mathcal{N}}$ $\newcommand{\NN}{\mathcal{N}}$ $\newcommand{\Hh}{\mathcal{H}}$ $\newcommand{\Bb}{\mathcal{B}}$ $\newcommand{\Ee}{\mathcal{E}}$ $\newcommand{\Cc}{\mathcal{C}}$ $\newcommand{\Gg}{\mathcal{G}}$ $\newcommand{\Ss}{\mathcal{S}}$ $\newcommand{\Pp}{\mathcal{P}}$ $\newcommand{\Ff}{\mathcal{F}}$ $\newcommand{\Xx}{\mathcal{X}}$ $\newcommand{\Mm}{\mathcal{M}}$ $\newcommand{\Ii}{\mathcal{I}}$ $\newcommand{\Dd}{\mathcal{D}}$ $\newcommand{\Ll}{\mathcal{L}}$ $\newcommand{\Tt}{\mathcal{T}}$ $\newcommand{\si}{\sigma}$ $\newcommand{\al}{\alpha}$ $\newcommand{\la}{\lambda}$ $\newcommand{\ga}{\gamma}$ $\newcommand{\Ga}{\Gamma}$ $\newcommand{\La}{\Lambda}$ $\newcommand{\si}{\sigma}$ $\newcommand{\Si}{\Sigma}$ $\newcommand{\be}{\beta}$ $\newcommand{\de}{\delta}$ $\newcommand{\De}{\Delta}$ $\newcommand{\phi}{\varphi}$ $\newcommand{\th}{\theta}$ $\newcommand{\om}{\omega}$ $\newcommand{\Om}{\Omega}$ $\newcommand{\eqdef}{\equiv}$

This tour studies linear regression method in conjunction with regularization. It contrasts ridge regression and the Lasso.

We recommend that after doing this Numerical Tours, you apply it to your own data, for instance using a dataset from <https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/ LibSVM>.

Disclaimer: these machine learning tours are intended to be overly-simplistic implementations and applications of baseline machine learning methods. For more advanced uses and implementations, we recommend to use a state-of-the-art library, the most well known being <http://scikit-learn.org/ Scikit-Learn>

In [1]:
library(plot3D)
library(pracma)
library(rmatio)
options(warn=-1) # turns off warnings, to turn on: "options(warn=0)"

# Importing the libraries
for (f in list.files(path="nt_toolbox/toolbox_general/", pattern="*.R")) {
    source(paste("nt_toolbox/toolbox_general/", f, sep=""))
}
for (f in list.files(path="nt_toolbox/toolbox_signal/", pattern="*.R")) {
    source(paste("nt_toolbox/toolbox_signal/", f, sep=""))
}

Helpers:

In [2]:
Xm = function(X){as.matrix(X - rep(colMeans(X), rep.int(nrow(X), ncol(X))))}
Cov = function(X){data.matrix(1. / (n - 1) * t(Xm(X)) %*% Xm(X))}

Dataset Loading

We test the method on the prostate dataset in $n=97$ samples with features $x_i \in \RR^p$ in dimension $p=8$. The goal is to predict the price value $y_i \in \RR$.

Helpers.

In [3]:
Xm = function(X){as.matrix(X - rep(colMeans(X), rep.int(nrow(X), ncol(X))))}
Cov = function(X){data.matrix(1. / (n - 1) * t(Xm(X)) %*% Xm(X))}

Load the dataset.

In [4]:
mat = read.mat("nt_toolbox/data/ml-prostate.mat")
A = mat$A
class_names = mat$class_names[[1]]
In [5]:
head(A)
-0.57981852.769459 50 -1.386294 0 -1.386294 6 0 -0.43078291
-0.99425233.319626 58 -1.386294 0 -1.386294 6 0 -0.16251891
-0.51082562.691243 74 -1.386294 0 -1.386294 7 20 -0.16251891
-1.20397283.282789 58 -1.386294 0 -1.386294 6 0 -0.16251891
0.75141613.432373 62 -1.386294 0 -1.386294 6 0 0.37156361
-1.04982213.228826 50 -1.386294 0 -1.386294 6 0 0.76546781

Randomly permute it.

In [6]:
A = A[sample(dim(A)[1]),]

Separate the features $X$ from the data $y$ to predict information.

In [7]:
X = A[,1:(dim(A)[2] - 2)]
y = A[,dim(A)[2] - 1]
c = A[,dim(A)[2]]

$n$ is the number of samples, $p$ is the dimensionality of the features,

In [8]:
n = dim(X)[1]
p = dim(X)[2]
print(n)
print(p)
[1] 97
[1] 8

Split into training and testing.

In [9]:
I0 = (c==1) # train
I1 = (c==0) # test
n0 = sum(I0)
n1 = n - n0
X0 = X[I0,]
y0 = y[I0]
X1 = X[I1,]
y1 = y[I1]

Normalize the features by the mean and std of the training set. This is optional.

In [10]:
mX0 = apply(X0, 2, mean)
sX0 = apply(X0, 2, std)
X0 = sweep(X0, 2, mX0,"-")
X0 = sweep(X0, 2, sX0,"/")
X1 = sweep(X1, 2, mX0,"-")
X1 = sweep(X1, 2, sX0,"/")

Remove the mean (computed from the test set) to avoid introducing a bias term and a constant regressor. This is optional.

In [11]:
m0 = mean(y0)
y0 = y0 - m0
y1 = y1 - m0

Dimenionality Reduction and PCA

In order to display in 2-D or 3-D the data, dimensionality is needed. The simplest method is the principal component analysis, which perform an orthogonal linear projection on the principal axsis (eigenvector) of the covariance matrix.

Display the covariance matrix of the training set.

In [12]:
options(repr.plot.width=4, repr.plot.height=4)
C = t(X0) %*% X0
image(1:p, 1:p, C, xlab="", ylab="", col=topo.colors(5), ylim=c(p + 0.5, 0.5))
In [13]:
options(repr.plot.width=4, repr.plot.height=4)
barplot(t(t(X0) %*% y0), xlab='', ylab='', col="darkblue")

Compute PCA ortho-basis and the feature in the PCA basis.

In [14]:
svd_decomp = svd(X0)
U = svd_decomp$u
s = svd_decomp$d
V = svd_decomp$v
X0r = X0 %*% V

Plot sqrt of the eigenvalues.

In [15]:
options(repr.plot.width=4, repr.plot.height=4)
plot(s, type="o", col=4, ylab="", xlab="", pch=16)

Display the features.

In [16]:
options(repr.plot.width=6, repr.plot.height=6)

panel.hist = function(x, ...)
{
    usr = par("usr"); on.exit(par(usr))
    par(usr = c(usr[1:2], 0, 1.5) )
    h = hist(x, plot = FALSE)
    breaks = h$breaks; nB <- length(breaks)
    y = h$counts
    y = y/max(y)
    rect(breaks[-nB], 0, breaks[-1], y, col = "darkblue")
}
pairs(X0, col="blue", diag.panel=panel.hist)

Display the points cloud of feature vectors in 2-D PCA space.

In [17]:
options(repr.plot.width=4, repr.plot.height=4)
plot(X0r[,1], X0r[,2], col="blue", pch=16, xlab="", ylab="")

1D plot of the function to regress along the main eigenvector axes.

In [18]:
options(repr.plot.width=5, repr.plot.height=3)
for (i in 1:p)
{
    plot(X0r[,i], y0, col=i+1, xlab="", ylab="", pch=16)
}

Linear Regression

We look for a linear relationship $ y_i = \dotp{w}{x_i} $ written in matrix format $ y= X w $ where the rows of $X \in \RR^{n \times p}$ stores the features $x_i \in \RR^p$.

Since here $ n > p $, this is an over-determined system, which can solved in the least square sense $$ \umin{ w } \norm{Xw-y}^2 $$ whose solution is given using the Moore-Penrose pseudo-inverse $$ w = (X^\top X)^{-1} X^\top y $$

Least square solution.

In [19]:
w = solve(t(X0) %*% X0) %*% t(X0) %*% y0

Prediction (along 1st eigenvector).

In [20]:
options(repr.plot.width=4, repr.plot.height=4)
plot( X1 %*% w, type="l", col="blue", ylim=c(min(y1), max(y1)), xlab="", ylab="")
lines(y1,  type="l", col="orange")
legend("topright", legend=c("X1*w", "y1"), col=c("blue", "orange"), pch="-")

Mean-square error on testing set.

In [21]:
E = sqrt(sum((X1 %*% w-y1)^2 ) / n1)
print(paste('Relative prediction error:', E))
[1] "Relative prediction error: 0.721993078573196"

Regularization is obtained by introducing a penalty. It is often called ridge regression, and is defined as $$ \umin{ w } \norm{Xw-y}^2 + \lambda \norm{w}^2 $$ where $\lambda>0$ is the regularization parameter.

The solution is given using the following equivalent formula $$ w = (X^\top X + \lambda \text{Id}_p )^{-1} X^\top y, $$ $$ w = X^\top ( XX^\top + \lambda \text{Id}_n)^{-1} y, $$ When $p<n$ (which is the case here), the first formula should be prefered.

In contrast, when the dimensionality $p$ of the feature is very large and there is little data, the second is faster. Furthermore, this second expression is generalizable to Kernel Hilbert space setting, corresponding possibly to $p=+\infty$ for some kernels.

In [22]:
lambda = .2 * norm(X0)**2
w = solve(t(X0) %*% X0 + lambda * base::diag(p)) %*% t(X0) %*% y0
w1 = t(X0) %*% solve(X0 %*% t(X0) + lambda * base::diag(n0)) %*% y0
print(paste('Error (should be 0):', round(norm(w-w1)/norm(w), 3)))
[1] "Error (should be 0): 0"

Exercise 1

Display the evolution of the test error $E$ as a function of $\lambda$. ind optimal lambda isplay error evolution.

In [23]:
source("nt_solutions/ml_2_regression/exo1.R")
In [24]:
## Insert your code here.

Exercise 2

Display the regularization path, i.e. the evolution of $w$ as a function of $\lambda$.

In [25]:
source("nt_solutions/ml_2_regression/exo2.R")
In [26]:
## Insert your code here.

Sparse Regularization

In order to perform feature selection (i.e. select a subsect of the features which are the most predictive), one needs to replace the $\ell^2$ regularization penalty by a sparsity inducing regularizer. The most well known is the $\ell^1$ norm $$ \norm{w}_1 \eqdef \sum_i \abs{w_i} . $$

The energy to minimize is $$ \umin{w} J(w) \eqdef \frac{1}{2}\norm{X w-y}^2 + \lambda \norm{w}_1. $$

In [27]:
J = function(w,Lambda){1/2 * norm(X0 %*% w - y0)**2 + Lambda * base::norm(as.matrix(w),"1")}

The simplest iterative algorithm to perform the minimization is the so-called iterative soft thresholding (ISTA), aka proximal gradient aka forward-backward.

It performs first a gradient step (forward) of the smooth part $\frac{1}{2}\norm{X w-y}^2$ of the functional and then a proximal step (backward) step which account for the $\ell^1$ penalty and induce sparsity. This proximal step is the soft-thresholding operator $$ \Ss_s(x) \eqdef \max( \abs{x}-\lambda,0 ) \text{sign}(x). $$

In [28]:
Soft = function(x,s){pmax(abs(x) - s, 0) * sign(x)}

The ISTA algorithm reads $$ w_{k+1} \eqdef \Ss_{\la\tau}( w_k - \tau X^\top ( X w_k - y ) ), $$ where, to ensure convergence, the step size should verify $ 0 < \tau < 2/\norm{X}^2 $ where $\norm{X}$ is the operator norm.

Display the soft thresholding operator.

In [29]:
options(repr.plot.width=4, repr.plot.height=4)
t = seq(-5,5,length=201)
plot(t, Soft(t,2), xlab="", ylab="", col=4, type="l")

Descent step size.

In [30]:
tau = 1.5 / base::norm(X0,"2")**2

Choose a regularization parameter $\la$.

In [31]:
lmax = max(abs(t(X0) %*% y0))
Lambda = lmax /10

Initialization $w_0$.

In [32]:
w = rep(0, p)

A single ISTA step.

In [33]:
C = t(X0) %*% X0
u = t(X0) %*% y0
ISTA = function(w,Lambda,tau){Soft(w - tau * (C %*% w - u), Lambda*tau)}
w = ISTA(w,Lambda,tau)

Exercise 3

Implement the ISTA algorithm, display the convergence of the energy.

In [34]:
source("nt_solutions/ml_2_regression/exo3.R")
In [35]:
## Insert your code here.

Exercise 4

Compute the test error along the full regularization path. You can start by large $\lambda$ and use a warm restart procedure to reduce the computation time. Compute the classification error. ind optimal lambda Display error evolution.

In [36]:
source("nt_solutions/ml_2_regression/exo4.R")
In [37]:
## Insert your code here.

Exercise 5

Display the regularization path, i.e. the evolution of $w$ as a function of $\lambda$. lot(lambda_list, W', 'LineWidth', 2);

In [38]:
source("nt_solutions/ml_2_regression/exo5.R")
In [39]:
## Insert your code here.

Exercise 6

Compare the optimal weights for ridge and lasso.

In [40]:
source("nt_solutions/ml_2_regression/exo6.R")