Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
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)     
Ejemplo n.º 3
0
def infer_parameters_sabc():
    # 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 SABC
    sampler = SABC([height_weight_model], [distance_calculator], backend, seed=1)
    steps, epsilon, n_samples, n_samples_per_param, beta, delta, v = 2, 40000, 10, 1, 2, 0.2, 0.3
    ar_cutoff, resample, n_update, full_output = 0.1, None, None, 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, full_output)

    return journal
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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')
Ejemplo n.º 7
0
        # # 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:
Ejemplo n.º 8
0
# 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')
Ejemplo n.º 9
0
        # Define Distance functions
        from abcpy.distances import Euclidean
        dist_calc_seminn = Euclidean(learned_seminn_stat_loaded)
        dist_calc_triplet = Euclidean(learned_triplet_stat_loaded)
        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)
        print(dist_calc_mult.distance(obsdata, obsdata))

        # print('Inference starting')
        # # ## SABC - SemiNN##
        from abcpy.inferences import SABC
        print('Inference using Semi NN')
        sampler = SABC([PD], [dist_calc_seminn], backend, kernel, seed=1)
        steps, epsilon, n_samples, n_samples_per_param, ar_cutoff, full_output, journal_file = 25, 10e20, 511, 1, 0.001, 1, None
        print('SABC Inferring')
        journal_sabc = sampler.sample(observations=[obsdata], 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())
        if fake:
            journal_sabc.save('Data/Journals/sabc_obs_fake_seminn.jrnl')
        else:
            journal_sabc.save('Data/Journals/sabc_obs_' + str(whichobs) + '_seminn.jrnl')
        ## SABC - Triplet##
        print('Inference using Triplet Loss')
        sampler = SABC([PD], [dist_calc_triplet], backend, kernel, seed=1)
        steps, epsilon, n_samples, n_samples_per_param, ar_cutoff, full_output, journal_file = 10, 10e20, 511, 1, 0.001, 1, None
Ejemplo n.º 10
0
#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())
Ejemplo 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