def time_lagged_correlation(): """Runs the time-lagged correlation analysis experiment. This function alculates the time-lagged correlation between the variables for lags of up to 48 hours and plots the result as a scatterplot matrix. """ var_names = [ "dayOfYear", "minuteOfYear", "minuteOfDay", "dayOfWeek", "isWeekend", "humidity_sensor", "temperature", "precip_intensity", "cloud_cover", "p1", "p2", "dew_point", "wind_speed" ] tau_min = 0 tau_max = 48 dataframe, var_list = generate_dataframe(var_names) print(f"Variable names: {var_names}") ci_test = ParCorr(significance='analytic') pcmci = PCMCI(dataframe=dataframe, cond_ind_test=ci_test, verbosity=1) correlations = pcmci.get_lagged_dependencies(tau_min=tau_min, tau_max=tau_max) lag_func_matrix = tp.plot_lagfuncs( name="experiments/causal_discovery/results/time_lagged_correlation.png", val_matrix=correlations, setup_args={ 'var_names': var_names, 'figsize': (50, 25), 'label_fontsize': 12, 'label_space_top': 0.025, 'label_space_left': 0.05, 'lag_units': 'hours', 'x_base': 6, 'y_base': .5 }) print(lag_func_matrix)
#var_names = [r'$X^0$', r'$X^1$', r'$X^2$', r'$X^3$'] dataframe = pp.DataFrame(data, datatime=np.arange(len(data)), var_names=headers) if verbose > 0: plot = tp.plot_timeseries(dataframe)[0] if display_images: plot.show() if save_images: plot.savefig("timeseries.png") parcorr = ParCorr(significance='analytic') pcmci = PCMCI(dataframe=dataframe, cond_ind_test=parcorr, verbosity=1) correlations = pcmci.get_lagged_dependencies(tau_max=3) lag_func_matrix = tp.plot_lagfuncs(val_matrix=correlations, setup_args={ 'var_names': headers, 'x_base': 5, 'y_base': .5 }) if verbose > 1: if display_images: lag_func_matrix.savefig() if save_images: lag_func_matrix.savefig("lag_func.png") pcmci.verbosity = 1 results = pcmci.run_pcmci(tau_max=tau_max, pc_alpha=None)
data.shape # In[5]: tp.plot_timeseries(dataframe) plt.show() # In[6]: parcorr = ParCorr(significance='analytic') pcmci = PCMCI(dataframe=dataframe, cond_ind_test=parcorr, verbosity=1) # In[7]: correlations = pcmci.get_lagged_dependencies(tau_max=20, val_only=True)['val_matrix'] lag_func_matrix = tp.plot_lagfuncs(val_matrix=correlations, setup_args={ 'figsize': (6, 6), 'var_names': var_names, 'x_base': 10, 'y_base': .5 }) # In[8]: pcmci.verbosity = 0 results = pcmci.run_pcmci(tau_max=8, pc_alpha=None) # In[9]: