def infer_parameters_smcabc():
    # 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 SMCABC
    sampler = SMCABC([height_weight_model], [distance_calculator], backend, seed=1)
    steps, n_samples, n_samples_per_param, epsilon = 2, 10, 1, 2000
    print('SMCABC Inferring')
    journal = sampler.sample([y_obs], steps, n_samples, n_samples_per_param, epsilon, full_output=1)

    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
示例#3
0
    def test_sample(self):
        # use the SMCABC scheme for T = 1
        steps, n_sample, n_simulate = 1, 10, 1
        sampler = SMCABC(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 SMCABC scheme for T = 2
        T, n_sample, n_simulate = 2, 10, 1
        sampler = SMCABC(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 - (-1.12595029091), 10e-2)
        self.assertLess(sigma_post_mean - 4.62512055437, 10e-2)
示例#4
0
    def test_sample(self):
        # use the SMCABC scheme for T = 1
        steps, n_sample, n_simulate = 1, 10, 1
        sampler = SMCABC([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.assertEqual((mu_post_mean, sigma_post_mean), (,))

        # use the SMCABC scheme for T = 2
        T, n_sample, n_simulate = 2, 10, 1
        sampler = SMCABC([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 - (-0.786118677019), 10e-2)
        self.assertLess(sigma_post_mean - 4.63324738665, 10e-2)

        self.assertFalse(journal.number_of_simulations == 0)
示例#5
0
def infer_parameters(backend,
                     scheme='rejection',
                     n_samples=250,
                     n_samples_per_param=10,
                     logging_level=logging.WARN):
    """Perform inference for this example.
    Parameters
    ----------
    backend
        The parallelization backend
    steps : integer, optional
        Number of iterations in the sequential PMCABC algoritm ("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)

    # experimental setup
    T = 50.  # simulation time
    dt = 0.025  # time step
    I_amp = 0.32  # stimulus amplitude
    r_soma = 40  # radius of soma
    threshold = -55  # AP threshold

    # input stimulus
    stimulus_dict = constant_stimulus(I_amp=I_amp,
                                      T=T,
                                      dt=dt,
                                      t_stim_on=10,
                                      t_stim_off=40,
                                      r_soma=r_soma)
    I = stimulus_dict["I"]
    #I_stim = stimulus_dict["I_stim"]

    # true parameters
    gbar_K_true = 36
    gbar_Na_true = 120

    gbar_K_std = 5
    gbar_Na_std = 5

    # define priors
    gbar_K = Normal([[gbar_K_true], [gbar_K_std]], name='gbar_K')
    gbar_Na = Normal([[gbar_Na_true], [gbar_Na_std]], name='gbar_Na')

    # define the model
    hh_simulator = HHSimulator([gbar_K, gbar_Na], I, T, dt)

    # observed data
    obs_data = hh_simulator.forward_simulate([gbar_K_true, gbar_Na_true])

    # define statistics
    statistics_calculator = Identity()

    # Learn the optimal summary statistics using Semiautomatic summary selection
    statistics_learning = Semiautomatic([hh_simulator],
                                        statistics_calculator,
                                        backend,
                                        n_samples=1000,
                                        n_samples_per_param=1,
                                        seed=42)
    new_statistics_calculator = statistics_learning.get_statistics()

    # define distance
    distance_calculator = Euclidean(new_statistics_calculator)

    # define kernel
    kernel = DefaultKernel([gbar_K, gbar_Na])

    # define sampling scheme
    if scheme == 'rejection':
        sampler = RejectionABC([hh_simulator], [distance_calculator],
                               backend,
                               seed=42)
        # sample from scheme
        epsilon = 2.
        journal = sampler.sample([obs_data], n_samples, n_samples_per_param,
                                 epsilon)

    elif scheme == 'smc':
        sampler = SMCABC([hh_simulator], [distance_calculator],
                         backend,
                         kernel,
                         seed=42)
        # sample from scheme
        steps = 3
        journal = sampler.sample([obs_data], steps, n_samples,
                                 n_samples_per_param)
    elif scheme == 'pmc':
        sampler = PMCABC([hh_simulator], [distance_calculator],
                         backend,
                         kernel,
                         seed=42)
        # sample from scheme
        steps = 3
        eps_arr = np.array([2.])
        epsilon_percentile = 10
        journal = sampler.sample([obs_data], steps, eps_arr, n_samples,
                                 n_samples_per_param, epsilon_percentile)

    return journal
示例#6
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