Ex. 17.8

Ex. 17.8

(a) Write a program to fit the lasso using the coordinate descent procedure (17.26). Compare its results to those from the lars program or some other convex optimizer, to check that it is working correctly.

(b) Using the program from (a), write code to implement the graphical lasso (Algorithm 17.2). Apply it to the flow cytometry data from the book website. Vary the regularization parameter and examine the resulting networks.

Soln. 17.8

We've already implemented Algorithm 17.1 in Ex. 17.7. The graphical lasso implementation only needs two minor changes on top of that. The first is the initialization of \(\bb{W} = \bb{S} + \lambda \bb{I}\). The second is in step 2 (b), instead of solving \(\beta\) using ordinary least squares, we use Lasso instead.

For this exercise, we will reuse the GraphicalLasso module in sklearn.

Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import pandas as pd
import numpy as np
import pathlib
from sklearn.covariance import GraphicalLassoCV

# get relative data folder
PATH = pathlib.Path(__file__).resolve().parents[1]
DATA_PATH = PATH.joinpath("data").resolve()

# get data
X = pd.read_csv(DATA_PATH.joinpath("cytometry_data.csv"), header=0)
nodes = list(X.columns)
X = X.to_numpy(dtype=float)

res = GraphicalLassoCV(cv=10, alphas=36).fit(X)
Sigma = res.covariance_
Theta = np.linalg.inv(Sigma)