def simulator(theta): N_samples = theta.shape[0] x = torch.zeros(N_samples, conj_model.N, dim) for i in range(N_samples): model_tmp = MultivariateNormal(theta[i], conj_model.model.covariance_matrix) x[i, :, :] = model_tmp.rsample(sample_shape=(conj_model.N, )) # return calc_summary_stats(x), theta #/math.sqrt(5) # div with std of prior to nomarlize data return func.calc_summary_stats(x)
x_test, theta_test = func.run_model_sim(N_prior_pred_test, seed + 2, conj_model, analytical_posterior, conj_model.model.covariance_matrix, dim, True) # Generate test data for obs data set print(conj_model.model_sim(theta_test).shape) N_test_obs_data = 1000 x_test_obs_data = torch.zeros(N_test_obs_data, 5) theta_test_obs_data = torch.zeros(N_test_obs_data, dim) for i in range(N_test_obs_data): x_test_obs_data[i, :] = func.calc_summary_stats(x_o) theta_test_obs_data[i, :] = conj_model.model.loc # Set up networks for the likelihood model # Base dist for posterior model flow_lik, flow_post = func.set_up_networks() hyper_params = [0.001, 0.002, 0.95, 0.7] # lr_like, lr_post, gamma_post, gamma if lambda_val > 0: hyper_params[-1] = lambda_val if hp_tuning >= 2: hyper_params = func.sample_hp("snpla", hp_tuning)
# Load all utility functions for all methods import mv_gaussian.low_dim_w_summary_stats.functions as func # Set model and generate data x_o, conj_model, analytical_posterior = func.set_up_model(seed) # sample data from posterior torch.manual_seed(seed) analytical_posterior_samples = conj_model.sample_analytical_posterior(analytical_posterior, 1000) np.savetxt('mv_gaussian/low_dim_w_summary_stats/data/post_samples_analytical_' + str(dim) + '_' + str(seed) + '_' + str(seed_data) + '.csv', analytical_posterior_samples.detach().numpy(), delimiter=",") np.savetxt('mv_gaussian/low_dim_w_summary_stats/data/x_o_summary_stats_true_' + str(dim) + '_' + str(seed) + '_' + str(seed_data) + '.csv', func.calc_summary_stats(x_o).detach().numpy(), delimiter=",") # Sample data sets for ground-truth model N_test_obs_data = 1000 x_test_obs_data = torch.zeros(N_test_obs_data, 5) theta_test_obs_data = torch.zeros(N_test_obs_data, dim) for i in range(N_test_obs_data): x_test_obs_data[i, :] = func.calc_summary_stats(conj_model.sample()) np.savetxt('mv_gaussian/low_dim_w_summary_stats/data/x_o_summary_stats_samples_true_' + str(dim) + '_' + str(seed) + '_' + str(seed_data) + '.csv', x_test_obs_data.detach().numpy(), delimiter=",") # Sample data from prior pred N_prior_pred_test = 1000
def simulator(theta): N_samples = theta.shape[0] x = torch.zeros(N_samples, conj_model.N, dim) for i in range(N_samples): model_tmp = MultivariateNormal(theta[i], conj_model.model.covariance_matrix) x[i, :, :] = model_tmp.rsample(sample_shape=(conj_model.N, )) # return calc_summary_stats(x), theta #/math.sqrt(5) # div with std of prior to nomarlize data return func.calc_summary_stats(x) s_x_o = func.calc_summary_stats(x_o) # check simulator and prior simulator, prior = prepare_for_sbi(simulator, conj_model.prior) # function that builds the network def build_custom_post_net(batch_theta, batch_x): flow_lik, flow_post = func.set_up_networks() return flow_post inference = SNRE_B(simulator, prior) learning_rate = 0.0005 # default value
N_prior_pred_test = 1000 x_test, theta_test = func.run_model_sim(N_prior_pred_test, seed + 2, conj_model, analytical_posterior, conj_model.model.covariance_matrix, dim, True) # Generate test data for obs data set print(conj_model.model_sim(theta_test).shape) N_test_obs_data = 1000 x_test_obs_data = torch.zeros(N_test_obs_data, 5) theta_test_obs_data = torch.zeros(N_test_obs_data, dim) for i in range(N_test_obs_data): x_test_obs_data[i, :] = func.calc_summary_stats(x_o) theta_test_obs_data[i, :] = conj_model.model.loc # Set up networks for the likelihood model # Base dist for posterior model flow_lik, flow_post = func.set_up_networks() optimizer_lik = torch.optim.Adam(flow_lik.parameters()) optimizer_post = torch.optim.Adam(flow_post.parameters(), lr=0.002) decay_rate_post = 0.95 # no adaptation of Adam's base rate nbr_rounds = 10 prob_prior_decay_rate = 0.7 prob_prior = spa.calc_prob_prior(nbr_rounds, prob_prior_decay_rate)