Ejemplo n.º 1
0
        def modified_experiment(grid_params, elfi_params, rl_params,
                                bolfi_params, obs_data, test_data, plot_data,
                                types, replicates, region_size, ground_truth,
                                n_cores, path_max_len, obs_set_size, seed, pdf,
                                figsize):
            elfi.new_model()
            model = get_model("approx", grid_params, p.get_elfi_params(),
                              rl_params, obs_data, path_max_len, obs_set_size)
            inference_task = BolfiFactory(model, bolfi_params).get()
            bounds = elfi_params.get_bounds()
            ret = dict()
            ret["n_cores"] = n_cores
            ret["MD"] = dict()
            random_state = np.random.RandomState(seed)
            for k, v in bounds.items():
                ret["MD"][k] = random_state.uniform(v[0], v[1])
            print("Random location: {}".format(ret["MD"]))
            ret["sampling_duration"] = 0
            ret["samples"] = dict()
            ret["n_samples"] = 0

            ret = PointEstimateSimulationPhase(replicates=replicates,
                                               region_size=region_size).run(
                                                   inference_task, ret)
            ret = PlottingPhase(pdf=pdf,
                                figsize=figsize,
                                obs_data=obs_data,
                                test_data=test_data,
                                plot_data=plot_data).run(inference_task, ret)
            ret = GroundTruthErrorPhase(ground_truth=ground_truth).run(
                inference_task, ret)
            ret = PredictionErrorPhase(test_data=test_data).run(
                inference_task, ret)
            return ret
Ejemplo n.º 2
0
def test_list_output():
    vsim = elfi.tools.vectorize(lsimulator)
    vsum = elfi.tools.vectorize(lsummary)

    v = vsim(np.array([[.2, .8], [.3, .7]]))
    assert is_array(v)
    assert not isinstance(v[0], list)

    vsim = elfi.tools.vectorize(lsimulator, dtype=False)

    v = vsim(np.array([[.2, .8], [.3, .7]]))
    assert is_array(v)
    assert isinstance(v[0], list)

    obs = lsimulator([.2, .8])

    elfi.new_model()
    p = elfi.Prior('dirichlet', [2, 2])
    sim = elfi.Simulator(vsim, p, observed=obs)
    S = elfi.Summary(vsum, sim)
    d = elfi.Distance('euclidean', S)

    pool = elfi.OutputPool(['sim'])
    rej = elfi.Rejection(d, batch_size=100, pool=pool, output_names=['sim'])
    sample = rej.sample(100, n_sim=1000)
    mean = np.mean(sample.samples['p'], axis=0)

    # Crude test
    assert mean[1] > mean[0]
Ejemplo n.º 3
0
def test_external_operation():
    # Note that the test string has intentionally not uniform formatting with spaces
    elfi.new_model()
    op = elfi.tools.external_operation('echo 1 {0} 4  5    6 {seed}')
    constant = elfi.Constant(123)
    simulator = elfi.Simulator(op, constant)
    v = simulator.generate(1)
    assert np.array_equal(v[:5], [1, 123, 4, 5, 6])

    # Can be pickled
    pickle.dumps(op)
 def predict(self, data_test):
     elfi.new_model("SMC")
     prior = elfi.Prior(MVUniform, self.p_lower, self.p_upper)
     sim = elfi.Simulator(self.simulator,
                          prior,
                          observed=data_test,
                          name='sim')
     SS = elfi.Summary(self.identity, sim, name='identity')
     d = elfi.Distance('euclidean', SS, name='d')
     smc = elfi.SMC(d, batch_size=1, seed=42)
     samples = smc.sample(self.n_particles, [self.threshold])
     return samples.samples_array
Ejemplo n.º 5
0
    def modified_experiment(grid_params, elfi_params, rl_params, bolfi_params,
                            obs_data, test_data, plot_data, types, replicates,
                            region_size, ground_truth, n_cores, path_max_len,
                            pdf, figsize):
        elfi.new_model()
        model = get_model(method, grid_params, p.get_elfi_params(), rl_params,
                          obs_data, path_max_len)
        inference_task = BolfiFactory(model, bolfi_params).get()
        ret = dict()
        ret["n_cores"] = n_cores

        ret = SamplingPhase().run(inference_task, ret)
        ret = PosteriorAnalysisPhase(types=types).run(inference_task, ret)
        return ret
