def get_probs_copula_props(probs, probs_rolled, n_ecop_bins): scorr = np.corrcoef(probs, probs_rolled)[0, 1] asymm_1, asymm_2 = get_asymms_sample(probs, probs_rolled) asymm_1 /= get_asymm_1_max(scorr) asymm_2 /= get_asymm_2_max(scorr) # plt.scatter(probs, probs_rolled, alpha=0.5) # plt.grid() # plt.show() # plt.close() ecop_dens_arr = np.full((n_ecop_bins, n_ecop_bins), np.nan, dtype=np.float64) fill_bi_var_cop_dens(probs, probs_rolled, ecop_dens_arr) non_zero_idxs = ecop_dens_arr > 0 dens = ecop_dens_arr[non_zero_idxs] etpy_arr = -(dens * np.log(dens)) etpy = etpy_arr.sum() etpy_min = get_etpy_min(n_ecop_bins) etpy_max = get_etpy_max(n_ecop_bins) etpy = (etpy - etpy_min) / (etpy_max - etpy_min) return asymm_1, asymm_2, etpy, scorr
def main(): main_dir = Path(r'P:\Synchronize\IWS\Testings\fourtrans_practice\phsann') os.chdir(main_dir) in_file = Path(r'neckar_norm_cop_infill_discharge_1961_2015_20190118.csv') lag_steps = np.arange(1, 3, dtype=np.int64) ecop_bins = 20 fig_size = (15, 10) plt_alpha = 0.5 in_ser = get_data_df(in_file) idx_labs = np.unique(in_ser.index.year) etpy_min = _get_etpy_min(ecop_bins) etpy_max = _get_etpy_max(ecop_bins) ecop_dens_arrs = np.full((ecop_bins, ecop_bins), np.nan, dtype=np.float64) axes = plt.subplots(2, 3, squeeze=False, figsize=fig_size)[1] cmap = 'jet' sim_clrs = plt.get_cmap(cmap)( (idx_labs - idx_labs.min()) / (idx_labs.max() - idx_labs.min())) sim_clrs = { idx_lab: sim_clr for (idx_lab, sim_clr) in zip(idx_labs, sim_clrs) } cmap_mappable_beta = plt.cm.ScalarMappable(cmap=cmap) cmap_mappable_beta.set_array([]) for idx_lab in idx_labs: data = in_ser.loc[f'{idx_lab}-01-01':f'{idx_lab}-12-31'].values probs = rankdata(data) / (data.size + 1.0) scorrs = [] asymms_1 = [] asymms_2 = [] etpys = [] pcorrs = [] for lag_step in lag_steps: probs_i, rolled_probs_i = roll_real_2arrs(probs, probs, lag_step) data_i, rolled_data_i = roll_real_2arrs(data, data, lag_step) # scorr. scorr = np.corrcoef(probs_i, rolled_probs_i)[0, 1] scorrs.append(scorr) # asymms. asymm_1, asymm_2 = get_asymms_sample(probs_i, rolled_probs_i) asymm_1 /= _get_asymm_1_max(scorr) asymm_2 /= _get_asymm_2_max(scorr) asymms_1.append(asymm_1) asymms_2.append(asymm_2) # ecop etpy. fill_bi_var_cop_dens(probs_i, rolled_probs_i, ecop_dens_arrs) non_zero_idxs = ecop_dens_arrs > 0 dens = ecop_dens_arrs[non_zero_idxs] etpy_arr = -(dens * np.log(dens)) etpy = etpy_arr.sum() etpy = (etpy - etpy_min) / (etpy_max - etpy_min) etpys.append(etpy) # pcorr. pcorr = np.corrcoef(data_i, rolled_data_i)[0, 1] pcorrs.append(pcorr) # plot axes[0, 0].plot(lag_steps, scorrs, alpha=plt_alpha, color=sim_clrs[idx_lab], label=idx_lab) axes[1, 0].plot(lag_steps, asymms_1, alpha=plt_alpha, color=sim_clrs[idx_lab]) axes[1, 1].plot(lag_steps, asymms_2, alpha=plt_alpha, color=sim_clrs[idx_lab]) axes[0, 1].plot(lag_steps, etpys, alpha=plt_alpha, color=sim_clrs[idx_lab]) axes[0, 2].plot(lag_steps, pcorrs, alpha=plt_alpha, color=sim_clrs[idx_lab]) axes[1, 2].plot(data, alpha=plt_alpha, color=sim_clrs[idx_lab]) axes[0, 0].grid() axes[1, 0].grid() axes[1, 1].grid() axes[0, 1].grid() axes[0, 2].grid() axes[1, 2].grid() axes[0, 0].legend() axes[0, 0].set_ylabel('Spearman correlation') axes[1, 0].set_xlabel('Lag steps') axes[1, 0].set_ylabel('Asymmetry (Type - 1)') axes[1, 1].set_xlabel('Lag steps') axes[1, 1].set_ylabel('Asymmetry (Type - 2)') axes[0, 1].set_ylabel('Entropy') axes[0, 2].set_xlabel('Lag steps') axes[0, 2].set_ylabel('Pearson correlation') # axes[1, 2].set_xlabel('Nth orders') # axes[1, 2].set_ylabel('Dist. Sum') # cbaxes = fig.add_axes([0.2, 0.0, 0.65, 0.05]) # plt.colorbar( # mappable=cmap_mappable_beta, # cax=axes[1, 2], # orientation='horizontal', # label='Relative Timing', # alpha=plt_alpha, # drawedges=False) plt.show() return
def main(): main_dir = Path(r'P:\Synchronize\IWS\Testings\fourtrans_practice\phsann') os.chdir(main_dir) in_file = Path(r'neckar_norm_cop_infill_discharge_1961_2015_20190118.csv') lag_steps = np.arange(1, 5, dtype=np.int64) ecop_bins = 20 fig_size = (15, 10) plt_alpha = 0.5 in_ser = pd.read_csv(in_file, sep=';', index_col=0)['420'] in_ser.index = pd.to_datetime(in_ser.index, format='%Y-%m-%d') idx_labs = np.unique(in_ser.index.year) ecop_dens_arr = np.full((ecop_bins, ecop_bins), np.nan, dtype=np.float64) lag_axes = { lag_step: plt.subplots(2, 3, squeeze=False, figsize=fig_size) for lag_step in lag_steps } cmap = 'jet' sim_clrs = plt.get_cmap(cmap)( (idx_labs - idx_labs.min()) / (idx_labs.max() - idx_labs.min())) sim_clrs = { idx_lab: sim_clr for (idx_lab, sim_clr) in zip(idx_labs, sim_clrs) } cmap_mappable_beta = plt.cm.ScalarMappable(cmap=cmap) cmap_mappable_beta.set_array([]) for idx_lab in idx_labs: data = in_ser.loc[f'{idx_lab}-01-01':f'{idx_lab+5}-12-31'].values probs = rankdata(data) / (data.size + 1.0) # scorrs = [] # asymms_1 = [] # asymms_2 = [] # etpys = [] # pcorrs = [] # cdf_valss = [] # etpy_cdf_valss = [] for lag_step in lag_steps: probs_i, rolled_probs_i = roll_real_2arrs(probs, probs, lag_step) data_i, rolled_data_i = roll_real_2arrs(data, data, lag_step) # scorr. scorrs = np.sort(rolled_probs_i * probs_i) # asymms. asymms_1 = np.sort((probs_i + rolled_probs_i - 1.0)**3) asymms_2 = np.sort((probs_i - rolled_probs_i)**3) # ecop etpy. fill_bi_var_cop_dens(probs_i, rolled_probs_i, ecop_dens_arr) non_zero_idxs = ecop_dens_arr > 0 dens = ecop_dens_arr[non_zero_idxs] etpy_arr = -(dens * np.log(dens)) etpys = np.sort(etpy_arr) # pcorr. pcorrs = np.sort((rolled_data_i - data_i)) # cdf vals. cdf_vals = np.arange(1.0, data_i.size + 1) cdf_vals /= cdf_vals.size + 1.0 # etpy cdf_vals etpy_cdf_vals = np.arange(1.0, etpy_arr.size + 1) etpy_cdf_vals /= etpy_cdf_vals.size + 1.0 # plot plt.figure(lag_axes[lag_step][0].number) lag_axes[lag_step][1][0, 0].plot(scorrs, cdf_vals, alpha=plt_alpha, color=sim_clrs[idx_lab]) lag_axes[lag_step][1][1, 0].plot(asymms_1, cdf_vals, alpha=plt_alpha, color=sim_clrs[idx_lab]) lag_axes[lag_step][1][1, 1].plot(asymms_2, cdf_vals, alpha=plt_alpha, color=sim_clrs[idx_lab]) lag_axes[lag_step][1][0, 1].plot(etpys, etpy_cdf_vals, alpha=plt_alpha, color=sim_clrs[idx_lab]) lag_axes[lag_step][1][0, 2].plot(pcorrs, cdf_vals, alpha=plt_alpha, color=sim_clrs[idx_lab]) lag_axes[lag_step][1][0, 0].grid() lag_axes[lag_step][1][1, 0].grid() lag_axes[lag_step][1][1, 1].grid() lag_axes[lag_step][1][0, 1].grid() lag_axes[lag_step][1][0, 2].grid() lag_axes[lag_step][1][1, 2].grid() lag_axes[lag_step][1][0, 0].set_ylabel('Spearman correlation') lag_axes[lag_step][1][1, 0].set_xlabel('Lag steps') lag_axes[lag_step][1][1, 0].set_ylabel('Asymmetry (Type - 1)') lag_axes[lag_step][1][1, 1].set_xlabel('Lag steps') lag_axes[lag_step][1][1, 1].set_ylabel('Asymmetry (Type - 2)') lag_axes[lag_step][1][0, 1].set_ylabel('Entropy') lag_axes[lag_step][1][0, 2].set_xlabel('Lag steps') lag_axes[lag_step][1][0, 2].set_ylabel('Pearson correlation') # lag_axes[lag_step][1][1, 2].set_xlabel('Nth orders') # lag_axes[lag_step][1][1, 2].set_ylabel('Dist. Sum') plt.colorbar(mappable=cmap_mappable_beta, cax=lag_axes[lag_step][1][1, 2], orientation='horizontal', label='Relative Timing', alpha=plt_alpha, drawedges=False) lag_axes[lag_step][0].suptitle(f'lag_step:{lag_step}') for lag_step in lag_steps: plt.figure(lag_axes[lag_step][0].number) plt.show(block=True) print(lag_step) return