def test_graphical_lasso_cv_scores(suffix): splits = 4 n_alphas = 5 n_refinements = 3 true_cov = np.array( [ [0.8, 0.0, 0.2, 0.0], [0.0, 0.4, 0.0, 0.0], [0.2, 0.0, 0.3, 0.1], [0.0, 0.0, 0.1, 0.7], ] ) rng = np.random.RandomState(0) X = rng.multivariate_normal(mean=[0, 0, 0, 0], cov=true_cov, size=200) cov = GraphicalLassoCV(cv=splits, alphas=n_alphas, n_refinements=n_refinements).fit( X ) cv_results = cov.cv_results_ # alpha and one for each split total_alphas = n_refinements * n_alphas + 1 keys = ["alphas"] split_keys = [f"split{i}_{suffix}" for i in range(splits)] for key in keys + split_keys: assert key in cv_results assert len(cv_results[key]) == total_alphas cv_scores = np.asarray([cov.cv_results_[key] for key in split_keys]) expected_mean = cv_scores.mean(axis=0) expected_std = cv_scores.std(axis=0) assert_allclose(cov.cv_results_[f"mean_{suffix}"], expected_mean) assert_allclose(cov.cv_results_[f"std_{suffix}"], expected_std)
def main(): mean = torch.tensor(np.ones(16), dtype=torch.float32) diag = torch.tensor(np.ones(16), dtype=torch.float32) population = Gaussian_Distribution(mean=mean, diag=diag, sub=0.3, type='chain', slash=1) truth = population.invcov.numpy() n = 1000 d = population.dim print(truth) dist, sample, _, S = population.generate(n, numpy_like=True) #print(S) #print(np.array(sample)) print(sample_mean(np.array(sample))) print(sample_cov(np.array(sample))) R = np.linalg.inv(S) #print(R) #print(sample) np.random.seed(0) model = GraphicalLassoCV() model.fit(np.array(sample)) cov_ = model.covariance_ prec_ = model.precision_ heatmap(prec_) plt.figure(figsize=(4, 3)) plt.axes([.2, .15, .75, .7]) plt.plot(model.cv_alphas_, np.mean(model.grid_scores_, axis=1), 'o-') plt.axvline(model.alpha_, color='.5') plt.title('Model selection') plt.ylabel('Cross-validation score') plt.xlabel('alpha') plt.show() print(model.cv_alphas_, model.grid_scores_) model = GraphicalLasso() model.fit(sample) heatmap(model.precision_, 0.055) score = dict() score['log_lik'] = [] score['AIC'] = [] alpha_list = np.hstack((np.arange(0, 0.1, 0.001), np.arange(0.11, 0.3, 0.01))) data = np.array(sample) for alpha in alpha_list: out_dict = cross_val_score_GLasso(data, alpha=alpha) score['log_lik'].append(out_dict['log_lik']) score['AIC'].append(out_dict['AIC']) plt.plot(alpha_list, score['log_lik'], 'o-') plt.show() plt.plot(alpha_list, score['AIC']) plt.show()
def test_graphical_lasso_cv_grid_scores_and_cv_alphas_deprecated(): splits = 4 n_alphas = 5 n_refinements = 3 true_cov = np.array([[0.8, 0.0, 0.2, 0.0], [0.0, 0.4, 0.0, 0.0], [0.2, 0.0, 0.3, 0.1], [0.0, 0.0, 0.1, 0.7]]) rng = np.random.RandomState(0) X = rng.multivariate_normal(mean=[0, 0, 0, 0], cov=true_cov, size=200) cov = GraphicalLassoCV(cv=splits, alphas=n_alphas, n_refinements=n_refinements).fit(X) total_alphas = n_refinements * n_alphas + 1 msg = (r"The grid_scores_ attribute is deprecated in version 0\.24 in " r"favor of cv_results_ and will be removed in version 1\.1 " r"\(renaming of 0\.26\).") with pytest.warns(FutureWarning, match=msg): assert cov.grid_scores_.shape == (total_alphas, splits) msg = (r"The cv_alphas_ attribute is deprecated in version 0\.24 in " r"favor of cv_results_\['alpha'\] and will be removed in version " r"1\.1 \(renaming of 0\.26\)") with pytest.warns(FutureWarning, match=msg): assert len(cov.cv_alphas_) == total_alphas
def test_graphical_lasso_cv_scores_deprecated(): """Check that the following keys in cv_results_ are deprecated: `mean_score`, `std_score`, and `split(k)_score`.""" splits = 4 n_alphas = 5 n_refinements = 3 true_cov = np.array( [ [0.8, 0.0, 0.2, 0.0], [0.0, 0.4, 0.0, 0.0], [0.2, 0.0, 0.3, 0.1], [0.0, 0.0, 0.1, 0.7], ] ) rng = np.random.RandomState(0) X = rng.multivariate_normal(mean=[0, 0, 0, 0], cov=true_cov, size=200) cov = GraphicalLassoCV(cv=splits, alphas=n_alphas, n_refinements=n_refinements).fit( X ) cv_results = cov.cv_results_ deprecated_keys = ["mean_score", "std_score"] + [ f"split{k}_score" for k in range(splits) ] for deprecated_key in deprecated_keys: new_key = deprecated_key.replace("_score", "_test_score") msg = ( f"Key: '{deprecated_key}', is deprecated in 1.0 and will be removed in 1.2." f" Use '{new_key}' instead" ) with pytest.warns(FutureWarning, match=msg): cv_results[deprecated_key]
def create_prior_from_samples(self, samples): from sklearn.covariance import GraphicalLassoCV from numpy import asarray, linalg model = GraphicalLassoCV() model.fit(asarray(samples)) return th_Mahalanobis( asarray(samples).mean(axis=0), linalg.cholesky(model.precision_), self.prefix)
def test_graphical_lasso_cv(random_state=1): # Sample area_data from a sparse multivariate normal dim = 5 n_samples = 6 random_state = check_random_state(random_state) prec = make_sparse_spd_matrix(dim, alpha=.96, random_state=random_state) cov = linalg.inv(prec) X = random_state.multivariate_normal(np.zeros(dim), cov, size=n_samples) # Capture stdout, to smoke test the verbose mode orig_stdout = sys.stdout try: sys.stdout = StringIO() # We need verbose very high so that Parallel prints on stdout GraphicalLassoCV(verbose=100, alphas=5, tol=1e-1).fit(X) finally: sys.stdout = orig_stdout # Smoke test with specified alphas GraphicalLassoCV(alphas=[0.8, 0.5], tol=1e-1, n_jobs=1).fit(X)
def est_connectivity(X, gm="Glasso", assume_centered=False): if gm == "QuicGlasso-CV": quic = QuicGraphicalLassoCV(cv=5) quic.fit(X) return quic.covariance_, quic.precision_, quic.lam_ elif gm == "QuicGlasso-BIC": quic_bic = QuicGraphicalLassoEBIC(gamma=0) quic_bic.fit(X) return quic_bic.covariance_, quic_bic.precision_, quic_bic.lam_ else: # Default: Glasso glasso = GraphicalLassoCV(assume_centered=assume_centered, cv=5).fit(X) return glasso.covariance_, glasso.get_precision(), glasso.alpha_
def _infer_network(self, data): """ Infer the network. Args: data (pd.DataFrame): data to be used for the inference. """ entities = data.columns model = GraphicalLassoCV(**self.parameters) model.fit(data.values) self.graph = Graph(adjacency=pd.DataFrame( from_precision_matrix_partial_correlations(model.precision_), index=entities, columns=entities)) logger.debug('inferred with {}'.format(self.method))
def test_graphical_lasso_cv_alphas_iterable(alphas_container_type): """Check that we can pass an array-like to `alphas`. Non-regression test for: https://github.com/scikit-learn/scikit-learn/issues/22489 """ true_cov = np.array([ [0.8, 0.0, 0.2, 0.0], [0.0, 0.4, 0.0, 0.0], [0.2, 0.0, 0.3, 0.1], [0.0, 0.0, 0.1, 0.7], ]) rng = np.random.RandomState(0) X = rng.multivariate_normal(mean=[0, 0, 0, 0], cov=true_cov, size=200) alphas = _convert_container([0.02, 0.03], alphas_container_type) GraphicalLassoCV(alphas=alphas, tol=1e-1, n_jobs=1).fit(X)
def test_deprecated_grid_scores(random_state=1): dim = 5 n_samples = 6 random_state = check_random_state(random_state) prec = make_sparse_spd_matrix(dim, alpha=.96, random_state=random_state) cov = linalg.inv(prec) X = random_state.multivariate_normal(np.zeros(dim), cov, size=n_samples) graphical_lasso = GraphicalLassoCV(alphas=[0.8, 0.5], tol=1e-1, n_jobs=1) graphical_lasso.fit(X) depr_message = ("Attribute grid_scores was deprecated in version " "0.19 and will be removed in 0.21. Use " "``grid_scores_`` instead") with pytest.warns(DeprecationWarning, match=depr_message): assert_equal(graphical_lasso.grid_scores, graphical_lasso.grid_scores_)
def estimate_covariance(x, method): """ Covariance estimator wrapper :param x: :param method: :return: """ cov = None if method == 'shrunk': cov = ShrunkCovariance().fit(x) elif method == 'glasso': cov = GraphicalLassoCV(cv=5, alphas=10, n_refinements=10).fit(x) else: ValueError('Covariance method not in [shrunk,glasso]') return cov.covariance_, cov.precision_
def getConnectome(imgPath=None, atlasPath=None, viewInBrowser=False, displayCovMatrix=False): """ Gets the connectome of a functional MRI scan imgPath -> absolute or relative path to the .nii file atlasPath -> download path for the reference MSDL atlas viewInBrowser (optional, default=False) -> if True, opens up an interactive viewer in the browser displayCovMatrix (optional, default=False) -> display the inverse covariance matrix Returns a tuple of shape (estimator, atlas) """ # Download the reference atlas atlas = datasets.fetch_atlas_msdl(data_dir=atlasPath) # Loading atlas image stored in 'maps' atlasFilename = atlas['maps'] # Get the time series for the fMRI scan masker = NiftiMapsMasker(maps_img=atlasFilename, standardize=True, memory='nilearn_cache', verbose=5) timeSeries = masker.fit_transform(imgPath) # Compute the connectome using sparse inverse covariance estimator = GraphicalLassoCV() estimator.fit(timeSeries) if (displayCovMatrix): labels = atlas['labels'] plotting.plot_matrix(estimator.covariance_, labels=labels, figure=(9, 7), vmax=1, vmin=-1, title='Covariance') plotting.plot_matrix(estimator.precision_, labels=labels, figure=(9, 7), vmax=1, vmin=-1, title='Inverse covariance (Precision)') #covPlot.get_figure().savefig('Covariance.png') # precPlot.get_figure().savefig('Inverse Covariance.png') if (viewInBrowser): coords = atlas.region_coords view = plotting.view_connectome(-estimator.precision_, coords, '60.0%') #view.save_as_html(file_name='Connectome Test.html') view.open_in_browser() return (estimator, atlas)
def glasso(data, alphas=5, n_jobs=None, mode='cd'): """ Estimates the graph with graphical lasso finding the best alpha based on cross validation Parameters ---------- data: numpy ndarray The input data for to reconstruct/estimate a graph on. Features as columns and observations as rows. alphas: int or array-like of shape (n_alphas,), dtype=float, default=5 Non-negative. If an integer is given, it fixes the number of points on the grids of alpha to be used. If a list is given, it gives the grid to be used. Returns ------- adjacency matrix : the estimated adjacency matrix. """ scaler = StandardScaler() data = scaler.fit_transform(data) cov = GraphicalLassoCV(alphas=alphas, n_jobs=n_jobs).fit(data) precision_matrix = cov.get_precision() adjacency_matrix = precision_matrix.astype(bool).astype(int) adjacency_matrix[np.diag_indices_from(adjacency_matrix)] = 0 return adjacency_matrix
def helper_graphical_lasso(X, theta_true, tf_names=[]): # Estimate the covariance if args.mode == 'cv': model = GraphicalLassoCV() else: model = GraphicalLasso(alpha=args.alpha_l1, mode=args.mode, tol=1e-7, enet_tol=1e-6, max_iter=100, verbose=False, assume_centered=False) model.fit(X) # cov_ = model.covariance_ prec_ = model.precision_ if args.USE_TF_NAMES == 'yes' and len(tf_names) != 0: prec_ = postprocess_tf(prec_, tf_names) recovery_metrics = report_metrics(np.array(theta_true), prec_) print( 'GLASSO: FDR, TPR, FPR, SHD, nnz_true, nnz_pred, precision, recall, Fb, aupr, auc' ) print('GLASSO: TEST: Recovery of true theta: ', *np.around(recovery_metrics, 3)) return list(recovery_metrics)
def get_conn_matrix(time_series, conn_model, dir_path, node_size, smooth, dens_thresh, network, ID, roi, min_span_tree, disp_filt, parc, prune, atlas_select, uatlas_select, label_names, coords, c_boot, norm, binary): from nilearn.connectome import ConnectivityMeasure from sklearn.covariance import GraphicalLassoCV conn_matrix = None if conn_model == 'corr' or conn_model == 'cor' or conn_model == 'correlation': # credit: nilearn print('\nComputing correlation matrix...\n') conn_measure = ConnectivityMeasure(kind='correlation') conn_matrix = conn_measure.fit_transform([time_series])[0] elif conn_model == 'partcorr' or conn_model == 'parcorr' or conn_model == 'partialcorrelation': # credit: nilearn print('\nComputing partial correlation matrix...\n') conn_measure = ConnectivityMeasure(kind='partial correlation') conn_matrix = conn_measure.fit_transform([time_series])[0] elif conn_model == 'cov' or conn_model == 'covariance' or conn_model == 'covar' or conn_model == 'sps' or conn_model == 'sparse' or conn_model == 'precision': # Fit estimator to matrix to get sparse matrix estimator_shrunk = None estimator = GraphicalLassoCV(cv=5) try: print('\nComputing covariance...\n') estimator.fit(time_series) except: print( 'Unstable Lasso estimation--Attempting to re-run by first applying shrinkage...' ) try: from sklearn.covariance import GraphicalLasso, empirical_covariance, shrunk_covariance emp_cov = empirical_covariance(time_series) for i in np.arange(0.8, 0.99, 0.01): shrunk_cov = shrunk_covariance(emp_cov, shrinkage=i) alphaRange = 10.0**np.arange(-8, 0) for alpha in alphaRange: try: estimator_shrunk = GraphicalLasso(alpha) estimator_shrunk.fit(shrunk_cov) print( "Retrying covariance matrix estimate with alpha=%s" % alpha) if estimator_shrunk is None: pass else: break except: print( "Covariance estimation failed with shrinkage at alpha=%s" % alpha) continue except ValueError: print( 'Unstable Lasso estimation! Shrinkage failed. A different connectivity model may be needed.' ) if estimator is None and estimator_shrunk is None: raise RuntimeError('\nERROR: Covariance estimation failed.') if conn_model == 'sps' or conn_model == 'sparse' or conn_model == 'precision': if estimator_shrunk is None: print( '\nFetching precision matrix from covariance estimator...\n' ) conn_matrix = -estimator.precision_ else: print( '\nFetching shrunk precision matrix from covariance estimator...\n' ) conn_matrix = -estimator_shrunk.precision_ elif conn_model == 'cov' or conn_model == 'covariance' or conn_model == 'covar': if estimator_shrunk is None: print( '\nFetching covariance matrix from covariance estimator...\n' ) conn_matrix = estimator.covariance_ else: conn_matrix = estimator_shrunk.covariance_ elif conn_model == 'QuicGraphicalLasso': try: from inverse_covariance import QuicGraphicalLasso except ImportError: print('Cannot run QuicGraphLasso. Skggm not installed!') # Compute the sparse inverse covariance via QuicGraphLasso # credit: skggm model = QuicGraphicalLasso(init_method='cov', lam=0.5, mode='default', verbose=1) print('\nCalculating QuicGraphLasso precision matrix using skggm...\n') model.fit(time_series) conn_matrix = -model.precision_ elif conn_model == 'QuicGraphLassoCV': try: from inverse_covariance import QuicGraphicalLassoCV except ImportError: print('Cannot run QuicGraphLassoCV. Skggm not installed!') # Compute the sparse inverse covariance via QuicGraphLassoCV # credit: skggm model = QuicGraphicalLassoCV(init_method='cov', verbose=1) print( '\nCalculating QuicGraphLassoCV precision matrix using skggm...\n') model.fit(time_series) conn_matrix = -model.precision_ elif conn_model == 'QuicGraphicalLassoEBIC': try: from inverse_covariance import QuicGraphicalLassoEBIC except ImportError: print('Cannot run QuicGraphLassoEBIC. Skggm not installed!') # Compute the sparse inverse covariance via QuicGraphLassoEBIC # credit: skggm model = QuicGraphicalLassoEBIC(init_method='cov', verbose=1) print( '\nCalculating QuicGraphLassoEBIC precision matrix using skggm...\n' ) model.fit(time_series) conn_matrix = -model.precision_ elif conn_model == 'AdaptiveQuicGraphLasso': try: from inverse_covariance import AdaptiveQuicGraphicalLasso, QuicGraphicalLassoEBIC except ImportError: print('Cannot run AdaptiveGraphLasso. Skggm not installed!') # Compute the sparse inverse covariance via # AdaptiveGraphLasso + QuicGraphLassoEBIC + method='binary' # credit: skggm model = AdaptiveQuicGraphicalLasso( estimator=QuicGraphicalLassoEBIC(init_method='cov', ), method='binary', ) print( '\nCalculating AdaptiveQuicGraphLasso precision matrix using skggm...\n' ) model.fit(time_series) conn_matrix = -model.estimator_.precision_ else: raise ValueError( '\nERROR! No connectivity model specified at runtime. Select a valid estimator using the ' '-mod flag.') if conn_matrix.shape < (2, 2): raise RuntimeError( '\nERROR! Matrix estimation selection yielded an empty or 1-dimensional graph. ' 'Check time-series for errors or try using a different atlas') coords = np.array(coords) label_names = np.array(label_names) return conn_matrix, conn_model, dir_path, node_size, smooth, dens_thresh, network, ID, roi, min_span_tree, disp_filt, parc, prune, atlas_select, uatlas_select, label_names, coords, c_boot, norm, binary
def extract_vector(self): if self.level == 2: self.df_cifti_load = pd.DataFrame( self.fmri_data_np_arr.mean(axis=2)) if type(self.seed_ROI_name) == list and len(self.seed_ROI_name) > 1: if self.seed_analysis_output == 'parcellated': self.df_cifti_load = pd.DataFrame( self.parcellated_cifti_load.get_fdata()) self.df_cifti_load.columns = self.parcel_labels self.df_cifti_load['avg'] = self.df_cifti_load[ self.seed_ROI_name].mean(axis=1) self.parcel_labels = self.df_cifti_load.columns.to_list() else: self.df_cifti_load = pd.DataFrame(self.cifti_load.get_fdata()) df_parcellated_cifti_load = pd.DataFrame( self.parcellated_cifti_load.get_fdata()) df_parcellated_cifti_load.columns = self.parcel_labels self.df_cifti_load['avg'] = df_parcellated_cifti_load[ self.seed_ROI_name].mean(axis=1) self.seed_ROI_name = 'avg' else: if self.seed_analysis_output == 'dense': self.df_cifti_load = pd.DataFrame(self.cifti_load.get_fdata()) df_parcellated_cifti_load = pd.DataFrame( self.parcellated_cifti_load.get_fdata()) df_parcellated_cifti_load.columns = self.parcel_labels self.df_cifti_load[ self.seed_ROI_name] = df_parcellated_cifti_load[ self.seed_ROI_name] else: self.df_cifti_load = pd.DataFrame( self.parcellated_cifti_load.get_fdata()) cifti_np_array = self.df_cifti_load.to_numpy() if self.method == 'correlation': #Pearson correlation coefficients with LedoitWolf covariance estimator #measure = ConnectivityMeasure(kind='correlation',cov_estimator='LedoitWolf') #Pearson correlation coefficients based oemperical covariance (i.e. standard) measure = ConnectivityMeasure(kind='correlation', cov_estimator=EmpiricalCovariance()) elif self.method == 'covariance': #LedoitWolf estimator measure = ConnectivityMeasure(kind='covariance') elif self.method == 'partial_correlation': # Partial correlation with LedoitWolf covariance estimator measure = ConnectivityMeasure(kind='partial correlation') elif self.method == 'precision': measure = ConnectivityMeasure(kind='precision') elif 'sparse' in self.method: measure = GraphicalLassoCV() if 'sparse' in self.method: measure.fit(cifti_np_array) if 'covariance' in self.method: network_matrix = measure.covariance_ elif 'precision' in self.method: network_matrix = measure.precision_ else: network_matrix = measure.fit_transform([cifti_np_array])[0] df_network_matrix = pd.DataFrame(network_matrix) df_network_matrix.columns = self.parcel_labels if self.seed_ROI_name == 'avg': # take everything except last element, i.e. avg. Need to do this because downstream this object must match grayordinate_file self.r_functional_vector = df_network_matrix[ self.seed_ROI_name][:-1].to_numpy() else: self.r_functional_vector = np.squeeze( df_network_matrix[self.seed_ROI_name].to_numpy()) self.z_functional_vector = 0.5 * ( np.log(1 + self.r_functional_vector) - np.log(1 - self.r_functional_vector))
subject_time_series.append(region_ts) ############################################################################## # Computing group-sparse precision matrices # ------------------------------------------ from nilearn.connectome import GroupSparseCovarianceCV gsc = GroupSparseCovarianceCV(verbose=2) gsc.fit(subject_time_series) try: from sklearn.covariance import GraphicalLassoCV except ImportError: # for Scitkit-Learn < v0.20.0 from sklearn.covariance import GraphLassoCV as GraphicalLassoCV gl = GraphicalLassoCV(verbose=2) gl.fit(np.concatenate(subject_time_series)) ############################################################################## # Displaying results # ------------------- atlas_img = msdl_atlas_dataset.maps atlas_region_coords = plotting.find_probabilistic_atlas_cut_coords(atlas_img) labels = msdl_atlas_dataset.labels plotting.plot_connectome(gl.covariance_, atlas_region_coords, edge_threshold='90%', title="Covariance", display_mode="lzr") plotting.plot_connectome(-gl.precision_,
random_state=prng) cov = linalg.inv(prec) d = np.sqrt(np.diag(cov)) cov /= d cov /= d[:, np.newaxis] prec *= d prec *= d[:, np.newaxis] X = prng.multivariate_normal(np.zeros(n_features), cov, size=n_samples) X -= X.mean(axis=0) X /= X.std(axis=0) # ############################################################################# # Estimate the covariance emp_cov = np.dot(X.T, X) / n_samples model = GraphicalLassoCV(cv=5) model.fit(X) cov_ = model.covariance_ prec_ = model.precision_ lw_cov_, _ = ledoit_wolf(X) lw_prec_ = linalg.inv(lw_cov_) # ############################################################################# # Plot the results plt.figure(figsize=(10, 6)) plt.subplots_adjust(left=0.02, right=0.98) # plot the covariances covs = [('Empirical', emp_cov), ('Ledoit-Wolf', lw_cov_), ('GraphicalLassoCV', cov_), ('True', cov)]
# --------------------- # # We start by estimating the signal **covariance** matrix. Here the # number of ROIs exceeds the number of samples, print('time series has {0} samples'.format(timeseries.shape[0])) ############################################################################### # in which situation the graphical lasso **sparse inverse covariance** # estimator captures well the covariance **structure**. try: from sklearn.covariance import GraphicalLassoCV except ImportError: # for Scitkit-Learn < v0.20.0 from sklearn.covariance import GraphLassoCV as GraphicalLassoCV covariance_estimator = GraphicalLassoCV(cv=3, verbose=1) ############################################################################### # We just fit our regions signals into the `GraphicalLassoCV` object covariance_estimator.fit(timeseries) ############################################################################### # and get the ROI-to-ROI covariance matrix. matrix = covariance_estimator.covariance_ print('Covariance matrix has shape {0}.'.format(matrix.shape)) ############################################################################### # Plot matrix, graph, and strength # -------------------------------- # # We use `:func: nilearn.plotting.plot_matrix` to visualize our correlation matrix
ns = nitk.NeighbourhoodSelectionColumnwiseCV() ns.fit(X) tpr, fpr, prec = nitk.methods.calculate_matrix_accuracy(K, ns.precision_) ns_f1[i] = nitk.methods.calculate_f1_score(tpr, prec) te = nitk.ThresholdEstimatorCV() te.fit(X) tpr, fpr, prec = nitk.methods.calculate_matrix_accuracy(K, te.covariance_) ts_f1[i] = nitk.methods.calculate_f1_score(tpr, prec) sc = nitk.SCIOColumnwiseCV() sc.fit(X) tpr, fpr, prec = nitk.methods.calculate_matrix_accuracy(K, sc.precision_) sc_f1[i] = nitk.methods.calculate_f1_score(tpr, prec) gl = GraphicalLassoCV() gl.fit(X) tpr, fpr, prec = nitk.methods.calculate_matrix_accuracy(K, gl.precision_) gl_f1[i] = nitk.methods.calculate_f1_score(tpr, prec) sli = nitk.ScaledLassoInference() sli.fit(X) tpr, fpr, prec = nitk.methods.calculate_matrix_accuracy(K, sli.precision_) sli_f1[i] = nitk.methods.calculate_f1_score(tpr, prec) cli = nitk.CLIMECV() cli.fit(X) tpr, fpr, prec = nitk.methods.calculate_matrix_accuracy(K, cli.precision_) cli_f1[i] = nitk.methods.calculate_f1_score(tpr, prec) print("Graphical Lasso & %s & %s & %6.3f $\pm$ %6.3f" % (p, n, np.mean(gl_f1), np.std(gl_f1)))
data = xlrd.open_workbook('nlmy.xlsx') table = data.sheet_by_name('Animal_husbandry') stock_list = table.col_values(colx=1, start_rowx=1, end_rowx=33) stock_list = [str(i) for i in stock_list] batch_K_data = get_kdata(stock_list, start='2013-09-01', end='2018-09-01') # 查看最近五年的数据 # print(batch_K_data.info()) stock_dataset, selected_stocks = preprocess_data(batch_K_data, min_K_num=1100) stock_dataset2, selected_stocks2 = preprocess_data2(batch_K_data, min_K_num=1100) print("The selected stocks is: ", selected_stocks) # 这是实际使用的股票列表stock_dataset,selected_stocks=preprocess_data2(batch_K_data,min_K_num=1100) # 从相关性中学习其图形结构 edge_model1 = GraphicalLassoCV(cv=3) edge_model2 = GraphicalLassoCV(cv=3) # edge_model.fit(stock_dataset) edge_model1.fit(stock_dataset) edge_model2.fit(stock_dataset2) # 使用近邻传播算法构建模型,并训练LassoCV graph _, labels1 = affinity_propagation(edge_model1.covariance_) _, labels2 = affinity_propagation(edge_model2.covariance_) n_labels = max(labels1) print('Stock Clusters: {}'.format(n_labels + 1)) # 10,即得到10个类别 sz50_df2 = stock_list # print(sz50_df2) for i in range(n_labels + 1): print('Cluster: {}----> stocks: {}'.format(i, ','.join(np.array(selected_stocks)[labels1 == i]))) # 这个只有股票代码而不是股票名称
standardize=True, memory='nilearn_cache', verbose=5) time_series = masker.fit_transform(data.func[0], confounds=data.confounds) ############################################################################## # Compute the sparse inverse covariance # -------------------------------------- try: from sklearn.covariance import GraphicalLassoCV except ImportError: # for Scitkit-Learn < v0.20.0 from sklearn.covariance import GraphLassoCV as GraphicalLassoCV estimator = GraphicalLassoCV() estimator.fit(time_series) ############################################################################## # Display the connectome matrix # ------------------------------ from nilearn import plotting # Display the covariance # The covariance can be found at estimator.covariance_ plotting.plot_matrix(estimator.covariance_, labels=labels, figure=(9, 7), vmax=1, vmin=-1, title='Covariance')
def main(): mean = torch.tensor(np.zeros(32), dtype=torch.float32) diag = torch.tensor(np.ones(32), dtype=torch.float32) X = torch.eye(32, dtype=torch.float32) X[5, 10] = X[10, 5] = X[19, 20] = X[20, 19] = X[1, 31] = X[31, 1] = -0.5 population = Gaussian_Distribution(mean=mean, diag=diag, sub=-0.2, type='DIY', slash=1, prec=X) truth = population.invcov.numpy() n = 400 p = population.dim print(truth) heatmap(truth) data = pd.read_csv("chain.csv") sample = data.values[1:, 1:] sample = z_score(sample) emp_cov = sample_cov(sample) score = dict() score['log_lik'] = [] score['AIC'] = [] score['non_zero'] = [] alpha_list = np.hstack((np.arange(1e-5, 0.1, 0.002), np.arange(0.1, 0.3, 0.01))) data = np.array(sample) for alpha in alpha_list: out_dict = cross_val_score_ProxGrad(data, alpha=alpha, type='FISTA') score['log_lik'].append(out_dict['log_lik']) score['AIC'].append(out_dict['AIC']) score['non_zero'].append(out_dict['non_zero']) plt.plot(alpha_list, score['log_lik']) plt.show() plt.plot(alpha_list, score['AIC']) plt.show() plt.plot(alpha_list, score['non_zero']) plt.show() model = ProxGrad() l = len(alpha_list) alpha = 0 log_lik = -1e12 for i in range(0, l): if score['log_lik'][i] > log_lik: alpha = alpha_list[i] log_lik = score['log_lik'][i] print(alpha) prec = model.fit_FISTA(emp_cov, alpha) heatmap(prec) print('nonzero:', L0_penal(prec)) alpha = 0 aic = 1e12 for i in range(0, l): if score['AIC'][i] < aic: alpha = alpha_list[i] aic = score['AIC'][i] print(alpha) prec = model.fit_FISTA(emp_cov, alpha) heatmap(prec) print('nonzero:', L0_penal(prec)) model = GraphicalLassoCV(tol=1e-8) model.fit(sample) heatmap(model.precision_) print('nonzero:', L0_penal(model.precision_))
def main(): # 'Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume' df = pd.read_csv(pt.join(DATA_ROOT, '1999_2018_complete.csv')) # drop malaysia and venezuela df.drop(['malaysia', 'venezuela'], axis=1, inplace=True) # print(df.columns[2:]) data = df.values[:, 2:] data = preprocessing.scale(data, axis=1) print(data.shape) # plt.plot(data[:, 6], c='b', label='Japan') # plt.plot(data[:, 13], c='g', label='Sri Lanka') # plt.legend() # plt.show() # Estimate the covariance emp_cov = np.dot(data.T, data) / data.shape[0] for count in range(20): temp_data = data[count * 242:(count + 1) * 242] # GraphicalLasso model = GraphicalLassoCV() model.fit(temp_data) cov_ = model.covariance_ prec_ = model.precision_ # print(model.alpha_) # print(model.cv_alphas_) # print(model.grid_scores_) # print(model.n_iter_) # Ledoit-Wolf lw_cov_, _ = ledoit_wolf(data) lw_prec_ = linalg.inv(lw_cov_) # ############################################################################# # Plot the results # plt.figure(figsize=(8, 6)) # plt.subplots_adjust(left=0.02, right=0.98) # # # plot the covariances # covs = [('Empirical', emp_cov), ('Ledoit-Wolf', # lw_cov_), ('GraphicalLassoCV', cov_)] # vmax = cov_.max() # for i, (name, this_cov) in enumerate(covs): # plt.subplot(2, 3, i + 1) # plt.imshow(this_cov, interpolation='nearest', vmin=-vmax, vmax=vmax, # cmap=plt.cm.RdBu_r) # plt.xticks(()) # plt.yticks(()) # plt.title('%s covariance' % name) # # # plot the precisions # precs = [('Empirical', linalg.inv(emp_cov)), ('Ledoit-Wolf', lw_prec_), # ('GraphicalLasso', prec_)] # vmax = .9 * prec_.max() # for i, (name, this_prec) in enumerate(precs): # ax = plt.subplot(2, 3, i + 4) # plt.imshow(np.ma.masked_equal(this_prec, 0), # interpolation='nearest', vmin=-vmax, vmax=vmax, # cmap=plt.cm.RdBu_r) # plt.xticks(()) # plt.yticks(()) # plt.title('%s precision' % name) # if hasattr(ax, 'set_facecolor'): # ax.set_facecolor('.7') # else: # ax.set_axis_bgcolor('.7') # plt.show() # print(prec_) name = 'GraphicalLasso' this_prec = prec_ vmax = .9 * prec_.max() plt.figure() ax = plt.subplot(1, 1, 1) plt.imshow(np.ma.masked_equal(this_prec, 0), interpolation='nearest', vmin=-vmax, vmax=vmax, cmap=plt.cm.RdBu_r) plt.xticks(()) plt.yticks(()) plt.title('year: %d' % (1999 + count)) if hasattr(ax, 'set_facecolor'): ax.set_facecolor('.7') else: ax.set_axis_bgcolor('.7') plt.show()
def get_conn_matrix(time_series, conn_model, dir_path, node_size, smooth, dens_thresh, network, ID, roi, min_span_tree, disp_filt, parc, prune, atlas, uatlas, labels, coords, norm, binary, hpass, extract_strategy): """ Computes a functional connectivity matrix based on a node-extracted time-series array. Includes a library of routines across Nilearn, scikit-learn, and skggm packages, among others. Parameters ---------- time_series : array 2D m x n array consisting of the time-series signal for each ROI node where m = number of scans and n = number of ROI's. conn_model : str Connectivity estimation model (e.g. corr for correlation, cov for covariance, sps for precision covariance, partcorr for partial correlation). sps type is used by default. dir_path : str Path to directory containing subject derivative data for given run. node_size : int Spherical centroid node size in the case that coordinate-based centroids are used as ROI's. smooth : int Smoothing width (mm fwhm) to apply to time-series when extracting signal from ROI's. dens_thresh : bool Indicates whether a target graph density is to be used as the basis for thresholding. network : str Resting-state network based on Yeo-7 and Yeo-17 naming (e.g. 'Default') used to filter nodes in the study of brain subgraphs. ID : str A subject id or other unique identifier. roi : str File path to binarized/boolean region-of-interest Nifti1Image file. min_span_tree : bool Indicates whether local thresholding from the Minimum Spanning Tree should be used. disp_filt : bool Indicates whether local thresholding using a disparity filter and 'backbone network' should be used. parc : bool Indicates whether to use parcels instead of coordinates as ROI nodes. prune : bool Indicates whether to prune final graph of disconnected nodes/isolates. atlas : str Name of atlas parcellation used. uatlas : str File path to atlas parcellation Nifti1Image in MNI template space. labels : list List of string labels corresponding to ROI nodes. coords : list List of (x, y, z) tuples corresponding to a coordinate atlas used or which represent the center-of-mass of each parcellation node. norm : int Indicates method of normalizing resulting graph. binary : bool Indicates whether to binarize resulting graph edges to form an unweighted graph. hpass : bool High-pass filter values (Hz) to apply to node-extracted time-series. extract_strategy : str The name of a valid function used to reduce the time-series region extraction. Returns ------- conn_matrix : array Adjacency matrix stored as an m x n array of nodes and edges. conn_model : str Connectivity estimation model (e.g. corr for correlation, cov for covariance, sps for precision covariance, partcorr for partial correlation). sps type is used by default. dir_path : str Path to directory containing subject derivative data for given run. node_size : int Spherical centroid node size in the case that coordinate-based centroids are used as ROI's for tracking. smooth : int Smoothing width (mm fwhm) to apply to time-series when extracting signal from ROI's. dens_thresh : bool Indicates whether a target graph density is to be used as the basis for thresholding. network : str Resting-state network based on Yeo-7 and Yeo-17 naming (e.g. 'Default') used to filter nodes in the study of brain subgraphs. ID : str A subject id or other unique identifier. roi : str File path to binarized/boolean region-of-interest Nifti1Image file. min_span_tree : bool Indicates whether local thresholding from the Minimum Spanning Tree should be used. disp_filt : bool Indicates whether local thresholding using a disparity filter and 'backbone network' should be used. parc : bool Indicates whether to use parcels instead of coordinates as ROI nodes. prune : bool Indicates whether to prune final graph of disconnected nodes/isolates. atlas : str Name of atlas parcellation used. uatlas : str File path to atlas parcellation Nifti1Image in MNI template space. labels : list List of string labels corresponding to graph nodes. coords : list List of (x, y, z) tuples corresponding to a coordinate atlas used or which represent the center-of-mass of each parcellation node. norm : int Indicates method of normalizing resulting graph. binary : bool Indicates whether to binarize resulting graph edges to form an unweighted graph. hpass : bool High-pass filter values (Hz) to apply to node-extracted time-series. extract_strategy : str The name of a valid function used to reduce the time-series region extraction. References ---------- .. [1] Varoquaux, G., & Craddock, R. C. (2013). Learning and comparing functional connectomes across subjects. NeuroImage. https://doi.org/10.1016/j.neuroimage.2013.04.007 .. [2] Jason Laska, Manjari Narayan, 2017. skggm 0.2.7: A scikit-learn compatible package for Gaussian and related Graphical Models. doi:10.5281/zenodo.830033 """ from nilearn.connectome import ConnectivityMeasure from sklearn.covariance import GraphicalLassoCV conn_matrix = None if conn_model == 'corr' or conn_model == 'cor' or conn_model == 'correlation': # credit: nilearn print('\nComputing correlation matrix...\n') conn_measure = ConnectivityMeasure(kind='correlation') conn_matrix = conn_measure.fit_transform([time_series])[0] elif conn_model == 'partcorr' or conn_model == 'parcorr' or conn_model == 'partialcorrelation': # credit: nilearn print('\nComputing partial correlation matrix...\n') conn_measure = ConnectivityMeasure(kind='partial correlation') conn_matrix = conn_measure.fit_transform([time_series])[0] elif conn_model == 'cov' or conn_model == 'covariance' or conn_model == 'covar' or conn_model == 'sps' or \ conn_model == 'sparse' or conn_model == 'precision': # Fit estimator to matrix to get sparse matrix estimator_shrunk = None estimator = GraphicalLassoCV(cv=5) try: print('\nComputing covariance...\n') estimator.fit(time_series) except: print('Unstable Lasso estimation--Attempting to re-run by first applying shrinkage...') try: from sklearn.covariance import GraphicalLasso, empirical_covariance, shrunk_covariance emp_cov = empirical_covariance(time_series) for i in np.arange(0.8, 0.99, 0.01): shrunk_cov = shrunk_covariance(emp_cov, shrinkage=i) alphaRange = 10.0 ** np.arange(-8, 0) for alpha in alphaRange: try: estimator_shrunk = GraphicalLasso(alpha) estimator_shrunk.fit(shrunk_cov) print(f"Retrying covariance matrix estimate with alpha={alpha}") if estimator_shrunk is None: pass else: break except: print(f"Covariance estimation failed with shrinkage at alpha={alpha}") continue except ValueError: print('Unstable Lasso estimation! Shrinkage failed. A different connectivity model may be needed.') if estimator is None and estimator_shrunk is None: raise RuntimeError('\nERROR: Covariance estimation failed.') if conn_model == 'sps' or conn_model == 'sparse' or conn_model == 'precision': if estimator_shrunk is None: print('\nFetching precision matrix from covariance estimator...\n') conn_matrix = -estimator.precision_ else: print('\nFetching shrunk precision matrix from covariance estimator...\n') conn_matrix = -estimator_shrunk.precision_ elif conn_model == 'cov' or conn_model == 'covariance' or conn_model == 'covar': if estimator_shrunk is None: print('\nFetching covariance matrix from covariance estimator...\n') conn_matrix = estimator.covariance_ else: conn_matrix = estimator_shrunk.covariance_ elif conn_model == 'QuicGraphicalLasso': try: from inverse_covariance import QuicGraphicalLasso except ImportError: print('Cannot run QuicGraphLasso. Skggm not installed!') # Compute the sparse inverse covariance via QuicGraphLasso # credit: skggm model = QuicGraphicalLasso( init_method='cov', lam=0.5, mode='default', verbose=1) print('\nCalculating QuicGraphLasso precision matrix using skggm...\n') model.fit(time_series) conn_matrix = -model.precision_ elif conn_model == 'QuicGraphicalLassoCV': try: from inverse_covariance import QuicGraphicalLassoCV except ImportError: print('Cannot run QuicGraphLassoCV. Skggm not installed!') # Compute the sparse inverse covariance via QuicGraphLassoCV # credit: skggm model = QuicGraphicalLassoCV( init_method='cov', verbose=1) print('\nCalculating QuicGraphLassoCV precision matrix using skggm...\n') model.fit(time_series) conn_matrix = -model.precision_ elif conn_model == 'QuicGraphicalLassoEBIC': try: from inverse_covariance import QuicGraphicalLassoEBIC except ImportError: print('Cannot run QuicGraphLassoEBIC. Skggm not installed!') # Compute the sparse inverse covariance via QuicGraphLassoEBIC # credit: skggm model = QuicGraphicalLassoEBIC( init_method='cov', verbose=1) print('\nCalculating QuicGraphLassoEBIC precision matrix using skggm...\n') model.fit(time_series) conn_matrix = -model.precision_ elif conn_model == 'AdaptiveQuicGraphicalLasso': try: from inverse_covariance import AdaptiveQuicGraphicalLasso, QuicGraphicalLassoEBIC except ImportError: print('Cannot run AdaptiveGraphLasso. Skggm not installed!') # Compute the sparse inverse covariance via # AdaptiveGraphLasso + QuicGraphLassoEBIC + method='binary' # credit: skggm model = AdaptiveQuicGraphicalLasso( estimator=QuicGraphicalLassoEBIC( init_method='cov', ), method='binary', ) print('\nCalculating AdaptiveQuicGraphLasso precision matrix using skggm...\n') model.fit(time_series) conn_matrix = -model.estimator_.precision_ else: raise ValueError('\nERROR! No connectivity model specified at runtime. Select a valid estimator using the ' '-mod flag.') # Enforce symmetry conn_matrix = np.maximum(conn_matrix, conn_matrix.T) if conn_matrix.shape < (2, 2): raise RuntimeError('\nERROR! Matrix estimation selection yielded an empty or 1-dimensional graph. ' 'Check time-series for errors or try using a different atlas') coords = np.array(coords) labels = np.array(labels) del time_series return (conn_matrix, conn_model, dir_path, node_size, smooth, dens_thresh, network, ID, roi, min_span_tree, disp_filt, parc, prune, atlas, uatlas, labels, coords, norm, binary, hpass, extract_strategy)
random_state=prng) cov = linalg.inv(prec) d = np.sqrt(np.diag(cov)) cov /= d cov /= d[:, np.newaxis] prec *= d prec *= d[:, np.newaxis] X = prng.multivariate_normal(np.zeros(n_features), cov, size=n_samples) X -= X.mean(axis=0) X /= X.std(axis=0) # ############################################################################# # Estimate the covariance emp_cov = np.dot(X.T, X) / n_samples model = GraphicalLassoCV() model.fit(X) cov_ = model.covariance_ prec_ = model.precision_ lw_cov_, _ = ledoit_wolf(X) lw_prec_ = linalg.inv(lw_cov_) # ############################################################################# # Plot the results plt.figure(figsize=(10, 6)) plt.subplots_adjust(left=0.02, right=0.98) # plot the covariances covs = [ ("Empirical", emp_cov),
def main(): mean = torch.tensor(np.ones(16), dtype=torch.float32) diag = torch.tensor(np.ones(16), dtype=torch.float32) population = Gaussian_Distribution(mean=mean, diag=diag, sub=0.25, type='chain', slash=1) truth = population.invcov.numpy() n = 1000 d = population.dim print(truth) dist, sample, _, S = population.generate(n, numpy_like=True) # ############################################################################# # Generate the data n_samples = 60 n_features = 20 prng = np.random.RandomState(1) prec = make_sparse_spd_matrix(n_features, alpha=.98, smallest_coef=.4, largest_coef=.7, random_state=prng) cov = linalg.inv(prec) d = np.sqrt(np.diag(cov)) cov /= d cov /= d[:, np.newaxis] prec *= d prec *= d[:, np.newaxis] X = prng.multivariate_normal(np.zeros(n_features), cov, size=n_samples) X -= X.mean(axis=0) X /= X.std(axis=0) #prec = population.invcov # ############################################################################# # Estimate the covariance emp_cov = np.dot(X.T, X) / n_samples model = GraphicalLassoCV() model.fit(sample) cov_ = model.covariance_ prec_ = model.precision_ lw_cov_, _ = ledoit_wolf(X) lw_prec_ = linalg.inv(lw_cov_) # ############################################################################# # Plot the results plt.figure(figsize=(10, 6)) plt.subplots_adjust(left=0.02, right=0.98) # plot the covariances covs = [('Empirical', emp_cov), ('Ledoit-Wolf', lw_cov_), ('GraphicalLassoCV', cov_), ('True', cov)] vmax = cov_.max() for i, (name, this_cov) in enumerate(covs): plt.subplot(2, 4, i + 1) plt.imshow(this_cov, interpolation='nearest', vmin=-vmax, vmax=vmax, cmap=plt.cm.RdBu_r) plt.xticks(()) plt.yticks(()) plt.title('%s covariance' % name) # plot the precisions precs = [('Empirical', linalg.inv(emp_cov)), ('Ledoit-Wolf', lw_prec_), ('GraphicalLasso', prec_), ('True', prec)] vmax = .9 * prec_.max() for i, (name, this_prec) in enumerate(precs): ax = plt.subplot(2, 4, i + 5) plt.imshow(np.ma.masked_equal(this_prec, 0), interpolation='nearest', vmin=-vmax, vmax=vmax, cmap=plt.cm.RdBu_r) plt.xticks(()) plt.yticks(()) plt.title('%s precision' % name) if hasattr(ax, 'set_facecolor'): ax.set_facecolor('.7') else: ax.set_axis_bgcolor('.7') # plot the model selection metric plt.figure(figsize=(4, 3)) plt.axes([.2, .15, .75, .7]) plt.plot(model.cv_alphas_, np.mean(model.grid_scores_, axis=1), 'o-') plt.axvline(model.alpha_, color='.5') plt.title('Model selection') plt.ylabel('Cross-validation score') plt.xlabel('alpha') plt.show()
def get_covariance(data, method, lambda_val='CV', do_scale=False, n_cv_folds=None): # default cov if it is not calculated properly cov = -1 # scale timecourse if do_scale: data = scale(data, axis=1) # select method if method == 'QUIC': if lambda_val == 'CV': # set up model model = QuicGraphicalLassoCV(cv=n_cv_folds) # fit data to model and return resulting covariance model.fit(np.transpose(data)) return model.covariance_ elif lambda_val == 'EBIC': # set up model model = QuicGraphicalLassoEBIC() # fit data to model and return resulting covariance model.fit(np.transpose(data)) return model.covariance_ elif isinstance(lambda_val, float) and lambda_val > 0 and lambda_val < 1: # set up model model = QuicGraphicalLasso(lam=lambda_val) # fit data to model and return resulting covariance model.fit(data) return model.covariance_ else: print('Error in QUIC covariance:') print( 'lambda_val must be a float between 0 and 1, "CV" to find the best value by cross-validation, or "EBIC" to use extended Bayesian information criterion for model selection.' ) elif method == 'graphLasso': # transpose data as graphLasso likes it this way round data = np.transpose(data) # select whether to use supplied regularisation parameter or find the # best regularisation parameter by cross validation and maximum likelihood # use scikit-learn implementation of graph lasso and CV graph lasso if lambda_val == 'CV': try: model = GraphicalLassoCV(max_iter=1500, cv=n_cv_folds, assume_centered=True) model.fit(data) cov = model.covariance_ except: print( 'An error in cross validated graphLasso calculation occured.' ) elif isinstance(lambda_val, float) and lambda_val > 0 and lambda_val < 1: try: model = GraphicalLasso(alpha=lambda_val, mode='cd', tol=0.0001, max_iter=1500, verbose=False) model.fit(data) cov = model.covariance_ except (FloatingPointError, e): print( 'A floating point error in cross validated graphLasso calculation occured.' ) print(e) else: print('Error in graphLasso covariance:') print( 'lambda_val must be a float between 0 and 1, or "CV" to find the best value by cross-validation' ) # select method else: print('Method must be one of "graphLasso" or "QUIC".') return cov
def sparce_invcov(self, df, cols=None, style="GraphLassoCV", param=0.2, layout="circular", center=None, figsize=(7, 7)): """ cols: columns to calculate. If None, takes all numerical columns style: GraphLassoCV or LedoitWolf param: Parameter to pass to fitting algorithm. If GraphLasso, =alpha; if LedoitWolf, =threshold layout: choose between "circular", "spring", "shell" center: Put a certain colname in the center of the graph Sparse covariance matrix estimation Plot the sparce precision matrix """ new_df = Utility().normalize(df).dropna() # Remove NA, normalize if cols == None: cols = df._get_numeric_data().columns data = new_df[cols] if style == "GraphLassoCV": model = GraphicalLassoCV(alphas=[param, param], cv=10, max_iter=5000) model.fit(data) sparce_mat = np.zeros(np.shape(model.precision_)) sparce_mat[model.precision_ != 0] = -1 np.fill_diagonal(sparce_mat, 1) else: # Style == LedoitWolf model = LedoitWolf() model.fit(data) sparce_mat = np.zeros(np.shape(model.get_precision())) sparce_mat[np.abs(model.get_precision()) > param] = -1 np.fill_diagonal(sparce_mat, 1) sparce_mat = pd.DataFrame(sparce_mat, index=data.columns, columns=data.columns) # NetworkX Graph fig, ax = plt.subplots(figsize=figsize) G = nx.from_pandas_adjacency(sparce_mat) pos = { "circular": nx.drawing.circular_layout, "shell": nx.drawing.shell_layout, "spring": nx.drawing.spring_layout, }[layout](G, scale=2) pos[center] = np.array([0, 0]) node_color = [ 'mintcream' if node == center else 'mintcream' for node in G.nodes ] node_size = [ len(node) * 1500 if node == center else len(node) * 500 for node in G.nodes() ] nodes = nx.draw_networkx_nodes(G, pos, node_shape='o', node_color=node_color, node_size=node_size) nodes.set_edgecolor('k') nx.draw_networkx_edges(G, pos, edge_color='r', width=2.0, alpha=0.8) nx.draw_networkx_labels(G, pos, font_weight='bold', font_size=10) plt.axis('off') plt.tight_layout() # Display precision matrix as heatmap fig, ax = plt.subplots(figsize=(5, 5)) sns.heatmap(sparce_mat, vmax=1, vmin=-1, linewidth=0.1, cmap=plt.cm.RdBu_r, cbar=False) ax.set_ylim(sparce_mat.T.shape[0] - 1e-9, -1e-9) plt.title('Sparse Inverse Covariance') plt.show() return sparce_mat
print(f"gradients: {gradients}") # update alpha alpha_grad = np.mean(gradients) diff = np.linalg.norm(alpha_grad) curr_alpha = gradient_update(curr_alpha, alpha_grad) curr_alpha = max(curr_alpha, 0) print(alpha_grad, curr_alpha) # s = prob(samples, new_alpha) return curr_alpha if __name__ == '__main__': import causaldag as cd from sklearn.covariance import GraphicalLassoCV d = cd.rand.directed_erdos(5, .5) g = cd.rand.rand_weights(d) samples = g.sample(100) cvgm_alpha = cvgm_glasso(samples, 1) print(cvgm_alpha) glcv = GraphicalLassoCV(alphas=[.1, .2, .3, .4, .5, .6, .7, .8, .9], cv=10) glcv.fit(samples) print(glcv.alpha_)