Ejemplo n.º 6
0
def test_dict_output():
    vsim = elfi.tools.vectorize(simulator)
    vsum = elfi.tools.vectorize(summary)

    obs = simulator([.2, .8])

    elfi.new_model()
    p = elfi.Prior('dirichlet', [2, 2])
    sim = elfi.Simulator(vsim, p, observed=obs)
    S = elfi.Summary(vsum, sim)
    d = elfi.Distance('euclidean', S)

    pool = elfi.OutputPool(['sim'])
    rej = elfi.Rejection(d, batch_size=100, pool=pool, output_names=['sim'])
    sample = rej.sample(100, n_sim=1000)
    mean = np.mean(sample.samples['p'], axis=0)

    # Crude test
    assert mean[1] > mean[0]
 def __init__(self,
              generator=None,
              p_lower=None,
              p_upper=None,
              n_particles=100):
     self.p_lower = p_lower
     self.p_upper = p_upper
     self.n_particles = n_particles
     self.generator = generator
     self.model = elfi.new_model()
     self.threshold = 0.1  # %% acceptance threshold
 def predict(self, data_test):
     elfi.new_model("BOLFI")
     prior = elfi.Prior(MVUniform, self.p_lower, self.p_upper)
     sim = elfi.Simulator(self.simulator,
                          prior,
                          observed=data_test,
                          name='sim')
     SS = elfi.Summary(self.identity, sim, name='identity')
     d = elfi.Distance('euclidean', SS, name='d')
     log_d = elfi.Operation(np.log, d)
     bolfi = elfi.BOLFI(log_d,
                        batch_size=1,
                        initial_evidence=20,
                        update_interval=10,
                        acq_noise_var=self.p_lower.size * [0.1],
                        bounds=None,
                        seed=42)
     bolfi.fit(n_evidence=self.n_particles)
     post = bolfi.extract_posterior(-1.)
     samples = post.model.X
     return samples
Ejemplo n.º 9
0
def get_model(n_obs=150, true_params=None, seed=None):
    """Return an initialised bivariate g-and-k model.

    Parameters
    ----------
    n_obs : int, optional
        Number of the observations.
    true_params : array_like, optional
        Parameters defining the model.
    seed : np.random.RandomState, optional

    Returns
    -------
    elfi.ElfiModel

    """
    m = elfi.new_model()

    # Initialising the parameters as in Drovandi & Pettitt (2011).
    if true_params is None:
        true_params = [3, 4, 1, 0.5, 1, 2, .5, .4, 0.6]

    # Initialising the prior settings as in Drovandi & Pettitt (2011).
    priors = []
    priors.append(elfi.Prior('uniform', 0, 5, model=m, name='a1'))
    priors.append(elfi.Prior('uniform', 0, 5, model=m, name='a2'))
    priors.append(elfi.Prior('uniform', 0, 5, model=m, name='b1'))
    priors.append(elfi.Prior('uniform', 0, 5, model=m, name='b2'))
    priors.append(elfi.Prior('uniform', -5, 10, model=m, name='g1'))
    priors.append(elfi.Prior('uniform', -5, 10, model=m, name='g2'))
    priors.append(elfi.Prior('uniform', -.5, 5.5, model=m, name='k1'))
    priors.append(elfi.Prior('uniform', -.5, 5.5, model=m, name='k2'))
    EPS = np.finfo(float).eps
    priors.append(
        elfi.Prior('uniform', -1 + EPS, 2 - 2 * EPS, model=m, name='rho'))

    # Obtaining the observations.
    y_obs = BiGNK(*true_params,
                  n_obs=n_obs,
                  random_state=np.random.RandomState(seed))

    # Defining the simulator.
    fn_simulator = partial(BiGNK, n_obs=n_obs)
    elfi.Simulator(fn_simulator, *priors, observed=y_obs, name='BiGNK')

    # Initialising the default summary statistics.
    default_ss = elfi.Summary(ss_robust, m['BiGNK'], name='ss_robust')

    # Using the customEuclidean distance function designed for
    # the summary statistics of shape (batch_size, dim_ss, dim_ss_point).
    elfi.Discrepancy(euclidean_multiss, default_ss, name='d')
    return m
