def test_ges_vs_lr(loop_num=1, n=30, p=7, c=4): # update_pri_params() lr_loss_list = [] ges_loss_list = [] count = 0 for i in tqdm(range(loop_num)): lr_ate = None ges_ate = None while ges_ate is None or lr_ate is None: count += 1 dag_edge = random_dag(p, c) pri_dict = set_pri_params(dag_edge) params = generate_params(pri_dict) x_do = 5.0 print("------Intervention------\nX = {}".format(str(x_do))) true_ate = calculate_true_ate(dag_edge, params, x_do) df = set_dataframe(n, dag_edge, params, write_bool=False) lr_ate = simple_linear_reg(df=df) ges_ate = ges_method(df=df) lr_loss = squared_loss(predicted_ate=lr_ate, true_ate=true_ate) ges_loss = squared_loss(predicted_ate=ges_ate, true_ate=true_ate) lr_loss_list.append(lr_loss) ges_loss_list.append(ges_loss) lr_res = np.mean(lr_loss_list) ges_res = np.mean(ges_loss_list) print("*****MSE Of LinearReg*****\n{}".format(lr_res)) print("*****MSE Of GES*****\n{}".format(ges_res)) print(count) return [lr_res, ges_res]
def test_rep_kind(loop_num=100, n=10, p=5, c=4, rep_num=10): logger.debug("loop_num={}, n={}, p={}, c={}, rep_num={}".format(loop_num, n, p, c, rep_num)) lr_loss_list = [] ges_loss_list = [] bayes_loss_list = {} for rep_kind in [1, 2, 3]: bayes_loss_list[rep_kind] = [] for i in tqdm(range(loop_num)): lr_ate = None ges_ate = None bayes_ate = {} for rep_kind in [1, 2, 3]: bayes_ate[rep_kind] = None while all([v is None for v in bayes_ate.values()]) or ges_ate is None or lr_ate is None: dag_edge = random_dag(p, c) pri_dict = set_pri_params(dag_edge) params = generate_params(pri_dict) x_do = 5.0 print("------Intervention------\nX = {}".format(str(x_do))) true_ate = calculate_true_ate(dag_edge, params, x_do) df = set_dataframe(n, dag_edge, params, write_bool=False) lr_ate = simple_linear_reg(df=df) ges_ate = ges_method(df=df) for rep_kind in tqdm([1, 2, 3]): bayes_ate[rep_kind] = bayes_method(df=df, c=c, rep_num=rep_num, rep_kind=rep_kind) lr_loss_list.append(squared_loss(true_ate=true_ate, predicted_ate=lr_ate)) ges_loss_list.append(squared_loss(true_ate=true_ate, predicted_ate=ges_ate)) for rep_kind in [1, 2, 3]: bayes_loss_list[rep_kind].append(squared_loss(true_ate=true_ate, predicted_ate=bayes_ate[rep_kind])) row = [np.mean(lr_loss_list), np.mean(ges_loss_list)] colomn = ['MSE of LinearReg', 'MSE of GES'] for rep_kind in [1, 2, 3]: row.append(np.mean(bayes_loss_list[rep_kind])) colomn.append('MSE of Bayes on rep_kind={}'.format(rep_kind)) df_res = pd.DataFrame([row], columns=colomn) # Write to csv file dt_now = datetime.datetime.now() sheet = dt_now.strftime('%Y-%m-%d-%H-%M-%S') path = "./data_files/loss_result_rep_kind.xlsx" if os.path.exists(path): with pd.ExcelWriter(path, engine="openpyxl", mode="a") as writer: df_res.to_excel(writer, sheet_name=sheet, index=False) else: with pd.ExcelWriter(path, engine="openpyxl") as writer: df_res.to_excel(writer, sheet_name=sheet, index=False) print("DONE!!\nWrite in the sheet={} of data_files/loss_result_rep_kind.xlsx".format(sheet))
def test(loop_num=5, n=10, p=7, c=4, rep_num=4, rep_kind=1, update_param=False, update_epsilon=False): if update_param: update_pri_params() if update_epsilon: update_epsilon_dict() lr_loss_list = [] ges_loss_list = [] bayes_loss_list = [] for i in tqdm(range(loop_num)): lr_ate = None bayes_ate = None ges_ate = None while bayes_ate is None or ges_ate is None or lr_ate is None: dag_edge = random_dag(p, c) pri_dict = set_pri_params(dag_edge) params = generate_params(pri_dict) x_do = 5.0 print("------Intervention------\nX = {}".format(str(x_do))) true_ate = calculate_true_ate(dag_edge, params, x_do) df = set_dataframe(n, dag_edge, params, write_bool=False) lr_ate = simple_linear_reg(df=df) ges_ate = ges_method(df=df) if ges_ate is None: continue bayes_ate = bayes_method(df=df, c=c, rep_num=rep_num, rep_kind=rep_kind) lr_loss = squared_loss(predicted_ate=lr_ate, true_ate=true_ate) ges_loss = squared_loss(predicted_ate=ges_ate, true_ate=true_ate) bayes_loss = squared_loss(true_ate=true_ate, predicted_ate=bayes_ate) # if lr_loss + 50 < bayes_loss: # continue lr_loss_list.append(lr_loss) ges_loss_list.append(ges_loss) bayes_loss_list.append(bayes_loss) print("******MSE******\n{}, {}, {}".format(np.mean(lr_loss_list), np.mean(ges_loss_list), np.mean(bayes_loss_list))) lr_res = np.mean(lr_loss_list) lr_std = np.std(lr_loss_list) ges_res = np.mean(ges_loss_list) ges_std = np.std(ges_loss_list) bayes_res = np.mean(bayes_loss_list) bayes_std = np.std(bayes_loss_list) print("*****MSE Of LinearReg*****\n{}".format(lr_res)) print("*****MSE Of GES*****\n{}".format(ges_res)) print("*****MSE Of Bayes*****\n{}".format(bayes_res)) logger.debug("loop_num={}, n={}, p={}, c={}, rep_num={}, rep_kind={}".format(loop_num, n, p, c, rep_num, rep_kind)) logger.debug("MSE Of LinearReg={}".format(lr_res)) logger.debug("STD of LinearReg={}".format(lr_std)) logger.debug("MSE Of GES={}".format(ges_res)) logger.debug("STD of GES={}".format(ges_std)) logger.debug("MSE Of Bayes={}".format(bayes_res)) logger.debug("STD of Bayes={}".format(bayes_std)) rows = [] for lr, ges, bayes in zip(lr_loss_list, ges_loss_list, bayes_loss_list): rows.append([lr, ges, bayes]) write_excel(rows=rows, col_name=['lr', 'GES', 'Bayes'], n=n) return [lr_res, ges_res, bayes_res, lr_std, ges_std, bayes_std]