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
def ABC_inference(algorithm, model, observation, distance_calculator, eps, n_samples, n_steps, backend, seed=None,
                  full_output=0, kernel=None, **kwargs):
    start = time()
    if algorithm == "PMCABC":
        sampler = PMCABC([model], [distance_calculator], backend, seed=seed, kernel=kernel)
        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, kernel=kernel)
        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, kernel=kernel)
        jrnl = sampler.sample([[observation]], n_steps, eps, n_samples=n_samples, full_output=full_output, **kwargs)
    elif algorithm == "SMCABC":
        sampler = SMCABC([model], [distance_calculator], backend, seed=seed, kernel=kernel)
        jrnl = sampler.sample([[observation]], n_steps, n_samples=n_samples, full_output=full_output, **kwargs)
    elif algorithm == "RejectionABC":
        sampler = RejectionABC([model], [distance_calculator], backend, seed=seed)
        jrnl = sampler.sample([[observation]], epsilon=eps, n_samples=n_samples, n_samples_per_param=1,
                              full_output=full_output, **kwargs)
    elif algorithm == "ABCsubsim":
        sampler = ABCsubsim([model], [distance_calculator], backend, seed=seed, kernel=kernel)
        # chain_length=10, ap_change_cutoff=10
        jrnl = sampler.sample([[observation]], steps=n_steps, n_samples=n_samples, n_samples_per_param=1,
                              full_output=full_output, **kwargs)

    print("It took ", time() - start, " seconds.")
    return jrnl
Esempio n. 3
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.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)
Esempio n. 4
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,
                                 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)
Esempio n. 5
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)
Esempio n. 6
0
# 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')

######### 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)
Esempio n. 7
0
# DistanceType3 is a measure which determine how close are the peaks both in maximum size and in location
# DistanceType4 is the Euclidean distance between pedestrian position at each time step
from Distance import DistanceType1
distance_calculator = DistanceType1(statistics_calculator)

# 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
# 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')
Esempio n. 9
0
                                      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(dircertory + "/" + algorithm + '_' + problem +
                          '_obs.jrnl')
    elif algorithm == 'apmcabc':
        #APMCABC
        from abcpy.inferences import APMCABC
        sampler = APMCABC([Bass], [distance_calculator],
                          backend,
                          kernel,
                          seed=1)
        steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file = 10, 111, 1, 0.6, 0.001, 2, 1, None
        print('APMCABC Inferring')
        journal_apmcabc = sampler.sample([obs_data], steps, n_samples,
                                         n_samples_per_param, alpha,
                                         acceptance_cutoff, covFactor,
                                         full_output, journal_file)
        print(journal_apmcabc.posterior_mean())
        journal_apmcabc.save('apmcabc_' + problem + '_obs.jrnl')

if predict:
    # from model import DrawFromPosteriorFile
    # dp = DrawFromPosteriorFile([Bass], backend)
    # print('Prediction')
    # parameters, simulations = dp.sample(file=dircertory + "/"+'rejection_one_pcent.csv')
Esempio n. 10
0
#distance_calculator = DistanceType4()
distance_calculator = DistanceWeighted()

print(distance_calculator.distance(resultfakeobs1, resultfakeobs2))

# # 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, 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':
Esempio n. 11
0
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