Ejemplo n.º 10
0
        def modified_experiment(grid_params, elfi_params, rl_params,
                                bolfi_params, obs_data, test_data, plot_data,
                                types, replicates, region_size, ground_truth,
                                n_cores, path_max_len, obs_set_size, pdf,
                                figsize):
            elfi.new_model()
            model = get_model(method, grid_params, p.get_elfi_params(),
                              rl_params, obs_data, path_max_len, obs_set_size)
            inference_task = BolfiFactory(model, bolfi_params).get()
            ret = dict()
            ret["n_cores"] = n_cores

            ret = SamplingPhase().run(inference_task, ret)
            ret = PosteriorAnalysisPhase(types=types).run(inference_task, ret)
            ret["plots_logl"] = inference_task.plot_post(pdf, figsize)

            elfi.new_model()
            model = get_model("approx", grid_params, p.get_elfi_params(),
                              rl_params, obs_data, path_max_len, obs_set_size)
            inference_task = BolfiFactory(model, bolfi_params).get()

            ret = PointEstimateSimulationPhase(replicates=replicates,
                                               region_size=region_size).run(
                                                   inference_task, ret)
            ret = LikelihoodSamplesSimulationPhase(replicates=replicates).run(
                inference_task, ret)
            ret = PlottingPhase(pdf=pdf,
                                figsize=figsize,
                                obs_data=obs_data,
                                test_data=test_data,
                                plot_data=plot_data).run(inference_task, ret)
            ret = GroundTruthErrorPhase(ground_truth=ground_truth).run(
                inference_task, ret)
            ret = PredictionErrorPhase(test_data=test_data).run(
                inference_task, ret)
            return ret
Ejemplo n.º 11
0
def get_model(n_obs=50, true_params=None, seed=None):
    """Initialise the g-and-k model.

    Parameters
    ----------
    n_obs : int, optional
        Number of the observations.
    true_params : array_like, optional
        Parameters defining the model.
    seed : np.random.RandomState, optional

    Returns
    -------
    elfi.ElfiModel

    """
    m = elfi.new_model()

    # Initialising the parameters as in Allingham et al. (2009).
    if true_params is None:
        true_params = [3, 1, 2, .5]

    # Initialising the prior settings as in Allingham et al. (2009).
    priors = []
    priors.append(elfi.Prior('uniform', 0, 10, model=m, name='A'))
    priors.append(elfi.Prior('uniform', 0, 10, model=m, name='B'))
    priors.append(elfi.Prior('uniform', 0, 10, model=m, name='g'))
    priors.append(elfi.Prior('uniform', 0, 10, model=m, name='k'))

    # Obtaining the observations.
    y_obs = GNK(*true_params,
                n_obs=n_obs,
                random_state=np.random.RandomState(seed))

    # Defining the simulator.
    fn_simulator = partial(GNK, n_obs=n_obs)
    elfi.Simulator(fn_simulator, *priors, observed=y_obs, name='GNK')

    # Initialising the summary statistics as in Allingham et al. (2009).
    default_ss = elfi.Summary(ss_order, m['GNK'], name='ss_order')

    # Using the multi-dimensional Euclidean distance function as
    # the summary statistics' implementations are designed for multi-dimensional cases.
    elfi.Discrepancy(euclidean_multiss, default_ss, name='d')
    return m
Ejemplo n.º 12
0
    def get_model(self, n_obs=100, true_params=None, seed_obs=None):
        m = elfi.new_model()

        elfi.Prior('uniform', -2, 4, name='x')
        elfi.Prior('uniform', -2, 4, name='y')
        params = [m['x'], m['y']]

        if true_params is None:
            true_params = [[1.5, 1]]
        y_obs = self.func(*true_params, random_state=np.random.RandomState(seed_obs))
        y_obs = [np.mean(y_obs), np.std(y_obs)]

        elfi.Simulator(self.func, *params, observed=y_obs, name='sim')
        # elfi.Summary(partial(np.mean, axis=1), m['sim'], name='Mean')
        # elfi.Summary(partial(np.std, axis=1), m['sim'], name='Std')
        elfi.Distance('euclidean', m['sim'], name='dist')

        return m
