def test_sample(self): # use the SABC scheme for T = 1 steps, epsilon, n_samples, n_samples_per_param = 1, 10, 10, 1 sampler = SABC([self.model], [self.dist_calc], self.backend, seed = 1) journal = sampler.sample([self.observation], steps, epsilon, n_samples, n_samples_per_param) mu_post_sample, sigma_post_sample, post_weights = np.array(journal.get_parameters()['mu']), np.array(journal.get_parameters()['sigma']), np.array(journal.get_weights()) # Compute posterior mean mu_post_mean, sigma_post_mean = np.average(mu_post_sample, weights=post_weights, axis=0), np.average(sigma_post_sample, weights=post_weights, axis=0) # test shape of sample mu_sample_shape, sigma_sample_shape, weights_sample_shape = np.shape(mu_post_sample), np.shape(mu_post_sample), np.shape(post_weights) self.assertEqual(mu_sample_shape, (10,1)) self.assertEqual(sigma_sample_shape, (10,1)) self.assertEqual(weights_sample_shape, (10,1)) # use the SABC scheme for T = 2 steps, epsilon, n_samples, n_samples_per_param = 2, 10, 10, 1 sampler = SABC([self.model], [self.dist_calc], self.backend, seed = 1) journal = sampler.sample([self.observation], steps, epsilon, n_samples, n_samples_per_param) mu_post_sample, sigma_post_sample, post_weights = np.array(journal.get_parameters()['mu']), np.array(journal.get_parameters()['sigma']), np.array(journal.get_weights()) # Compute posterior mean mu_post_mean, sigma_post_mean = np.average(mu_post_sample, weights=post_weights, axis=0), np.average(sigma_post_sample, weights=post_weights, axis=0) # test shape of sample mu_sample_shape, sigma_sample_shape, weights_sample_shape = np.shape(mu_post_sample), np.shape(mu_post_sample), np.shape(post_weights) self.assertEqual(mu_sample_shape, (10,1)) self.assertEqual(sigma_sample_shape, (10,1)) self.assertEqual(weights_sample_shape, (10,1)) self.assertLess(mu_post_mean - 0.55859197, 10e-2) self.assertLess(sigma_post_mean - 7.03987723, 10e-2) self.assertFalse(journal.number_of_simulations == 0)
def test_sample(self): # use the SABC scheme for T = 1 steps, epsilon, n_samples, n_samples_per_param = 1, .1, 10, 1 sampler = SABC(self.model, self.dist_calc, self.kernel, self.backend, seed = 1) journal = sampler.sample(self.observation, steps, epsilon, n_samples, n_samples_per_param) samples = (journal.get_parameters(), journal.get_weights()) # Compute posterior mean mu_post_sample, sigma_post_sample, post_weights = np.asarray(samples[0][:,0]), np.asarray(samples[0][:,1]), np.asarray(samples[1][:,0]) mu_post_mean, sigma_post_mean = np.average(mu_post_sample, weights = post_weights), np.average(sigma_post_sample, weights = post_weights) # test shape of sample mu_sample_shape, sigma_sample_shape, weights_sample_shape = np.shape(mu_post_sample), np.shape(mu_post_sample), np.shape(post_weights) self.assertEqual(mu_sample_shape, (10,)) self.assertEqual(sigma_sample_shape, (10,)) self.assertEqual(weights_sample_shape, (10,)) # use the SABC scheme for T = 2 steps, epsilon, n_samples, n_samples_per_param = 2, .1, 10, 1 sampler = SABC(self.model, self.dist_calc, self.kernel, self.backend, seed = 1) journal = sampler.sample(self.observation, steps, epsilon, n_samples, n_samples_per_param) samples = (journal.get_parameters(), journal.get_weights()) # Compute posterior mean mu_post_sample, sigma_post_sample, post_weights = np.asarray(samples[0][:,0]), np.asarray(samples[0][:,1]), np.asarray(samples[1][:,0]) mu_post_mean, sigma_post_mean = np.average(mu_post_sample, weights = post_weights), np.average(sigma_post_sample, weights = post_weights) # test shape of sample mu_sample_shape, sigma_sample_shape, weights_sample_shape = np.shape(mu_post_sample), np.shape(mu_post_sample), np.shape(post_weights) self.assertEqual(mu_sample_shape, (10,)) self.assertEqual(sigma_sample_shape, (10,)) self.assertEqual(weights_sample_shape, (10,)) self.assertLess(mu_post_mean - 1.51315443746, 10e-2) self.assertLess(sigma_post_mean - 6.85230360302, 10e-2)
def infer_parameters_sabc(): # define observation for true parameters mean=170, 65 rng = np.random.RandomState() y_obs = [ np.array( rng.multivariate_normal([170, 65], np.eye(2), 1).reshape(2, )) ] # define prior from abcpy.continuousmodels import Uniform mu0 = Uniform([[150], [200]], ) mu1 = Uniform([[25], [100]], ) # define the model height_weight_model = NestedBivariateGaussian([mu0, mu1]) # define statistics from abcpy.statistics import Identity statistics_calculator = Identity(degree=2, cross=False) # define distance from abcpy.distances import Euclidean distance_calculator = Euclidean(statistics_calculator) # define sampling scheme from abcpy.inferences import SABC sampler = SABC([height_weight_model], [distance_calculator], backend, seed=1) print('sampling') steps, epsilon, n_samples, n_samples_per_param, beta, delta, v = 2, np.array( [40000]), 10, 1, 2, 0.2, 0.3 ar_cutoff, resample, n_update, adaptcov, full_output = 0.1, None, None, 1, 1 # # # print('SABC Inferring') journal = sampler.sample([y_obs], steps, epsilon, n_samples, n_samples_per_param, beta, delta, v, ar_cutoff, resample, n_update, adaptcov, full_output) return journal
def infer_parameters(model_num, synthetic, T, n_sample, ar_cutoff): y_obs = [0] #prior = Uniform([[0, 0, 0, 0], [5, 5, 2.5, 2.5]]) prior1 = Uniform([[0], [5]]) prior2 = Uniform([[0], [5]]) prior3 = Uniform([[0], [2.5]]) prior4 = Uniform([[0], [2.5]]) model = Airport([prior1, prior2, prior3, prior4], name="Airport", model_num=model_num, synthetic=synthetic) from abcpy.statistics import Identity statistics_calculator = Identity(degree=1, cross=False) from abcpy.distances import Euclidean distance_calculator = Euclidean(statistics_calculator) # define sampling scheme from abcpy.inferences import SABC sampler = SABC([model], [distance_calculator], backend) # define sampling scheme #from abcpy.inferences import PMCABC #sampler = PMCABC([model], [distance_calculator], backend, kernel, seed=1) # sample from scheme journal = sampler.sample([y_obs], T, 1000, n_sample, 1, ar_cutoff=ar_cutoff) return journal
import numpy as np from abcpy.continuousmodels import Uniform from Model import AshDispersal u0 = Uniform([[100], [300]], name='u0') l0 = Uniform([[30], [100]], name='l0') AD = AshDispersal([u0, l0], name='AD') from abcpy.backends import BackendDummy as Backend backend = Backend(process_per_model=2) from abcpy.perturbationkernel import DefaultKernel kernel = DefaultKernel([u0, l0]) from Distance import DepositionDistance distance_calculator = DepositionDistance() print(distance_calculator.distance("test.h5", "test.h5")) from abcpy.inferences import SABC sampler = SABC([AD], [distance_calculator], backend, kernel, seed=1) #steps, epsilon, n_samples, n_samples_per_param, beta, delta, v, ar_cutoff, resample, n_update, adaptcov, full_output = 3, [50], 50, 1, 2, 0.2, 0.3, 0.1, None, None, 1, 1 steps, epsilon, n_samples, n_samples_per_param, beta, delta, v, ar_cutoff, resample, n_update, adaptcov, full_output = 3, [ 1 ], 1, 1, 2, 0.2, 0.3, 0.1, None, None, 1, 1 print('SABC Inferring') journal_sabc = sampler.sample("test.h5", steps, epsilon, n_samples, n_samples_per_param, beta, delta, v, ar_cutoff, resample, n_update, adaptcov, full_output) print('SABC done')
# # Define kernel and join the defined kernels from abcpy.perturbationkernel import DefaultKernel kernel = DefaultKernel([pAd, pAg, pT, pF, aT, v_z_AP, v_z_NAP]) # Define Distance functions from abcpy.distances import Euclidean from statistic import Multiply L = np.load('Data/L_all_3_cross.npz')['L'] stat_mult = Multiply(L=L, degree=3, cross=True) dist_calc_mult = Euclidean(stat_mult) # SABC - Multiply## from abcpy.inferences import SABC print('Inference using Classifier Loss') sampler = SABC([PD], [dist_calc_mult], backend, kernel, seed=1) steps, epsilon, n_samples, n_samples_per_param, ar_cutoff, full_output, journal_file = 20, 10e20, 511, 1, 0.001, 1, None print('SABC Inferring') fakedata = PD.forward_simulate([noAP, noNAP, SR_x, 89.0, 76.0, 2.49, 7e-3, 7.7, 6e-3, 8e-4], k=1) print(fakedata) journal_sabc = sampler.sample(observations=[fakedata], steps=steps, epsilon=epsilon, n_samples=n_samples, n_samples_per_param=n_samples_per_param, beta=2, delta=0.2, v=0.3, ar_cutoff=0.001, resample=None, n_update=None, full_output=1, journal_file=journal_file) print(journal_sabc.posterior_mean()) journal_sabc.save('Data/Journals/sabc_obs_fake_'+str(ind)+'_multiply.jrnl') if compute_MLE:
print('Something may be wrong with the distance!') ############################################################################### # APMCABC # ############################################################################### if abc_method=='apmcabc': from abcpy.inferences import APMCABC sampler = APMCABC([ff], [distance_calculator], backend, kernel, seed = 1) steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file = 4, 1000, 1, 0.1, 0.03, 2, 1.0, None print('APMCABC Inferring') # We use resultfakeobs1 as our observed dataset journal_apmcabc = sampler.sample([resultfakeobs1], steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file) print(journal_apmcabc.posterior_mean()) journal_apmcabc.save('apmcabc_' + sim_model + '_' + exp_dataset + '.jrnl') ############################################################################### # SABC # ############################################################################### if abc_method=='sabc': from abcpy.inferences import SABC sampler = SABC([ff], [distance_calculator], backend, kernel, seed = 1) steps, epsilon, n_samples, n_samples_per_param, beta, delta, v, ar_cutoff, resample, n_update, adaptcov, full_output, journal_file = 2, 40, 1000, 1, 2, 0.2, 0.3, 0.5, None, None, 1, 0, None print('SABC Inferring') ## We use resultfakeobs1 as our observed dataset journal_sabc1 = sampler.sample([resultfakeobs1], steps, epsilon, n_samples, n_samples_per_param, beta, delta, v, ar_cutoff, resample, n_update, adaptcov, full_output, journal_file) print(journal_sabc1.posterior_mean()) journal_sabc1.save('sabc_' + sim_model + '_' + exp_dataset + '.jrnl')
# print(distance_calculator.distance(resultfakeobs1, resultfakeobs2)) # # Define kernel from abcpy.perturbationkernel import DefaultKernel kernel = DefaultKernel([ B_0, activation_energy, energy_tissue, energy_food, energy_synthesis, half_saturation_coeff, max_ingestion_rate, mass_birth, mass_cocoon, mass_maximum, mass_sexual_maturity, growth_constant, max_reproduction_rate, speed ]) ## SABC ## from abcpy.inferences import SABC sampler = SABC([EarthWorm], [distance_calculator], backend, kernel, seed=1) steps, epsilon, n_samples, n_samples_per_param, ar_cutoff, full_output, journal_file = 10, 10000, 500, 1, 0.001, 1, None print('SABC Inferring') # We use resultfakeobs1 as our observed dataset journal_sabc = sampler.sample([resultfakeobs1], steps=steps, epsilon=epsilon, n_samples=n_samples, n_samples_per_param=n_samples_per_param, ar_cutoff=ar_cutoff, full_output=full_output, journal_file=journal_file) print(journal_sabc.posterior_mean()) journal_sabc.save('sabc_earthworm_fakeobs1.jrnl')
for ind in range(60): wt = wt + list((1 / 30) * np.ones(11)) distance_calculator = WeightedEuclidean(statistics_calculator, weight=np.array(wt)) #print('# Check whether the distance works') #print(distance_calculator.distance(obs_data, resultfakeobs1)) if sample: # Define kernel from abcpy.perturbationkernel import DefaultKernel kernel = DefaultKernel([H, Am, AE, PM, I]) if algorithm == 'sabc': ## SABC ## from abcpy.inferences import SABC sampler = SABC([Bass], [distance_calculator], backend, kernel, seed=1) steps, epsilon, n_samples, n_samples_per_param, ar_cutoff, full_output, journal_file = 10, 10e20, 111, 1, 0.001, 1, None print('SABC Inferring') # We use resultfakeobs1 as our observed dataset journal_sabc = sampler.sample(observations=[obs_data], steps=steps, epsilon=epsilon, n_samples=n_samples, n_samples_per_param=n_samples_per_param, beta=2, delta=0.2, v=0.3, ar_cutoff=0.001, resample=None, n_update=None, full_output=1,
#Define model lb, ub = [50, 5, 0.1, 0.5e-3, 0], [150, 20, 1.5, 3e-3, 10] prior = Uniform(lb=[50, 5, 0.1, 0.5e-3, 0], ub=[150, 20, 1.5, 3e-3, 10]) pAd, pAg, pT, pF, aT = 110, 14.6, 0.6, 1.7e-3, 6 model = Deposition(prior, pAd, pAg, pT, pF, aT, seed=1) # Observed data a = np.array([[0, 0, 0, 172200, 4808], [20, 1689, 26.8, 155100, 1683], [60, 2004, 29.9, 149400, 0], [120, 1968, 31.3, 140700, 0], [300, 1946, 36.6, 125800, 0]]) np.save('depo_experimental.npy', a) BE = np.zeros(shape=(10, 5)) index = 5 y_obs = np.load('depo_experimental.npy')[index] # Define summary stat and distance stat_calc = DepositionStatistics(degree=1, cross=0) dist_calc = DepositionDistance(stat_calc) mean = np.array([-13.0, .0, 7.0]) cov = np.eye(3) kernel = MultiNormal(mean, cov, seed=1) steps, epsilon, n_samples, n_samples_per_param = 2, 40, 1, 1 sampler = SABC(model, dist_calc, kernel, backend, seed=1) journal_sabc = sampler.sample([y_obs], steps, epsilon, n_samples, n_samples_per_param) journal_sabc.save('experimental_5.jrnl') samples = (journal_sabc.get_parameters(), journal_sabc.get_weights())
def ABC_inference(algorithm, model, observation, distance_calculator, eps, n_samples, n_steps, backend, seed=None, full_output=0, **kwargs): """NB: eps represents initial value of epsilon for PMCABC and SABC; represents the single eps value for RejectionABC and represents the final value for SMCABC.""" start = time() if algorithm == "PMCABC": sampler = PMCABC([model], [distance_calculator], backend, seed=seed) jrnl = sampler.sample([[observation]], n_steps, np.array([eps]), n_samples=n_samples, full_output=full_output, **kwargs) if algorithm == "APMCABC": sampler = APMCABC([model], [distance_calculator], backend, seed=seed) jrnl = sampler.sample([[observation]], n_steps, n_samples=n_samples, full_output=full_output, **kwargs) elif algorithm == "SABC": sampler = SABC([model], [distance_calculator], backend, seed=seed) jrnl = sampler.sample([[observation]], n_steps, eps, n_samples=n_samples, full_output=full_output, **kwargs) elif algorithm == "RejectionABC": sampler = RejectionABC([model], [distance_calculator], backend, seed=seed) jrnl = sampler.sample([[observation]], eps, n_samples=n_samples, full_output=full_output, **kwargs) elif algorithm == "SMCABC": # for this usually a larger number of steps is required. alpha can be left to 0.95, covFactor=2 and # resample=None. epsilon_final is instead important to fix! sampler = SMCABC([model], [distance_calculator], backend, seed=seed) # sample(observations, steps, n_samples=10000, n_samples_per_param=1, epsilon_final=0.1, alpha=0.95, # covFactor=2, resample=None, full_output=0, which_mcmc_kernel=0, journal_file=None) jrnl = sampler.sample([[observation]], n_steps, n_samples=n_samples, full_output=full_output, epsilon_final=eps, **kwargs) print("It took ", time() - start, " seconds.") return jrnl