def test_gowl_vs_glasso_duality_gap_3(self): """ Duality Gap goes negative in this case. Should that happen? """ np.random.seed(680) p = 10 blocks = [ Block(dim=p, idx=0, block_min_size=2, block_max_size=6, block_value=0.9), Block(dim=p, idx=1, block_min_size=2, block_max_size=6, block_value=-0.9), Block(dim=p, idx=3, block_min_size=2, block_max_size=6, block_value=-0.5), ] theta_star, blocks, theta_blocks = generate_theta_star_gowl(p=p, alpha=0.5, noise=0.1, blocks=blocks) lam1 = 0.001 # controls sparsity lam2 = 0.01 # encourages equality of coefficients rho = oscar_weights(lam1, lam2, (p ** 2 - p) / 2) theta_star = theta_star[0] sigma = np.linalg.inv(theta_star) n = 100 X = np.random.multivariate_normal(np.zeros(p), sigma, n) X = standardize(X) S = np.cov(X.T) theta_0 = np.linalg.inv(S) model = GOWLModel(X, S, theta_0, lam1, lam2, 'backtracking', max_iters=100000) model.fit() theta_gowl = model.theta_hat gl = GraphicalLasso(max_iter=200) gl.fit(S) theta_glasso = gl.get_precision() print('Non zero entries in precision matrix {}'.format(np.count_nonzero(theta_gowl))) plot_multiple_theta_matrices_2d([theta_blocks, theta_star, theta_glasso, theta_gowl], [f"Blocks: {len(blocks)}", 'True Theta', 'GLASSO', 'GOWL']) _fit_evaluations(theta_star, theta_glasso, 3, 'GLASSO') _fit_evaluations(theta_star, theta_gowl, 3, 'GOWL') y_hat_gowl = spectral_clustering(theta=theta_gowl, K=4) y_hat_glasso = spectral_clustering(theta=theta_glasso, K=4) y_true = spectral_clustering(theta=theta_blocks, K=4).flatten() _cluster_evaluations(y_true, y_hat_gowl, 'GOWL') _cluster_evaluations(y_true, y_hat_glasso, 'GLASSO')
def predict(self, data: pd.DataFrame, alpha: float = 0.01, max_iter: int = 2000, **kwargs) -> nx.Graph: """Predict the graph structure """ edge_model = GraphicalLasso(alpha=alpha, max_iter=max_iter) edge_model.fit(data.values) return nx.relabel_nodes(nx.DiGraph(edge_model.get_precision()), {idx: i for idx, i in enumerate(data.columns)})
def predict(self, data, alpha=0.01, max_iter=2000, **kwargs): """ Predict the graph skeleton. Args: data (pandas.DataFrame): observational data alpha (float): regularization parameter max_iter (int): maximum number of iterations Returns: networkx.Graph: Graph skeleton """ edge_model = GraphicalLasso(alpha=alpha, max_iter=max_iter) edge_model.fit(data.values) return nx.relabel_nodes(nx.DiGraph(edge_model.get_precision()), {idx: i for idx, i in enumerate(data.columns)})
import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.covariance import GraphicalLasso filename = "expr_ceph_utah_1000.txt" data = pd.read_csv(filename, delimiter='\t', index_col=0) n_rows = data.shape[0] n_cols = data.shape[1] cov_matrix = np.cov(data.T) np.savetxt("cov_matrix.csv", cov_matrix, delimiter=',') model = GraphicalLasso(alpha=0.55) model.fit(data) prec_matrix = model.get_precision() # print(prec_matrix) np.savetxt("precision_matrix.csv", prec_matrix, delimiter=',') n = n_cols adj_matrix = np.zeros((n, n)) n_edges = 0 for i in range(n): for j in range(i, n): if prec_matrix[i, j] != 0: adj_matrix[i, j] = 1 adj_matrix[j, i] = 1 n_edges += 1 np.savetxt("glasso_adj_matrix.csv", adj_matrix, delimiter=',') degree_list = np.sum(adj_matrix, axis=0) - 1