Ejemplo n.º 13
0
    def get_model(self, n_obs=10, true_params=None, seed_obs=None):
        m = elfi.new_model()

        # Parameters: ns, kc, alpha, r_star, As
        # priors from Sinha and Souradeep 2006.
        elfi.Prior('uniform', 0.5, 1.0, name='ns')
        elfi.Prior('uniform', 1e-7, 1e-3, name='kc')
        elfi.Prior('uniform', 0., 10., name='alpha')
        elfi.Prior('uniform', 0., 1., name='r_star')
        elfi.Prior('uniform', 2.7, 1.3, name='As')
        params = [m['ns'], m['kc'], m['alpha'], m['r_star'], m['As']]

        if true_params is None:
            true_params = [[0.96, 0.0003, 0.58, 0.75, 3.35]]
        y_obs = self.func(*true_params,
                          random_state=np.random.RandomState(seed_obs))

        elfi.Simulator(self.func, *params, observed=y_obs, name='sim')
        elfi.Distance('euclidean', m['sim'], name='dist')
        return m
Ejemplo n.º 14
0
def get_model(n_obs=50,
              true_params=None,
              seed_obs=None,
              nd_mean=False,
              cov_matrix=None):
    """Return a Gaussian noise model.

    Parameters
    ----------
    n_obs : int, optional
    true_params : list, optional
        Default parameter settings.
    seed_obs : int, optional
        Seed for the observed data generation.
    nd_mean : bool, optional
        Option to use an n-D mean Gaussian noise model.
    cov_matrix : array_like, optional
        Covariance matrix, a requirement for the nd_mean model.

    Returns
    -------
    elfi.ElfiModel

    """
    # Defining the default settings.
    if true_params is None:
        if nd_mean:
            true_params = [4, 4]  # 2-D mean.
        else:
            true_params = [4, .4]  # mean and standard deviation.

    # Choosing the simulator for both observations and simulations.
    if nd_mean:
        fn_simulator = partial(gauss_nd_mean,
                               cov_matrix=cov_matrix,
                               n_obs=n_obs)
    else:
        fn_simulator = partial(gauss, n_obs=n_obs)

    # Obtaining the observations.
    y_obs = fn_simulator(*true_params,
                         n_obs=n_obs,
                         random_state=np.random.RandomState(seed_obs))

    m = elfi.new_model()
    # Initialising the priors.
    eps_prior = 5  # The longest distance from the median of an initialised prior's distribution.
    priors = []
    if nd_mean:
        n_dim = len(true_params)
        for i in range(n_dim):
            name_prior = 'mu_{}'.format(i)
            prior_mu = elfi.Prior('uniform',
                                  true_params[i] - eps_prior,
                                  2 * eps_prior,
                                  model=m,
                                  name=name_prior)
            priors.append(prior_mu)
    else:
        priors.append(
            elfi.Prior('uniform',
                       true_params[0] - eps_prior,
                       2 * eps_prior,
                       model=m,
                       name='mu'))
        priors.append(
            elfi.Prior('truncnorm',
                       np.amax([.01, true_params[1] - eps_prior]),
                       2 * eps_prior,
                       model=m,
                       name='sigma'))
    elfi.Simulator(fn_simulator, *priors, observed=y_obs, name='gauss')

    # Initialising the summary statistics.
    sumstats = []
    sumstats.append(elfi.Summary(ss_mean, m['gauss'], name='ss_mean'))
    sumstats.append(elfi.Summary(ss_var, m['gauss'], name='ss_var'))

    # Choosing the discrepancy metric.
    if nd_mean:
        elfi.Discrepancy(euclidean_multidim, *sumstats, name='d')
    else:
        elfi.Distance('euclidean', *sumstats, name='d')
    return m
