rcot = RCOT2(significance=parameters['cond_ind_test.significance'], num_f=parameters['cond_ind_test.num_f']) pcmci = PCMCI(dataframe, cond_ind_test=rcot, selected_variables=parameters['selected_variables'], var_names=parameters['var_names'], verbosity=10) q_matrix = pcmci.get_corrected_pvalues(p_matrix=p_matrix, fdr_method='fdr_bh') q_matrix_tsbh = pcmci.get_corrected_pvalues(p_matrix=p_matrix, fdr_method='fdr_tsbh') #%% print results pcmci._print_significant_links( p_matrix = p_matrix, q_matrix = q_matrix, val_matrix = val_matrix, alpha_level = 0.1) #%% get selected parents and fit linear model q_0 = 0.05 parameters['q_0'] = q_0 parameters['q_matrix'] = q_matrix parent_dict = pcmci._return_significant_parents(q_matrix, val_matrix, q_0) parameters['parent_dict'] = parent_dict med = LinearMediation(dataframe=dataframe) #, data_transform=False) med.fit_model(all_parents=parent_dict['parents'])
# ====================================================================================================================== results = pcmci.run_pcmci( tau_max=tau_max, pc_alpha=pc_alpha, tau_min=tau_min, max_combinations=1) #selected_links = dictionary/None #results = pcmci.run_pcmci(selected_links =None, tau_max=tau_max, pc_alpha = pc_alpha, tau_min = tau_min,save_iterations=False, max_conds_dim=None, max_combinations=1, max_conds_py=None, max_conds_px=None) #selected_links = dictionary/None #results = pcmci.run_pcmci(selected_links = dictionary, tau_max=tau_max, pc_alpha = pc_alpha, tau_min = tau_min,save_iterations=False, max_conds_dim=None, max_combinations=1, max_conds_py=None, max_conds_px=None) #selected_links = dictionary/None q_matrix = pcmci.get_corrected_pvalues(p_matrix=results['p_matrix'], fdr_method='fdr_bh') pcmci._print_significant_links( p_matrix=results['p_matrix'], q_matrix=q_matrix, #results['p_matrix'] val_matrix=results['val_matrix'], alpha_level=alpha_level) sig = pcmci._return_significant_parents( pq_matrix=q_matrix, val_matrix=results['val_matrix'], alpha_level=alpha_level) all_parents = sig['parents'] link_matrix = sig['link_matrix'] # parents of index of interest: # parents_neighbors = all_parents, estimates, iterations parents_MT = all_parents[0]
def caus_gpdc(data, var_names): import numpy as np import matplotlib as mpl from matplotlib import pyplot as plt import sklearn import tigramite from tigramite import data_processing as pp from tigramite import plotting as tp from tigramite.pcmci import PCMCI from tigramite.independence_tests import ParCorr, GPDC, CMIknn, CMIsymb from tigramite.models import LinearMediation, Prediction data_mask_row = np.zeros(len(data)) for i in range(68904): if (i % 72) < 30 or (i % 72) > 47: data_mask_row[i] = True data_mask = np.zeros(data.shape) data_mask[:, 0] = data_mask_row data_mask[:, 1] = data_mask_row data_mask[:, 2] = data_mask_row data_mask[:, 9] = data_mask_row data_mask[:, 10] = data_mask_row data_mask[:, 11] = data_mask_row dataframe = pp.DataFrame(data, mask=data_mask) datatime = np.arange(len(data)) # tp.plot_timeseries(data, datatime, var_names, use_mask=True, # mask=data_mask, grey_masked_samples='data') gpdc = GPDC(significance='analytic', gp_params=None, use_mask=True, mask_type='y') gpdc.generate_and_save_nulldists(sample_sizes=range(495, 501), null_dist_filename='dc_nulldists.npz') gpdc.null_dist_filename = 'dc_nulldists.npz' pcmci_gpdc = PCMCI(dataframe=dataframe, cond_ind_test=gpdc, var_names=var_names, verbosity=1) # correlations = pcmci.get_lagged_dependencies(tau_max=20) # lag_func_matrix = tp.plot_lagfuncs(val_matrix=correlations, # setup_args={'var_names':var_names, # 'x_base':5, 'y_base':.5}) results = pcmci_gpdc.run_pcmci(tau_max=6, tau_min=1, pc_alpha=0.01) # print("p-values") # print (results['p_matrix'].round(3)) # print("MCI partial correlations") # print (results['val_matrix'].round(2)) q_matrix = pcmci_gpdc.get_corrected_pvalues(p_matrix=results['p_matrix'], fdr_method='fdr_bh') pcmci_gpdc._print_significant_links(p_matrix=results['p_matrix'], q_matrix=q_matrix, val_matrix=results['val_matrix'], alpha_level=0.01) link_matrix = pcmci_gpdc._return_significant_parents( pq_matrix=q_matrix, val_matrix=results['val_matrix'], alpha_level=0.01)['link_matrix'] tp.plot_time_series_graph( val_matrix=results['val_matrix'], link_matrix=link_matrix, var_names=var_names, link_colorbar_label='MCI', ) return results, link_matrix