def test_smc(ma2): thresholds = [.5, .2] N = 1000 smc = elfi.SMC(ma2['d'], batch_size=20000) res = smc.sample(N, thresholds=thresholds) dens = smc._prior.logpdf(res.samples_array) # Test that the density is uniform assert np.allclose(dens, dens[0]) # Test that weighted mean is computed w = res.weights assert w is not None samples = res.samples_array means = res.sample_means_array assert np.allclose(means, np.average(samples, 0, w)) assert not np.allclose(means, np.mean(samples, 0)) # Test result summaries res.summary() res.summary(all=True) res.sample_means_summary() res.sample_means_summary(all=True) # Ensure prior pdf > 0 for samples assert np.all(exma2.CustomPrior1.pdf(samples[:, 0], 2) > 0) assert np.all(exma2.CustomPrior2.pdf(samples[:, 1], samples[:, 0], 1) > 0)
def test_progress_bar(ma2): thresholds = [.5, .2] N = 1000 rej = elfi.Rejection(ma2['d'], batch_size=20000) assert not rej.progress_bar.finished rej.sample(N) assert rej.progress_bar.finished smc = elfi.SMC(ma2['d'], batch_size=20000) assert not smc.progress_bar.finished smc.sample(N, thresholds=thresholds) assert smc.progress_bar.finished bolfi = elfi.BOLFI(ma2['d'], initial_evidence=10, update_interval=10, batch_size=5, bounds={ 't1': (-2, 2), 't2': (-1, 1) }) assert not bolfi.progress_bar.finished n = 20 bolfi.infer(n) assert bolfi.progress_bar.finished
def test_smc(ma2): bs = 3 n_samples = 10 thresholds = [1, .9, .8] smc = elfi.SMC(ma2, 'd', batch_size=bs) sample = smc.sample(n_samples, thresholds=thresholds) seed = smc.seed smc = elfi.SMC(ma2, 'd', batch_size=bs, seed=seed) sample_same = smc.sample(n_samples, thresholds=thresholds) smc = elfi.SMC(ma2, 'd', batch_size=bs) sample_diff = smc.sample(n_samples, thresholds=thresholds) check_consistent_sample(sample, sample_diff, sample_same)
def test_smc(): elfi.env.client(4, 1) t1_0 = .6 t2_0 = .2 N = 1000 itask = ma2.inference_task(500, true_params=[t1_0, t2_0]) smc = elfi.SMC(itask.discrepancy, itask.parameters, batch_size=10000) res = smc.sample(N, 3, schedule=[1, 0.5, 0.1]) assert not np.array_equal(res.samples_history[0][0], res.samples_history[1][0]) assert not np.array_equal(res.samples_history[-1][0], list(res.samples.values())[0]) assert not np.array_equal(res.weights_history[0], res.weights_history[1]) assert not np.array_equal(res.weights[-1], res.weights) assert not np.array_equal(res.weights_history[0], res.weights_history[1]) assert not np.array_equal(res.weights[-1], res.weights) s = list(res.samples.values()) w = res.weights assert len(s[0]) == N assert res.n_samples == N # Set somewhat loose intervals for now e = 0.1 assert np.abs(np.average(s[0], axis=0, weights=w) - t1_0) < e assert np.abs(np.average(s[1], axis=0, weights=w) - t2_0) < e elfi.env.client().shutdown()
def test_smc_prior_use(ma2): thresholds = [.5] N = 1000 smc = elfi.SMC(ma2['d'], batch_size=20000) res = smc.sample(N, thresholds=thresholds) dens = res.populations[0].outputs[smc.prior_logpdf] # Test that the density is uniform assert np.allclose(dens, dens[0])
def test_smc_with_quantiles(): m, true_params = setup_ma2_with_informative_data() quantiles = [.5, .5, .5] N = 1000 smc = elfi.SMC(m['d'], batch_size=20000) res = smc.sample(N, quantiles=quantiles) check_inference_with_informative_data(res.samples, N, true_params)
def test_threshold_evolution_in_smc(ma2): threshold_selection_quantiles = [0.5] * 5 N = 100 smc = elfi.SMC(ma2['d'], batch_size=100) res = smc.sample(N, quantiles=threshold_selection_quantiles) # Check that tolerance threshold decreased between iterations thresholds = smc.objective['thresholds'][1:] assert np.all(np.diff(thresholds) < 0)
def test_smc(): m, true_params = setup_ma2_with_informative_data() thresholds = [.5, .25, .1] N = 1000 smc = elfi.SMC(m['d'], batch_size=20000) res = smc.sample(N, thresholds=thresholds) check_inference_with_informative_data(res.samples, N, true_params) # We should be able to carry out the inference in less than six batches assert res.populations[-1].n_batches < 6
def predict(self, data_test): elfi.new_model("SMC") prior = elfi.Prior(MVUniform, self.p_lower, self.p_upper) sim = elfi.Simulator(self.simulator, prior, observed=data_test, name='sim') SS = elfi.Summary(self.identity, sim, name='identity') d = elfi.Distance('euclidean', SS, name='d') smc = elfi.SMC(d, batch_size=1, seed=42) samples = smc.sample(self.n_particles, [self.threshold]) return samples.samples_array
def test_sample_object_to_dict(): data_rej = OrderedDict() data_smc = OrderedDict() m = get_model(n_obs=100, true_params=[.6, .2]) batch_size, n = 1, 2 schedule = [0.7, 0.2, 0.05] rej = elfi.Rejection(m['d'], batch_size=batch_size) res_rej = rej.sample(n, threshold=0.1) smc = elfi.SMC(m['d'], batch_size=batch_size) res_smc = smc.sample(n, schedule) sample_object_to_dict(data_rej, res_rej) sample_object_to_dict(data_smc, res_smc, skip='populations') assert any(x not in data_rej for x in ['meta', 'output']) is True assert any(x not in data_smc for x in ['meta', 'output', 'populations']) is True
def test_smc_consistency(): elfi.env.client(4, 1) t1_0 = .6 t2_0 = .2 N = 10 samples = None for i in range(3): itask = ma2.inference_task(500, true_params=[t1_0, t2_0]) smc = elfi.SMC(itask.discrepancy, itask.parameters, batch_size=1) res = smc.sample(N, 2, schedule=[100, 99]) si = list(res.samples.values())[0] if samples is None: samples = si else: assert np.array_equal(samples, si)
def test_SMC(self): self.set_simple_model() n = 20 batch_size = 10 smc = elfi.SMC(self.d, [self.p], batch_size=batch_size) n_populations = 3 schedule = [0.5] * n_populations prior_id = id(self.p) result = smc.sample(n, n_populations, schedule) assert id(self.p) == prior_id # changed within SMC, finally reverted assert self.mock_sim_calls == int(n / schedule[0] * n_populations) assert self.mock_sum_calls == int(n / schedule[0] * n_populations) + 1 assert self.mock_dis_calls == int(n / schedule[0] * n_populations)
def test_smc_special_cases(): """ Cases ----- - batches with no accepts. - division by zero in computing SMC proposal weighted covariance """ elfi.env.client(4, 1) t1_0 = .6 t2_0 = .2 N = 1 batch_size = 10 itask = ma2.inference_task(500, true_params=[t1_0, t2_0]) smc = elfi.SMC(itask.discrepancy, itask.parameters, batch_size=batch_size) s = smc.sample(N, 2, schedule=[1, .5]) # Ensure that there was an empty batch by checking that more than one # batch was computed and that there were less accepted than batches assert s.n_sim > batch_size n_batches = s.n_sim / batch_size assert s.n_sim * s.accept_rate < n_batches
def test_smc(ma2): thresholds = [.5, .2] N = 1000 smc = elfi.SMC(ma2['d'], batch_size=20000) res = smc.sample(N, thresholds=thresholds) dens = res.populations[0].outputs[smc.prior_logpdf] # Test that the density is uniform assert np.allclose(dens, dens[0]) # Test that weighted mean is computed w = res.weights assert w is not None samples = res.samples_array means = res.sample_means_array assert np.allclose(means, np.average(samples, 0, w)) assert not np.allclose(means, np.mean(samples, 0)) # Test result summaries res.summary() res.summary(all=True) res.sample_means_summary() res.sample_means_summary(all=True)
distl = [] for ggr in generated_graph: distl.append(eucMultiArgs(ggr, gr_obs)[0]) print("Schedule thresholds: ", np.percentile(np.array(distl), schedule_per)) print("***********************************") print("***********************************") final_activation = [] generated_graph = [] schedule = list(np.percentile(np.array(distl), schedule_per)) # print("observation matrix shape after transformation: ", gr_obs.shape[1]) smc = elfi.SMC(d, batch_size=1) result = smc.sample(N, schedule) print(result.summary(all=True)) print(count_esobs, min(all_i), max(all_i)) print( "******************************************************************" ) count_samp = pd.to_numeric(pd.Series(list( result.samples['prop_prob']))) count_samples = len(count_samp) ltp_count = len(count_samp[count_samp >= 0.5]) lta_count = len(count_samp[count_samp < 0.5]) ltp_lta = len(count_samp[(count_samp < 0.5) | (count_samp >= 0.5)])
def stickiness(actions, lag=1): return np.mean(np.diff(actions, n=lag, axis=1) == 0, axis=1) def average_reward(actions, action_payoff_mat=None): # average across all blocks return np.mean(action_payoff_mat[np.arange(len(action_payoff_mat)), actions], axis=1) / 100 stickiness(best_action[np.newaxis, :]) # probability of staying on the current action (regardless of reward) # at lags of 1 and 2 S1 = elfi.Summary(stickiness, sim, 1) S2 = elfi.Summary(stickiness, sim, 2) S3 = elfi.Summary(average_reward, sim, action_payoff_mat) d = elfi.Distance('euclidean', S1, S2, S3) # elfi.draw(d).view() elfi.set_client('multiprocessing') elfi.set_client(elfi.clients.multiprocessing.Client(num_processes=3)) smc = elfi.SMC(d, batch_size=10000, seed=1) res = smc.sample(100, [0.7, 0.2, 0.01]) # rej = elfi.Rejection(d, batch_size=100, seed=1) # res = rej.sample(1000, quantile=0.01) # , vis=dict(xlim=[-1, 1], ylim=[-1, 1])) print(res.summary()) res.plot_pairs() plt.show()
# Rejection to "train" sum stat scaler start_time = time.time() pool = elfi.OutputPool(['sum']) rej = elfi.Rejection(m['d'], batch_size=1, seed=1, pool=pool) rej.sample(train_scaler_n_sim, quantile=1, bar=False) # Accept all sum_stats = pool.get_store('sum') sum_stats = np.array(list(sum_stats.values())) sum_stats = sum_stats.reshape(-1, sum_stats.shape[2]) # Drop batches axis scaler = StandardScaler() scaler.fit(sum_stats) elapsed_time = time.time() - start_time logging.info( f"{train_scaler_n_sim} simulations to train standard scaler completed in {elapsed_time/60:.2f} minutes" ) m["sum_scaler"].become(elfi.Summary(elfi_sum_scaler, m["sum"], scaler, model=m)) # Now carries out scaling # Run SMC start_time = time.time() smc = elfi.SMC(m['d'], batch_size=1, seed=42) smc_res = smc.sample(smc_n_samples, smc_schedule, bar=False) elapsed_time = time.time() - start_time logging.info(f"SMC completed at {elapsed_time/60:.2f} minutes.") smc_res.save("../output/smc_posterior.pkl") # Save results logging.info(f"Shutting down cluster") c = elfi.client.get_client().ipp_client c.shutdown(hub=True)
def Perfrom_ABC(self,save = False): sigma_prior = elfi.Prior('uniform',self.start_prior,self.end_prior) y0 = self.simulator(self.sigma0) sim = elfi.Simulator(self.simulator,sigma_prior,observed=y0) S1 = elfi.Summary(self.var,sim) S2 = elfi.Summary(self.max_min_diff,sim) S3 = elfi.Summary(self.PDP_diff_mean,sim) S4 = elfi.Summary(self.PDP_diff_var,sim) S5 = elfi.Summary(self.mean,sim) sumstats = [] sumstats.append(S1) sumstats.append(S2) sumstats.append(S3) sumstats.append(S4) sumstats.append(S5) output_names = 'S1,S2,S3,S4,S5' output_list = ['S1','S2','S3','S4','S5'] if self.distance == 'euclidean': d = elfi.Distance('euclidean',*sumstats) elif self.distance == 'seuclidean': d = elfi.Distance('seuclidean',*sumstats,V = None) if self.mode == 'SMC': rej_temp = elfi.Rejection(d.model['d'],output_names = output_list) rej = elfi.SMC(d.model['d'],output_names = rej_temp.output_names) quantile_list = list(np.repeat(self.quantile,self.T)) res_sample = rej.sample(self.N,quantile_list) try: adj_res = elfi.adjust_posterior(res_sample.T, d.model, output_list) except: print('Error in calculation of adjustment') adj_res = 'NaN' if self.mode == 'Rejection': rej = elfi.Rejection(d.model['d'],output_names = output_list) res_sample = rej.sample(self.N,self.quantile) try: adj_res = elfi.adjust_posterior(res_sample.T, d.model, output_list) except: print('Error in calculation of adjustment') adj_res = 'NaN' dat = res_sample.samples_array[:,-1] dat_grid = np.linspace(self.start_prior,self.end_prior,self.N) kde = kde_scipy(dat,dat_grid,bandwidth = 1) self.data_dict = {'theta':res_sample.samples_array[:,-1],\ 'summariers':np.array([res_sample.outputs['S1'],res_sample.outputs['S2'],\ res_sample.outputs['S3'],res_sample.outputs['S4'],res_sample.outputs['S5']]),\ 'True sim': y0, 'true summaries' : np.array([self.var(y0),\ self.max_min_diff(y0),self.PDP_diff_mean(y0),self.PDP_diff_var(y0),self.mean(y0),]),\ 'kde' : kde, 'kde_grid':dat_grid,'sigma0': self.sigma0,\ 'elfi adjust' : adj_res,\ 'elfi results' : res_sample} if save == True: try: string = str(self.quantile)[-1] f = open('examples/Pickle_data_%s_q_%s_sig0_%d_summaries_%s_distance_%s'%(self.mode,string,int(self.sigma0),output_names,self.distance),'wb') p.dump(self.data_dict,f) f.close() except: print('Error in saving') return self.data_dict