Ejemplo n.º 15
0
def test_romc2():
    """Test ROMC at the simple 1D example."""

    # the prior distribution
    class Prior:
        def rvs(self, size=None, random_state=None):
            # size from (BS,) -> (BS,1)
            if size is not None:
                size = np.concatenate((size, [1]))
            return ss.uniform(loc=-2.5, scale=5).rvs(size=size,
                                                     random_state=random_state)

        def pdf(self, theta):
            return ss.uniform(loc=-2.5, scale=5).pdf(theta)

        def logpdf(self, theta):
            return ss.uniform(loc=-2.5, scale=5).logpdf(theta)

    # function for sampling from the likelihood
    def likelihood_sample(theta, seed=None):
        """Vectorized sampling from likelihood."""
        assert isinstance(theta, np.ndarray)
        theta = theta.astype(np.float)
        samples = np.empty_like(theta)

        c = 0.5 - 0.5**4

        tmp_theta = theta[theta <= -0.5]
        samples[theta <= -0.5] = ss.norm(loc=-tmp_theta - c,
                                         scale=1).rvs(random_state=seed)
        theta[theta <= -0.5] = np.inf

        tmp_theta = theta[theta <= 0.5]
        samples[theta <= 0.5] = ss.norm(loc=tmp_theta**4,
                                        scale=1).rvs(random_state=seed)
        theta[theta <= 0.5] = np.inf

        tmp_theta = theta[theta < np.inf]
        samples[theta < np.inf] = ss.norm(loc=tmp_theta - c,
                                          scale=1).rvs(random_state=seed)
        theta[theta < np.inf] = np.inf

        assert np.allclose(theta, np.inf)
        return samples

    # define the simulator
    def simulator(theta, dim, batch_size=10000, random_state=None):
        theta = np.repeat(theta, dim, -1)
        return likelihood_sample(theta, seed=random_state)

    data = np.array([0.])
    dim = data.shape[0]

    # Define ELFI model
    elfi.new_model("1D_example")
    elfi_prior = elfi.Prior(Prior(), name="theta")
    elfi_simulator = elfi.Simulator(simulator,
                                    elfi_prior,
                                    dim,
                                    observed=np.expand_dims(data, 0),
                                    name="simulator")
    dist = elfi.Distance('euclidean', elfi_simulator, name="dist")

    # Define ROMC inference method
    bounds = [(-2.5, 2.5)]
    romc = elfi.ROMC(dist, bounds)

    # Bayesian Optimisation solution part
    n1 = 50
    seed = 21
    optimizer_args = {}
    use_bo = True
    romc.solve_problems(n1=n1,
                        seed=seed,
                        use_bo=use_bo,
                        optimizer_args=optimizer_args)

    eps_filter = .75
    fit_models = True
    use_surrogate = True
    fit_models_args = {"nof_points": 30}
    romc.estimate_regions(eps_filter=eps_filter,
                          use_surrogate=use_surrogate,
                          fit_models=fit_models,
                          fit_models_args=fit_models_args)

    n2 = 100
    romc.sample(n2=n2)

    # assert summary statistics of samples match the ground truth
    assert np.allclose(romc.compute_expectation(h=lambda x: np.squeeze(x)),
                       0,
                       atol=.4)
    assert np.allclose(romc.compute_expectation(h=lambda x: np.squeeze(x)**2),
                       1.1,
                       atol=.4)
Ejemplo n.º 16
0
def kisdi_model(population_filename):
    # (cumulative) deaths every day from 1st of march
    deaths = [0]*20 + [1, 1, 1, 1, 3, 4, 7, 9, 11, 13, 17, 17, 19, 20, 25, 27,
            27, 34, 40, 42, 47, 49, 56, 59, 64, 72, 75]

    # In ICU
    # hospitalized = [np.nan]*24 + [22, 24, 32, 31, 41, 49, 56, 62, 65, 72, 73,
    #         76, 81, 83, 82, 82, 81, 80, 77, 74, 75, 75, 76]

    # total hospitalized
    hospitalized = [np.nan]*24 + [82, 96, 108, 112, 134, 143, 137, 159, 160,
            180, 187, 209, 228, 231, 239, 244, 236, 235, 235, 230, 232, 226,
            215]

    observed = np.array([list(zip(deaths, hospitalized))])

    model = elfi.new_model()
    areas = set()
    with open(population_filename, newline='') as f:
        for row in csv.DictReader(f):
            areas.add(row['Area'])

    univariate_params = {
            'beta_presymptomatic':  stats.uniform(0, 1),
            'beta_asymptomatic':    stats.uniform(0, 1),
            'beta_infected':        stats.uniform(0, 1),
            'pi':                   stats.beta(4, 6),
            'kappa':                stats.beta(2, 8),
            'reciprocal_eta':       stats.gamma(2.34,  5.0),
            'reciprocal_alpha':     stats.gamma(2.,  5.0),
            'reciprocal_theta':     stats.gamma(8.0,  5.0),
            'reciprocal_nu':        stats.gamma(2.86, 5.0),
            'reciprocal_rho':       stats.gamma(5.0,  5.0),
            'reciprocal_chi':       stats.gamma(10.0, 5.0),
            'reciprocal_delta':     stats.gamma(7.0, 5.0),
    }

    multivar_params = {
            'contact_y': stats.dirichlet([1, 1, 1]),
            'contact_m': stats.dirichlet([1, 1, 1]),
            'contact_o': stats.dirichlet([1, 1, 1]),
    }


    parameters = {}
    for k, v in itertools.chain(
            univariate_params.items(),
            multivar_params.items()):
        parameters[k] = elfi.Prior(v, name=k, model=model)

    initial_condition_parameters = {}
    for area in list(areas):
        for compartment in ['Exposed', 'Presymptomatic',
                'Asymptomatic', 'Infected']:
            for age_group in ['Young', 'Adults', 'Elderly']:
                key = (area, compartment, age_group)
                initial_condition_parameters[key] = \
                        elfi.Constant(1, name=' '.join(key),
                                model=model)
                        # elfi.Prior(stats.uniform(0, 1000), name=' '.join(key),
                        #         model=model)


    sim_fun = elfi.tools.vectorize(simulate, constants=[0, 1, 2, 3, 4],
            dtype=np.float_)
    sim = elfi.Simulator(sim_fun,
            population_filename, list(areas), observed.shape[1],
            list(parameters), list(initial_condition_parameters),
            *parameters.values(), *initial_condition_parameters.values(),
            observed = observed)

    deaths = elfi.Summary(deaths_column, sim, model=model)
    hospitalized = elfi.Summary(hospitalized_column, sim, model=model)

    dist = elfi.Discrepancy(time_series_discrepancy,
            deaths, hospitalized, model=model)

    return model, dist,\
            univariate_params, multivar_params, initial_condition_parameters
