def test_sample(self): # use the APMCABC scheme for T = 1 steps, n_sample, n_simulate = 1, 10, 1 sampler = APMCABC(self.model, self.dist_calc, self.kernel, self.backend, seed=1) journal = sampler.sample(self.observation, steps, n_sample, n_simulate) 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.assertEqual((mu_post_mean, sigma_post_mean), (,)) # use the APMCABC scheme for T = 2 T, n_sample, n_simulate = 2, 10, 1 sampler = APMCABC(self.model, self.dist_calc, self.kernel, self.backend, seed=1) journal = sampler.sample(self.observation, T, n_sample, n_simulate) 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 - (2.19137364411), 10e-2) self.assertLess(sigma_post_mean - 5.66226403628, 10e-2)
def test_sample(self): # use the APMCABC scheme for T = 1 steps, n_sample, n_simulate = 1, 10, 1 sampler = APMCABC([self.model], [self.dist_calc], self.backend, seed=1) journal = sampler.sample([self.observation], steps, n_sample, n_simulate, alpha=.9) 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 = journal.posterior_mean( )['mu'], journal.posterior_mean()['sigma'] # test shape of sample mu_sample_shape, sigma_sample_shape, weights_sample_shape = (len(mu_post_sample), mu_post_sample[0].shape[1]), \ (len(sigma_post_sample), sigma_post_sample[0].shape[1]), post_weights.shape self.assertEqual(mu_sample_shape, (10, 1)) self.assertEqual(sigma_sample_shape, (10, 1)) self.assertEqual(weights_sample_shape, (10, 1)) self.assertFalse(journal.number_of_simulations == 0) # use the APMCABC scheme for T = 2 T, n_sample, n_simulate = 2, 10, 1 sampler = APMCABC([self.model], [self.dist_calc], self.backend, seed=1) journal = sampler.sample([self.observation], T, n_sample, n_simulate, alpha=.9) 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 = journal.posterior_mean( )['mu'], journal.posterior_mean()['sigma'] # test shape of sample mu_sample_shape, sigma_sample_shape, weights_sample_shape = (len(mu_post_sample), mu_post_sample[0].shape[1]), \ (len(sigma_post_sample), sigma_post_sample[0].shape[1]), post_weights.shape 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 - (-3.397848324005792), 10e-2) self.assertLess(sigma_post_mean - 6.451434816944525, 10e-2) self.assertFalse(journal.number_of_simulations == 0)
def test_sample(self): # use the APMCABC scheme for T = 1 steps, n_sample, n_simulate = 1, 10, 1 sampler = APMCABC([self.model], [self.dist_calc], self.backend, seed=1) journal = sampler.sample([self.observation], steps, n_sample, n_simulate) 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.assertFalse(journal.number_of_simulations == 0) # use the APMCABC scheme for T = 2 T, n_sample, n_simulate = 2, 10, 1 sampler = APMCABC([self.model], [self.dist_calc], self.backend, seed=1) journal = sampler.sample([self.observation], T, n_sample, n_simulate) 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 - (-2.785), 10e-2) self.assertLess(sigma_post_mean - 6.2058, 10e-2) self.assertFalse(journal.number_of_simulations == 0)
def infer_parameters_apmcabc(): # define observation for true parameters mean=170, 65 rng = np.random.RandomState(seed=1) 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]], name="mu0") mu1 = Uniform([[25], [100]], name="mu1") # 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 APMCABC sampler = APMCABC([height_weight_model], [distance_calculator], backend, seed=1) steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file = 2, 100, 1, 0.2, 0.03, 2.0, 1, None print('APMCABC Inferring') journal = sampler.sample([y_obs], steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file) return journal
# Define kernel from abcpy.backends import BackendMPI as Backend backend = Backend() from abcpy.perturbationkernel import DefaultKernel kernel = DefaultKernel([theta1, theta2]) ######### Inference for simulated data ############### water_obs = [np.load('Data/obs_data.npy')] sampler = APMCABC([water], [distance_calculator], backend, kernel, seed = 1) steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file =10, 100, 1, 0.1, 0.03, 2.0, 1, None print('TIP4P: APMCABC Inferring for simulated data') journal_apmcabc = sampler.sample([water_obs], steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file) print('TIP4P: APMCABC done for simulated data') journal_apmcabc.save('Result/MD_GROMACS_APMCABC_obs.jrnl') ######### Inference for Experimental data 1 (Neutron Diffraction of Water) ############### water_obs = [np.load('Data/exp_data.npy')] sampler = APMCABC([water], [distance_calculator], backend, kernel, seed = 1) steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file =10, 100, 1, 0.1, 0.03, 2.0, 1, None print('TIP4P: APMCABC Inferring for experimental data 1') journal_apmcabc = sampler.sample([water_obs], steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file) print('TIP4P: APMCABC done for experimental data 1') journal_apmcabc.save('Result/MD_GROMACS_APMCABC_exp.jrnl')
# Check whether the distance works if distance_calculator.distance(resultfakeobs1, resultfakeobs1)==distance_calculator.distance(resultfakeobs1, resultfakeobs2): 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())
# define statistics # from abcpy.statistics import Identity # statistics_calculator = Identity(degree=1, cross=False) from Statistics import NeuralEmbeddingStatistics from Statistics import load_net from distance_learning.networks import EmbeddingNet # Here we load the network and pass it to the statistics calculator embedding_net_triplet = load_net("saved-networks/triplet.pth", EmbeddingNet) embedding_net_triplet.eval() statistics_calculator = NeuralEmbeddingStatistics(embedding_net_triplet, degree=1, cross=False) # define distance from Distance import DistanceVolcano distance_calculator = DistanceVolcano(statistics_calculator) #print(distance_calculator.distance(fake_data, obs_data)) # # define sampling scheme # # (seed is not used for now) from abcpy.inferences import APMCABC sampler = APMCABC([volcano_model], [distance_calculator], backend, seed=1) print('sampling') steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file = 6, 100, 1, 0.1, 0.03, 2.0, 1, None #steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file = 1, 100, 1, 0.1, 0.03, 2.0, 1, 'VolcanojournalAPMCABC_pululagua.jrnl' journal = sampler.sample([obs_data], steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file) journal.save('VolcanojournalAPMCABC_pululagua.jrnl')
from abcpy.statistics import Identity statistics_calculator = Identity(degree=1, cross=False) # Define distance from Distance import DistanceType1 distance_calculator = DistanceType1(statistics_calculator) print('# Check whether the distance works') print(distance_calculator.distance(resultfakeobs1, resultfakeobs1)) print(distance_calculator.distance(resultfakeobs1, resultfakeobs2)) # Define kernel from abcpy.perturbationkernel import MultivariateNormalKernel, RandomWalkKernel, JointPerturbationKernel # Join the defined kernels kernelcontinuous = MultivariateNormalKernel( [bufferRatio, kW, kS, kD, decay, diffusion]) kerneldiscrete = RandomWalkKernel([bufferAngle]) kernel = JointPerturbationKernel([kernelcontinuous, kerneldiscrete]) # ## 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, 100, 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_fakeobs1.jrnl')
# if distance_calculator.distance(resultfakeobs1, resultfakeobs1)==distance_calculator.distance(resultfakeobs1, resultfakeobs2): # 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, 10, 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 Inferences import SABC sampler = SABC([ff], [distance_calculator], backend, kernel, seed=1) print('SABC Inferring') ## We use resultfakeobs1 as our observed dataset journal_sabc1 = sampler.sample([resultfakeobs1], steps=4, epsilon=40, n_samples=30, n_samples_per_param=1, beta=2, \