def test(self): B1 = Binomial([10, 0.2]) N1 = Normal([0.03, 0.01]) N2 = Normal([0.1, N1]) graph1 = Normal([B1, N2]) graph2 = Normal([1, N2]) statistics_calculator = Identity(degree=2, cross=False) distance_calculator = LogReg(statistics_calculator) backend = Backend() sampler = RejectionABC([graph1, graph2], [distance_calculator, distance_calculator], backend) rng = np.random.RandomState(1) sampler.sample_from_prior(rng=rng) y_sim = sampler.simulate(1, rng=rng) self.assertTrue(isinstance(y_sim, list)) self.assertTrue(len(y_sim) == 2) self.assertTrue(isinstance(y_sim[0][0], np.ndarray))
def test_DefaultKernel(self): B1 = Binomial([10, 0.2]) N1 = Normal([0.1, 0.01]) N2 = Normal([0.3, N1]) graph = Normal([B1, N2]) Manager = AcceptedParametersManager([graph]) backend = Backend() kernel = DefaultKernel([N1, N2, B1]) Manager.update_broadcast(backend, [[2, 0.27, 0.097], [3, 0.32, 0.012]], np.array([1, 1]), accepted_cov_mats=[[[0.01, 0], [0, 0.01]], []]) kernel_parameters = [] for krnl in kernel.kernels: kernel_parameters.append( Manager.get_accepted_parameters_bds_values(krnl.models)) Manager.update_kernel_values(backend, kernel_parameters=kernel_parameters) rng = np.random.RandomState(1) perturbed_values_and_models = kernel.update(Manager, 1, rng) self.assertEqual(perturbed_values_and_models, [(N1, [0.17443453636632419]), (N2, [0.25882435863499248]), (B1, [3])])
def setUp(self): class Mockobject(Normal): def __init__(self, parameters): super(Mockobject, self).__init__(parameters) def pdf(self, input_values, x): return x self.N1 = Uniform([[1.3], [1.55]], name='n1') self.N2 = Uniform([[self.N1], [1.60]], name='n2') self.N3 = Uniform([[2], [4]], name='n3') self.graph1 = Uniform([[self.N1], [self.N2]], name='graph1') self.graph2 = Uniform([[.5], [self.N3]]) self.graph = [self.graph1, self.graph2] statistics_calculator = Identity(degree=2, cross=False) distance_calculator = LogReg(statistics_calculator) backend = Backend() self.sampler1 = RejectionABC([self.graph1], [distance_calculator], backend) self.sampler2 = RejectionABC([self.graph2], [distance_calculator], backend) self.sampler3 = RejectionABC( self.graph, [distance_calculator, distance_calculator], backend) self.pdf1 = self.sampler1.pdf_of_prior(self.sampler1.model, [1.32088846, 1.42945274]) self.pdf2 = self.sampler2.pdf_of_prior(self.sampler2.model, [3]) self.pdf3 = self.sampler3.pdf_of_prior(self.sampler3.model, [1.32088846, 1.42945274, 3])
def setUp(self): # define prior and model sigma = Uniform([[10], [20]]) mu = Normal([0, 1]) self.Y = Normal([mu, sigma]) # define backend self.backend = Backend() # define statistics self.statistics_cal = Identity(degree=3, cross=False) if has_torch: # Initialize statistics learning self.statisticslearning = SemiautomaticNN([self.Y], self.statistics_cal, self.backend, n_samples=100, n_samples_per_param=1, seed=1, n_epochs=10, scale_samples=False) # with sample scaler: self.statisticslearning_with_scaler = SemiautomaticNN( [self.Y], self.statistics_cal, self.backend, n_samples=100, n_samples_per_param=1, seed=1, n_epochs=10, scale_samples=True)
def infer_parameters(): # define observation for true parameters mean=170, std=15 y_obs = [ 160.82499176, 167.24266737, 185.71695756, 153.7045709, 163.40568812, 140.70658699, 169.59102084, 172.81041696, 187.38782738, 179.66358934, 176.63417241, 189.16082803, 181.98288443, 170.18565017, 183.78493886, 166.58387299, 161.9521899, 155.69213073, 156.17867343, 144.51580379, 170.29847515, 197.96767899, 153.36646527, 162.22710198, 158.70012047, 178.53470703, 170.77697743, 164.31392633, 165.88595994, 177.38083686, 146.67058471763457, 179.41946565658628, 238.02751620619537, 206.22458790620766, 220.89530574344568, 221.04082532837026, 142.25301427453394, 261.37656571434275, 171.63761180867033, 210.28121820385866, 237.29130237612236, 175.75558340169619, 224.54340549862235, 197.42448680731226, 165.88273684581381, 166.55094082844519, 229.54308602661584, 222.99844054358519, 185.30223966014586, 152.69149367593846, 206.94372818527413, 256.35498655339154, 165.43140916577741, 250.19273595481803, 148.87781549665536, 223.05547559193792, 230.03418198709608, 146.13611923127021, 138.24716809523139, 179.26755740864527, 141.21704876815426, 170.89587081800852, 222.96391329259626, 188.27229523693822, 202.67075179617672, 211.75963110985992, 217.45423324370509 ] # define prior from abcpy.distributions import Uniform prior = Uniform([150, 5], [200, 25]) # define the model model = Gaussian(prior) # define statistics from abcpy.statistics import Identity statistics_calculator = Identity(degree=2, cross=False) # define distance from abcpy.distances import LogReg distance_calculator = LogReg(statistics_calculator) # define kernel from abcpy.distributions import MultiStudentT mean, cov, df = np.array([.0, .0]), np.eye(2), 3. kernel = MultiStudentT(mean, cov, df) # define backend from abcpy.backends import BackendDummy as Backend backend = Backend() # define sampling scheme from abcpy.inferences import PMCABC sampler = PMCABC(model, distance_calculator, kernel, backend) # sample from scheme T, n_sample, n_samples_per_param = 3, 250, 10 eps_arr = np.array([.75]) epsilon_percentile = 10 journal = sampler.sample(y_obs, T, eps_arr, n_sample, n_samples_per_param, epsilon_percentile) return journal
def setUp(self): self.B1 = Binomial([10, 0.2]) self.N1 = Normal([0.03, 0.01]) self.N2 = Normal([0.1, self.N1]) self.graph = Normal([self.B1, self.N2]) statistics_calculator = Identity(degree=2, cross=False) distance_calculator = LogReg(statistics_calculator) backend = Backend() self.sampler = PMCABC([self.graph], [distance_calculator], backend) self.rng = np.random.RandomState(1) self.sampler.sample_from_prior(rng=self.rng) kernel = DefaultKernel([self.N1, self.N2, self.B1]) self.sampler.kernel = kernel self.sampler.accepted_parameters_manager.update_broadcast( self.sampler.backend, [[3, 0.11, 0.029], [4, 0.098, 0.031]], accepted_cov_mats=[[[1, 0], [0, 1]]], accepted_weights=np.array([1, 1])) kernel_parameters = [] for kernel in self.sampler.kernel.kernels: kernel_parameters.append( self.sampler.accepted_parameters_manager. get_accepted_parameters_bds_values(kernel.models)) self.sampler.accepted_parameters_manager.update_kernel_values( self.sampler.backend, kernel_parameters)
def setup_backend(): from abcpy.backends import BackendMPI as Backend backend = Backend() # The above line is equivalent to: # backend = Backend(process_per_model=1) # Notice: Models not parallelized by MPI should not be given process_per_model > 1 return backend
def setup_backend(): global backend import pyspark sc = pyspark.SparkContext() from abcpy.backends import BackendSpark as Backend backend = Backend(sc, parallelism=4)
def test(self): B1 = Binomial([10, 0.2]) N1 = Normal([0.1, 0.01]) N2 = Normal([0.3, N1]) graph = Normal([B1, N2]) Manager = AcceptedParametersManager([graph]) backend = Backend() Manager.update_broadcast(backend, [[2,3,4],[0.27,0.32,0.28],[0.97,0.12,0.99]]) values = Manager.get_accepted_parameters_bds_values([B1,N2,N1]) values_expected = [np.array(x).reshape(-1,) for x in [[2,3,4],[0.27,0.32,0.28],[0.97,0.12,0.99]]] self.assertTrue(all([all(a == b) for a, b in zip(values, values_expected)]))
def setUp(self): # define prior and model sigma = Uniform([[10], [20]]) mu = Normal([0, 1]) Y = Normal([mu, sigma]) # define backend self.backend = Backend() # define statistics self.statistics_cal = Identity(degree = 3, cross = False) # Initialize summaryselection self.summaryselection = Semiautomatic([Y], self.statistics_cal, self.backend, n_samples = 1000, n_samples_per_param = 1, seed = 1)
def setUp(self): self.stat_calc = Identity(degree = 1, cross = 0) # define prior and model prior = Uniform([150, 5],[200, 25]) self.model = Gaussian(prior, seed = 1) # define backend self.backend = Backend() # define statistics self.statistics_cal = Identity(degree = 2, cross = False) #Initialize summaryselection self.summaryselection = Semiautomatic(self.model, self.statistics_cal, self.backend, n_samples = 1000, seed = 1)
def test(self): N1 = Normal([1, 0.1]) N2 = Normal([N1, 0.1]) N2.visited = True N1.visited = True statistics_calculator = Identity(degree=2, cross=False) distance_calculator = LogReg(statistics_calculator) backend = Backend() sampler = RejectionABC([N2], [distance_calculator], backend) sampler._reset_flags() self.assertFalse(N1.visited) self.assertFalse(N2.visited)
def setUp(self): self.B1 = Binomial([10, 0.2]) self.N1 = Normal([0.03, 0.01]) self.N2 = Normal([0.1, self.N1]) self.graph = Normal([self.B1, self.N2]) statistics_calculator = Identity(degree=2, cross=False) distance_calculator = LogReg(statistics_calculator) backend = Backend() self.sampler = RejectionABC([self.graph], [distance_calculator], backend) self.rng = np.random.RandomState(1) self.sampler.sample_from_prior(rng=self.rng)
def test_return_value_Student_T(self): N1 = Normal([0.1, 0.01]) N2 = Normal([0.3, N1]) graph = Normal([N1, N2]) Manager = AcceptedParametersManager([graph]) backend = Backend() kernel = JointPerturbationKernel([MultivariateStudentTKernel([N1, N2], df=2)]) Manager.update_broadcast(backend, [[0.4, 0.09], [0.2, 0.008]], np.array([0.5, 0.2])) kernel_parameters = [] for krnl in kernel.kernels: kernel_parameters.append(Manager.get_accepted_parameters_bds_values(krnl.models)) Manager.update_kernel_values(backend, kernel_parameters) mapping, mapping_index = Manager.get_mapping(Manager.model) covs = [[[1, 0], [0, 1]], []] Manager.update_broadcast(backend, accepted_cov_mats=covs) pdf = kernel.pdf(mapping, Manager, Manager.accepted_parameters_bds.value()[1], [0.3, 0.1]) self.assertTrue(isinstance(pdf, float))
def test(self): B1 = Binomial([10, 0.2]) N1 = Normal([0.03, 0.01]) N2 = Normal([0.1, N1]) graph = Normal([B1, N2]) statistics_calculator = Identity(degree=2, cross=False) distance_calculator = LogReg(statistics_calculator) backend = Backend() sampler = RejectionABC([graph], [distance_calculator], backend) rng = np.random.RandomState(1) sampler.sample_from_prior(rng=rng) self.assertIsNotNone(B1._fixed_values) self.assertIsNotNone(N1._fixed_values) self.assertIsNotNone(N2._fixed_values)
def test_return_value(self): B1 = Binomial([10, 0.2]) N1 = Normal([0.1, 0.01]) N2 = Normal([0.3, N1]) graph = Normal([B1, N2]) Manager = AcceptedParametersManager([graph]) backend = Backend() kernel = DefaultKernel([N1, N2, B1]) Manager.update_broadcast(backend, [[2, 0.4, 0.09], [3, 0.2, 0.008]], np.array([0.5, 0.2])) kernel_parameters = [] for krnl in kernel.kernels: kernel_parameters.append(Manager.get_accepted_parameters_bds_values(krnl.models)) Manager.update_kernel_values(backend, kernel_parameters) mapping, mapping_index = Manager.get_mapping(Manager.model) covs = [[[1,0],[0,1]],[]] Manager.update_broadcast(backend, accepted_cov_mats=covs) pdf = kernel.pdf(mapping, Manager, 1, [2,0.3,0.1]) self.assertTrue(isinstance(pdf, float))
def setUp(self): # define observation for true parameters mean=170, std=15 self.y_obs = [160.82499176] self.model_array = [None] * 2 #Model 1: Gaussian # define prior prior = Uniform([150, 5], [200, 25]) # define the model self.model_array[0] = Gaussian(prior, seed=1) #Model 2: Student t # define prior prior = Uniform([150, 1], [200, 30]) # define the model self.model_array[1] = Student_t(prior, seed=1) # define statistics self.statistics_calc = Identity(degree=2, cross=False) # define backend self.backend = Backend()
def test_Student_T(self): N1 = Normal([0.1, 0.01]) N2 = Normal([0.3, N1]) graph = Normal([N1, N2]) Manager = AcceptedParametersManager([graph]) backend = Backend() kernel = JointPerturbationKernel([MultivariateStudentTKernel([N1, N2], df=2)]) Manager.update_broadcast(backend, [[0.27, 0.097], [0.32, 0.012]], np.array([1, 1])) kernel_parameters = [] for krnl in kernel.kernels: kernel_parameters.append(Manager.get_accepted_parameters_bds_values(krnl.models)) Manager.update_kernel_values(backend, kernel_parameters) covs = kernel.calculate_cov(Manager) print(covs) self.assertTrue(len(covs) == 1) self.assertTrue(len(covs[0]) == 2)
def test(self): B1 = Binomial([10, 0.2]) N1 = Normal([0.03, 0.01]) N2 = Normal([0.1, N1]) graph1 = Normal([B1, N2]) graph2 = Normal([1, N2]) statistics_calculator = Identity(degree=2, cross=False) distance_calculator = LogReg(statistics_calculator) backend = Backend() sampler = RejectionABC([graph1, graph2], [distance_calculator, distance_calculator], backend) rng = np.random.RandomState(1) sampler.sample_from_prior(rng=rng) mapping, index = sampler._get_mapping() self.assertTrue(mapping == [(B1, 0), (N2, 1), (N1, 2)])
def setUp(self): # define observation for true parameters mean=170, std=15 self.y_obs = [160.82499176] self.model_array = [None] * 2 # Model 1: Gaussian # define prior self.mu1 = Uniform([[150], [200]], name='mu1') self.sigma1 = Uniform([[5.0], [25.0]], name='sigma1') # define the model self.model_array[0] = Normal([self.mu1, self.sigma1]) # Model 2: Student t # define prior self.mu2 = Uniform([[150], [200]], name='mu2') self.sigma2 = Uniform([[1], [30.0]], name='sigma2') # define the model self.model_array[1] = StudentT([self.mu2, self.sigma2]) # define statistics self.statistics_calc = Identity(degree=2, cross=False) # define backend self.backend = Backend()
def setUp(self): # define prior and model sigma = Uniform([[10], [20]]) mu = Normal([0, 1]) self.Y = Normal([mu, sigma]) # define backend self.backend = Backend() # define statistics self.statistics_cal = Identity(degree=3, cross=False) if has_torch: # Initialize statistics learning self.statisticslearning = TripletDistanceLearning( [self.Y], self.statistics_cal, self.backend, n_samples=100, n_samples_per_param=1, seed=1, n_epochs=10)
def test(self): B1 = Binomial([10, 0.2]) N1 = Normal([0.1, 0.01]) N2 = Normal([0.3, N1]) graph = Normal([B1, N2]) Manager = AcceptedParametersManager([graph]) backend = Backend() kernel = DefaultKernel([N1, N2, B1]) Manager.update_broadcast(backend, [[2, 0.27, 0.097], [3, 0.32, 0.012]], np.array([1, 1])) kernel_parameters = [] for krnl in kernel.kernels: kernel_parameters.append(Manager.get_accepted_parameters_bds_values(krnl.models)) Manager.update_kernel_values(backend, kernel_parameters) covs = kernel.calculate_cov(Manager) self.assertTrue(len(covs)==2) self.assertTrue(len(covs[0])==2) self.assertTrue(not(covs[1]))
def infer_model(): # define observation for true parameters mean=170, std=15 y_obs = [160.82499176] ## Create a array of models from abcpy.continuousmodels import Uniform, Normal, StudentT model_array = [None] * 2 #Model 1: Gaussian mu1 = Uniform([[150], [200]], name='mu1') sigma1 = Uniform([[5.0], [25.0]], name='sigma1') model_array[0] = Normal([mu1, sigma1]) #Model 2: Student t mu2 = Uniform([[150], [200]], name='mu2') sigma2 = Uniform([[1], [30.0]], name='sigma2') model_array[1] = StudentT([mu2, sigma2]) # define statistics from abcpy.statistics import Identity statistics_calculator = Identity(degree=2, cross=False) # define backend from abcpy.backends import BackendDummy as Backend backend = Backend() # Initiate the Model selection scheme modelselection = RandomForest(model_array, statistics_calculator, backend, seed=1) # Choose the correct model model = modelselection.select_model(y_obs, n_samples=100, n_samples_per_param=1) # Compute the posterior probability of each of the models model_prob = modelselection.posterior_probability(y_obs)
def test_Student_T(self): N1 = Normal([0.1, 0.01]) N2 = Normal([0.3, N1]) graph = Normal([N1, N2]) Manager = AcceptedParametersManager([graph]) backend = Backend() kernel = JointPerturbationKernel([MultivariateStudentTKernel([N1, N2], df=2)]) Manager.update_broadcast(backend, [[0.27, 0.097], [0.32, 0.012]], np.array([1, 1]), accepted_cov_mats=[[[0.01, 0], [0, 0.01]], []]) kernel_parameters = [] for krnl in kernel.kernels: kernel_parameters.append( Manager.get_accepted_parameters_bds_values(krnl.models)) Manager.update_kernel_values(backend, kernel_parameters=kernel_parameters) rng = np.random.RandomState(1) perturbed_values_and_models = kernel.update(Manager, 1, rng) print(perturbed_values_and_models) self.assertEqual(perturbed_values_and_models, [(N1, [0.2107982411716391]), (N2, [-0.049106838502166614])])
def setup_backend(): global backend from abcpy.backends import BackendMPI as Backend backend = Backend()
from abcpy.inferences import APMCABC from abcpy.continuousmodels import Uniform from Model import TIP4PGromacsOOOH as Water # Define Graphical model theta1 = Uniform([[.281] , [.53]],name='theta1') theta2 = Uniform([[0.2] , [0.9]],name='theta2') water = Water([theta1, theta2, 2500000]) # Define distance and statistics statistics_calculator = Identity(degree = 1, cross = False) distance_calculator = Abserror(statistics_calculator) # 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')
def infer_parameters(steps=2, n_sample=50, n_samples_per_param=1, logging_level=logging.WARN): """Perform inference for this example. Parameters ---------- steps : integer, optional Number of iterations in the sequential PMCABC algorithm ("generations"). The default value is 3 n_samples : integer, optional Number of posterior samples to generate. The default value is 250. n_samples_per_param : integer, optional Number of data points in each simulated data set. The default value is 10. Returns ------- abcpy.output.Journal A journal containing simulation results, metadata and optionally intermediate results. """ logging.basicConfig(level=logging_level) # define backend # Note, the dummy backend does not parallelize the code! from abcpy.backends import BackendDummy as Backend backend = Backend() # define observation for true parameters mean=170, std=15 height_obs = [160.82499176, 167.24266737, 185.71695756, 153.7045709, 163.40568812, 140.70658699, 169.59102084, 172.81041696, 187.38782738, 179.66358934, 176.63417241, 189.16082803, 181.98288443, 170.18565017, 183.78493886, 166.58387299, 161.9521899, 155.69213073, 156.17867343, 144.51580379, 170.29847515, 197.96767899, 153.36646527, 162.22710198, 158.70012047, 178.53470703, 170.77697743, 164.31392633, 165.88595994, 177.38083686, 146.67058471763457, 179.41946565658628, 238.02751620619537, 206.22458790620766, 220.89530574344568, 221.04082532837026, 142.25301427453394, 261.37656571434275, 171.63761180867033, 210.28121820385866, 237.29130237612236, 175.75558340169619, 224.54340549862235, 197.42448680731226, 165.88273684581381, 166.55094082844519, 229.54308602661584, 222.99844054358519, 185.30223966014586, 152.69149367593846, 206.94372818527413, 256.35498655339154, 165.43140916577741, 250.19273595481803, 148.87781549665536, 223.05547559193792, 230.03418198709608, 146.13611923127021, 138.24716809523139, 179.26755740864527, 141.21704876815426, 170.89587081800852, 222.96391329259626, 188.27229523693822, 202.67075179617672, 211.75963110985992, 217.45423324370509] # define prior from abcpy.continuousmodels import Uniform mu = Uniform([[150], [200]], name="mu") sigma = Uniform([[5], [25]], name="sigma") # define the model from abcpy.continuousmodels import Normal height = Normal([mu, sigma], ) # 1) generate simulations from prior from abcpy.inferences import DrawFromPrior draw_from_prior = DrawFromPrior([height], backend=backend) # notice the use of the `.sample_par_sim_pairs` method rather than `.sample` to obtain data suitably formatted # for the summary statistics learning routines parameters, simulations = draw_from_prior.sample_par_sim_pairs(100, n_samples_per_param=1) # if you want to use the test loss to do early stopping in the training: parameters_val, simulations_val = draw_from_prior.sample_par_sim_pairs(100, n_samples_per_param=1) # discard the mid dimension (n_samples_per_param, as the StatisticsLearning classes use that =1) simulations = simulations.reshape(simulations.shape[0], simulations.shape[2]) simulations_val = simulations_val.reshape(simulations_val.shape[0], simulations_val.shape[2]) # 2) now train the NNs with the different methods with the generated data from abcpy.statistics import Identity identity = Identity() # to apply before computing the statistics logging.info("semiNN") from abcpy.statisticslearning import SemiautomaticNN, TripletDistanceLearning semiNN = SemiautomaticNN([height], identity, backend=backend, parameters=parameters, simulations=simulations, parameters_val=parameters_val, simulations_val=simulations_val, early_stopping=True, # early stopping seed=1, n_epochs=10, scale_samples=False, use_tqdm=False) logging.info("triplet") triplet = TripletDistanceLearning([height], identity, backend=backend, parameters=parameters, simulations=simulations, parameters_val=parameters_val, simulations_val=simulations_val, early_stopping=True, # early stopping seed=1, n_epochs=10, scale_samples=True, use_tqdm=False) # 3) save and re-load NNs: # get the statistics from the already fit StatisticsLearning object 'semiNN': learned_seminn_stat = semiNN.get_statistics() learned_triplet_stat = triplet.get_statistics() # this has a save net method: learned_seminn_stat.save_net("seminn_net.pth") # if you used `scale_samples=True` in learning the NNs, need to provide a path where pickle stores the scaler too: learned_triplet_stat.save_net("triplet_net.pth", path_to_scaler="scaler.pkl") # to reload: need to use the Neural Embedding statistics fromFile; this needs to know which kind of NN it is using; # need therefore to pass either the input/output size (it data size and number parameters) or the network class if # that was specified explicitly in the StatisticsLearning class. Check the docstring for NeuralEmbedding.fromFile # for more details. from abcpy.statistics import NeuralEmbedding learned_seminn_stat_loaded = NeuralEmbedding.fromFile("seminn_net.pth", input_size=1, output_size=2) learned_triplet_stat_loaded = NeuralEmbedding.fromFile("triplet_net.pth", input_size=1, output_size=2, path_to_scaler="scaler.pkl") # 4) you can optionally rescale the different summary statistics be their standard deviation on a reference dataset # of simulations. To do this, it is enough to pass at initialization the reference dataset, and the rescaling will # be applied every time the statistics is computed on some simulation or observation. learned_triplet_stat_loaded = NeuralEmbedding.fromFile("triplet_net.pth", input_size=1, output_size=2, path_to_scaler="scaler.pkl", reference_simulations=simulations_val) # 5) perform inference # define distance from abcpy.distances import Euclidean distance_calculator = Euclidean(learned_seminn_stat_loaded) # define kernel from abcpy.perturbationkernel import DefaultKernel kernel = DefaultKernel([mu, sigma]) # define sampling scheme from abcpy.inferences import PMCABC sampler = PMCABC([height], [distance_calculator], backend, kernel, seed=1) eps_arr = np.array([500]) # starting value of epsilon; the smaller, the slower the algorithm. # at each iteration, take as epsilon the epsilon_percentile of the distances obtained by simulations at previous # iteration from the observation epsilon_percentile = 10 journal = sampler.sample([height_obs], steps, eps_arr, n_sample, n_samples_per_param, epsilon_percentile) return journal
def infer_parameters(): # define backend # Note, the dummy backend does not parallelize the code! from abcpy.backends import BackendDummy as Backend backend = Backend() # define observation for true parameters mean=170, std=15 height_obs = [ 160.82499176, 167.24266737, 185.71695756, 153.7045709, 163.40568812, 140.70658699, 169.59102084, 172.81041696, 187.38782738, 179.66358934, 176.63417241, 189.16082803, 181.98288443, 170.18565017, 183.78493886, 166.58387299, 161.9521899, 155.69213073, 156.17867343, 144.51580379, 170.29847515, 197.96767899, 153.36646527, 162.22710198, 158.70012047, 178.53470703, 170.77697743, 164.31392633, 165.88595994, 177.38083686, 146.67058471763457, 179.41946565658628, 238.02751620619537, 206.22458790620766, 220.89530574344568, 221.04082532837026, 142.25301427453394, 261.37656571434275, 171.63761180867033, 210.28121820385866, 237.29130237612236, 175.75558340169619, 224.54340549862235, 197.42448680731226, 165.88273684581381, 166.55094082844519, 229.54308602661584, 222.99844054358519, 185.30223966014586, 152.69149367593846, 206.94372818527413, 256.35498655339154, 165.43140916577741, 250.19273595481803, 148.87781549665536, 223.05547559193792, 230.03418198709608, 146.13611923127021, 138.24716809523139, 179.26755740864527, 141.21704876815426, 170.89587081800852, 222.96391329259626, 188.27229523693822, 202.67075179617672, 211.75963110985992, 217.45423324370509 ] # define prior from abcpy.continuousmodels import Uniform mu = Uniform([[150], [200]], ) sigma = Uniform([[5], [25]], ) # define the model from abcpy.continuousmodels import Normal height = Normal([mu, sigma], ) # define statistics from abcpy.statistics import Identity statistics_calculator = Identity(degree=3, cross=True) # Learn the optimal summary statistics using Semiautomatic summary selection from abcpy.summaryselections import Semiautomatic summary_selection = Semiautomatic([height], statistics_calculator, backend, n_samples=1000, n_samples_per_param=1, seed=1) # Redefine the statistics function statistics_calculator.statistics = lambda x, f2=summary_selection.transformation, \ f1=statistics_calculator.statistics: f2(f1(x)) # define distance from abcpy.distances import LogReg distance_calculator = LogReg(statistics_calculator) # define kernel from abcpy.perturbationkernel import DefaultKernel kernel = DefaultKernel([mu, sigma]) # define sampling scheme from abcpy.inferences import PMCABC sampler = PMCABC([height], [distance_calculator], backend, kernel, seed=1) # sample from scheme T, n_sample, n_samples_per_param = 3, 250, 10 eps_arr = np.array([.75]) epsilon_percentile = 10 journal = sampler.sample([height_obs], T, eps_arr, n_sample, n_samples_per_param, epsilon_percentile) return journal
def setup_backend(): global backend from abcpy.backends import BackendMPI as Backend backend = Backend(process_per_model=2)
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')