Ejemplo n.º 17
0
 def teardown_method(self, method):
     """Refresh ELFI after the execution of the test class's each method."""
     elfi.new_model()
Ejemplo n.º 18
0
likelihood = Likelihood()
prior = Prior()

factor = create_factor(x=data)
Z = approximate_Z(factor, a, b)
gt_posterior_pdf = create_gt_posterior(likelihood, prior, data, Z)


############# DEFINE ELFI MODEL ##################
def simulator(theta, dim, batch_size=10000, random_state=None):
    likelihood = Likelihood()
    theta = np.repeat(theta, dim, -1)
    return likelihood.rvs(theta, seed=random_state)


elfi.new_model("1D_example")
elfi_prior = elfi.Prior(Prior(), name="theta")
elfi_simulator = elfi.Simulator(simulator,
                                elfi_prior,
                                dim,
                                observed=np.expand_dims(data, 0),
                                name="simulator")
dist = elfi.Distance('euclidean', elfi_simulator, name="dist")

bounds = [(-2.5, 2.5)]
dim = data.shape[-1]

# Defines the ROMC inference method
romc = elfi.ROMC(dist, bounds)
romc1 = elfi.ROMC(dist, bounds)
romc2 = elfi.ROMC(dist, bounds)
def start_all(variable_name, variable_num, quantile, f):
    f.write('{0} {1}\n'.format(variable_name, quantile))
    seed = 20170530
    np.random.seed(seed)

    # We set true parameters
    true_values = {
        'p': 0.005,
        'k': 1,
        's': 0.0005,
        'xC': 100,
        'xE': 25,
        'y': 100,
        'yy': 0.01,
        'n': 100,
        'w': 100,
        'd': 0.00005,
        'bC': 0.03,
        'bE': 0.03,
        'am': 0.01,
        'aM': 0.07
    }

    number_of_samples = int(1 / sampling)

    full_range = np.arange(start, stop, sampling)
    all_x_range = np.array(full_range).reshape((1, len(full_range)))

    first_x_data, second_x_data, third_x_data = get_three_ranges(
        start, first_x_range, second_x_range, third_x_range, number_of_samples,
        full_range)

    pred_prey_model = ImprovedHandyModel()
    pred_prey_model.variable_num = variable_num

    # Plot the observed sequence for whole range
    y_obs = pred_prey_model.calculate_model(true_values, all_x_range)
    plt.figure(figsize=(11, 6))
    plt.plot(all_x_range[0, :], y_obs[0, :])

    # Points between these lines are training points
    plt.axvline(x=first_x_range, color='r')
    plt.axvline(x=second_x_range, color='r')

    plt.xlabel('X value as an argument for model')
    plt.ylabel('Y value of the model')
    plt.savefig('{0}_{1}_all.png'.format(variable_name, quantile))
    plt.close()

    # We plot only training part
    train_data = pred_prey_model.calculate_model(true_values, second_x_data)
    plt.figure(figsize=(11, 6))

    plt.xticks(np.arange(first_x_range, second_x_range, 1.0))

    plt.plot(second_x_data[0, :], train_data[0, :])
    plt.xlabel('X value as an argument for function')
    plt.ylabel('Y value of the function')
    plt.savefig('{0}_{1}_part.png'.format(variable_name, quantile))
    plt.close()

    # MAGIC
    pred_prey_model.second_x_data = second_x_data
    elfi.new_model()
    # has to be this way, so the keys in result dict have parameter names
    xC = elfi.Prior(scipy.stats.uniform,
                    true_values['xC'] - width * true_values['xC'],
                    2 * width * true_values['xC'])
    xE = elfi.Prior(scipy.stats.uniform,
                    true_values['xE'] - width * true_values['xE'],
                    2 * width * true_values['xE'])
    y = elfi.Prior(scipy.stats.uniform,
                   true_values['y'] - width * true_values['y'],
                   2 * width * true_values['y'])
    w = elfi.Prior(scipy.stats.uniform,
                   true_values['w'] - width * true_values['w'],
                   2 * width * true_values['w'])
    k = elfi.Prior(scipy.stats.uniform,
                   true_values['k'] - width * true_values['k'],
                   2 * width * true_values['k'])
    s = elfi.Prior(scipy.stats.uniform,
                   true_values['s'] - width * true_values['s'],
                   2 * width * true_values['s'])
    p = elfi.Prior(scipy.stats.uniform,
                   true_values['p'] - width * true_values['p'],
                   2 * width * true_values['p'])
    yy = elfi.Prior(scipy.stats.uniform,
                    true_values['yy'] - width * true_values['yy'],
                    2 * width * true_values['yy'])
    n = elfi.Prior(scipy.stats.uniform,
                   true_values['n'] - width * true_values['n'],
                   2 * width * true_values['n'])
    d = elfi.Prior(scipy.stats.uniform,
                   true_values['d'] - width * true_values['d'],
                   2 * width * true_values['d'])
    bC = elfi.Prior(scipy.stats.uniform,
                    true_values['bC'] - width * true_values['bC'],
                    2 * width * true_values['bC'])
    bE = elfi.Prior(scipy.stats.uniform,
                    true_values['bE'] - width * true_values['bE'],
                    2 * width * true_values['bE'])
    am = elfi.Prior(scipy.stats.uniform,
                    true_values['am'] - width * true_values['am'],
                    2 * width * true_values['am'])
    aM = elfi.Prior(scipy.stats.uniform,
                    true_values['aM'] - width * true_values['aM'],
                    2 * width * true_values['aM'])

    # Define the simulator node with the MA2 model ,give the priors to it as arguments.
    Y = elfi.Simulator(pred_prey_model.model,
                       p,
                       k,
                       s,
                       xC,
                       xE,
                       y,
                       yy,
                       n,
                       d,
                       w,
                       bC,
                       bE,
                       am,
                       aM,
                       observed=train_data)

    # Autocovariances as the summary statistics
    def autocov(x, lag=1):
        c = np.mean(x[:, lag:] * x[:, :-lag], axis=1)
        return c

    # Summary node is defined by giving the autocovariance function and the simulated data (also includes observed data)
    S1 = elfi.Summary(autocov, Y)
    S2 = elfi.Summary(autocov, Y, 2)

    # Calculating the squared distance (S1_sim-S1_obs)**2 + (S2_sim-S2_obs)**2
    d = elfi.Distance('euclidean', S1, S2)

    # Instantiation of the Rejection Algorithm
    rej = elfi.Rejection(d, batch_size=batch_size_c, seed=seed)

    result = rej.sample(N, quantile=quantile)
    # Print sampled means of parameters

    print(result)

    # Final result of mean samples
    mean_results = {k: v.mean() for k, v in result.samples.items()}

    for key, value in mean_results.items():
        print('{0}: {1}'.format(key, value))
        f.write('{0}: {1}\n'.format(key, value))

    y_obs = pred_prey_model.calculate_model(true_values, second_x_data)
    plt.figure(figsize=(11, 6))
    plt.plot(y_obs.ravel(), label="observed")
    plt.plot(pred_prey_model.calculate_model(mean_results,
                                             second_x_data).ravel(),
             label="simulated")
    plt.legend(loc="upper left")
    plt.savefig('{0}_{1}_final_part.png'.format(variable_name, quantile))
    plt.close()

    y_obs = pred_prey_model.calculate_model(true_values, all_x_range)
    plt.figure(figsize=(11, 6))
    plt.plot(y_obs.ravel(), label="observed")
    all_results_predicted = pred_prey_model.calculate_model(
        mean_results, all_x_range)
    plt.plot(all_results_predicted.ravel(), label="simulated")
    plt.legend(loc="upper left")
    plt.savefig('{0}_{1}_final_all.png'.format(variable_name, quantile))
    plt.close()

    def calculate_error(start, stop):
        calculate = 0
        for i in range(start, stop, 1):
            calculate += (y_obs[0][i] - all_results_predicted[0][i])**2
        return calculate

    aa = calculate_error(int(start + 1),
                         int(first_x_range * number_of_samples))
    bb = calculate_error(int(first_x_range * number_of_samples),
                         int(second_x_range * number_of_samples))
    cc = calculate_error(int(second_x_range * number_of_samples),
                         int(third_x_range * number_of_samples))
    f.write('{0}\n{1}\n{2}\n\n'.format(aa, bb, cc))
Ejemplo n.º 20
0
 def setup_class(cls):
     """Refresh ELFI upon initialising the test class."""
     elfi.new_model()
Ejemplo n.º 21
0
    def get_model(self, n_obs=100, true_params=None, seed_obs=None):
        """Return a complete model in inference task.

        Parameters
        ----------
        n_obs : int, optional
            observation length of the MA2 process
        true_params : list, optional
            parameters with which the observed data is generated
        seed_obs : int, optional
            seed for the observed data generation

        Returns
        -------
        m : elfi.ElfiModel

        """
        
        m = elfi.new_model()
        burden = elfi.Prior('normal', 200, 30, name='burden')

        joint = elfi.RandomVariable(ops.JointPrior, burden, self.mean_obs_bounds,
                                    self.t1_bound, self.a1_bound)

        # DummyPrior takes a marginal from the joint prior
        R2 = elfi.Prior(ops.DummyPrior, joint, 0, name='R2')
        R1 = elfi.Prior(ops.DummyPrior, joint, 1, name='R1')
        t1 = elfi.Prior(ops.DummyPrior, joint, 2, name='t1')

        # Turn the epidemiological parameters to rate parameters for the simulator
        d1 = elfi.Operation(ops.Rt_to_d, R1, t1)
        d2 = 5.95
        a2 = elfi.Operation(operator.mul, R2, d2)
        a1 = elfi.Operation(ops.Rt_to_a, R1, t1)

        if true_params is None:
            y0_burden = 192
            y0_R2 = 0.09
            y0_R1 = 5.88
            y0_t1 = 6.74

            y0_d1 = ops.Rt_to_d(y0_R1, y0_t1)
            y0_a2 = operator.mul(y0_R2, d2)
            y0_a1 = ops.Rt_to_a(y0_R1, y0_t1)
            self.y0 = ops.simulator(y0_burden, y0_a2, d2, y0_a1, y0_d1, 2,
                                    self.cluster_size_bound, self.warmup_bounds)

            self.y0_sum = [self.y0['n_obs'], self.y0['n_clusters'], self.y0['largest'], self.y0['clusters'], self.y0['obs_times']]

        # Add the simulator
        sim = elfi.Simulator(ops.simulator, burden, a2, d2, a1, d1, 2,
                             self.cluster_size_bound, self.warmup_bounds, observed=self.y0)

        # Summaries extracted from the simulator output
        n_obs = elfi.Summary(ops.pick, sim, 'n_obs')
        n_clusters = elfi.Summary(ops.pick, sim, 'n_clusters')
        largest = elfi.Summary(ops.pick, sim, 'largest')
        clusters = elfi.Summary(ops.pick, sim, 'clusters')
        obs_times = elfi.Summary(ops.pick, sim, 'obs_times')

        sim = elfi.Operation(ops.distance, n_obs, n_clusters, largest, clusters,
                                obs_times, self.y0_sum, name = 'sim')

        # Distance
        dist = elfi.Discrepancy(ops.distance, n_obs, n_clusters, largest, clusters,
                                obs_times, name = 'dist')
        return m