Beispiel #1
0
def get_model(n_obs=50, true_params=None, seed_obs=None):
    """Return a complete Gaussian noise model.

    Parameters
    ----------
    n_obs : int, optional
        the number of observations
    true_params : list, optional
        true_params[0] corresponds to the mean,
        true_params[1] corresponds to the standard deviation
    seed_obs : int, optional
        seed for the observed data generation

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

    """
    if true_params is None:
        true_params = [10, 2]

    y_obs = Gauss(*true_params,
                  n_obs=n_obs,
                  random_state=np.random.RandomState(seed_obs))
    sim_fn = partial(Gauss, n_obs=n_obs)

    m = elfi.ElfiModel()
    elfi.Prior('uniform', -10, 50, model=m, name='mu')
    elfi.Prior('truncnorm', 0.01, 5, model=m, name='sigma')
    elfi.Simulator(sim_fn, m['mu'], m['sigma'], observed=y_obs, name='Gauss')
    elfi.Summary(ss_mean, m['Gauss'], name='S1')
    elfi.Summary(ss_var, m['Gauss'], name='S2')
    elfi.Distance('euclidean', m['S1'], m['S2'], name='d')

    return m
Beispiel #2
0
def inference_task(n_obs=100, true_params=None, seed_obs=12345):
    """Returns a complete MA2 model in inference task

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

    Returns
    -------
    InferenceTask
    """
    if true_params is None:
        true_params = [.6, .2]
    if len(true_params) != 2:
        raise ValueError("Invalid length of params_obs. Should be 2.")

    y = MA2(n_obs, *true_params, random_state=np.random.RandomState(seed_obs))
    sim = partial(MA2, n_obs)
    itask = elfi.InferenceTask()
    t1 = elfi.Prior('t1', 'uniform', 0, 1, inference_task=itask)
    t2 = elfi.Prior('t2', 'uniform', 0, 1, inference_task=itask)
    Y = elfi.Simulator('MA2', sim, t1, t2, observed=y, inference_task=itask)
    S1 = elfi.Summary('S1', autocov, Y, inference_task=itask)
    S2 = elfi.Summary('S2', autocov, Y, 2, inference_task=itask)
    d = elfi.Discrepancy('d', discrepancy, S1, S2, inference_task=itask)
    itask.parameters = [t1, t2]
    return itask
def create_model(shell_command):

    m = elfi.ElfiModel(name='nfds')  #creates model

    # shell_command = './updated_nfds_model/freqDepSelect -f input_files/mass/mass.input -c rmlB -v {0} -u 0.95 -n 50000 -s {1} -g 73 -t 1 -l 0.05 -i {2} -p f -o temp_output | perl -lane "print $F[2];"'
    nfds_sim = elfi.tools.external_operation(
        shell_command, stdout=True)  #wraps command in a python function
    nfds_sim_vector = elfi.tools.vectorize(nfds_sim)  # vectorizes command

    # m = elfi.ElfiModel(name = 'nfds') #creates model

    #Defines 3 priors for the model
    elfi.Prior('uniform', 0, 1, model=m, name='t1')
    elfi.Prior('uniform', 0, 0.05, model=m, name='t2')
    elfi.Prior('uniform', 0, 1, model=m, name='t3')

    # create central node that runs the model, I think
    nfds_simulator = elfi.Simulator(nfds_sim_vector,
                                    m['t1'],
                                    m['t2'],
                                    m['t3'],
                                    name='nfds',
                                    observed=0.0)

    return (nfds_simulator)
Beispiel #4
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.ElfiModel()
        if true_params is None:
            elfi.Prior('uniform', -20.0, 20.0, model=m, name='white')
            elfi.Prior('uniform', -20.0, 20.0, model=m, name='yellow')
            elfi.Prior('uniform', -20.0, 20.0, model=m, name='red')
            elfi.Prior('uniform', -20.0, 20.0, model=m, name='green')
            elfi.Prior('uniform', -20.0, 20.0, model=m, name='purple')
            params = [
                m['white'], m['yellow'], m['red'], m['green'], m['purple']
            ]

        y_obs = self.get_observed_data()
        elfi.Simulator(self.func, *params, observed=y_obs, name='sim')
        elfi.Distance('euclidean', m['sim'], name='dist')
        # elfi.Distance(self.discrepancyII, m['DGP'], name='d')
        return m
Beispiel #5
0
def create_model(shell_command, m, cog_catergories_dict):

    nfds_sim = elfi.tools.external_operation(
        shell_command, stdout=True,
        prepare_inputs=prepare_inputs)  #wraps command in a python function
    nfds_sim_vector = elfi.tools.vectorize(nfds_sim)  # vectorizes command

    #Defines 3 priors for the model
    elfi.Prior('uniform', -3.5, 3.4, model=m, name='v')
    elfi.Prior('uniform', -6, 5.9, model=m, name='s')
    elfi.Prior('uniform', -3.5, 2.5, model=m, name='i')

    #
    for i in cog_catergories_dict:
        if i != 'BCA':
            elfi.Prior('uniform', 0.01, 0.98, model=m, name=i)

    # create central node that runs the model, I think
    nfds_simulator = elfi.Simulator(
        nfds_sim_vector,
        m['v'],
        m['s'],
        m['i'],
        m['Bacteriocin'],
        m['CPS_BCA'],
        m['CPS'],
        m['GeneralMetabolism'],
        m['GeneralMetabolism_NutrientTransporters'],
        m['NutrientTransporters'],
        m['Prophage_ICE_PRCI_ProphageRemnant_OtherMGE'],
        m['Resistance'],
        name='nfds',
        observed=0.0)

    return (nfds_simulator)
Beispiel #6
0
def get_model(n_obs=100, true_params=None, seed_obs=None):
    """Returns a complete MA2 model in inference task

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

    Returns
    -------
    m : elfi.ElfiModel
    """
    if true_params is None:
        true_params = [.6, .2]

    y = MA2(*true_params,
            n_obs=n_obs,
            random_state=np.random.RandomState(seed_obs))
    sim_fn = partial(MA2, n_obs=n_obs)

    m = elfi.ElfiModel(set_current=False)
    elfi.Prior(CustomPrior1, 2, model=m, name='t1')
    elfi.Prior(CustomPrior2, m['t1'], 1, name='t2')
    elfi.Simulator(sim_fn, m['t1'], m['t2'], observed=y, name='MA2')
    elfi.Summary(autocov, m['MA2'], name='S1')
    elfi.Summary(autocov, m['MA2'], 2, name='S2')
    elfi.Distance('euclidean', m['S1'], m['S2'], name='d')
    return m
Beispiel #7
0
def get_model(n_obs=100, true_params=None, seed_obs=None, n_lags=5):
    """Return a complete ARCH(1) model.

    Parameters
    ----------
    n_obs: int
        Observation length of the ARCH(1) process.
    true_params: list, optinal
        Parameters with which the observed data are generated.
    seed_obs: int, optional
        Seed for the observed data generation.
    n_lags: int, optional
        Number of lags in summary statistics.

    Returns
    -------
    elfi.ElfiModel

    """
    if true_params is None:
        true_params = [0.3, 0.7]
        logger.info(
            f'true_params were not given. Now using [t1, t2] = {true_params}.')

    # elfi model
    m = elfi.ElfiModel()

    # priors
    t1 = elfi.Prior('uniform', -1, 2, model=m)
    t2 = elfi.Prior('uniform', 0, 1, model=m)
    priors = [t1, t2]

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

    # simulator
    Y = elfi.Simulator(arch, *priors, observed=y_obs)

    # summary statistics
    ss = []
    ss.append(elfi.Summary(sample_mean, Y, name='MU', model=m))
    ss.append(elfi.Summary(sample_variance, Y, name='VAR', model=m))
    for i in range(1, n_lags + 1):
        ss.append(elfi.Summary(autocorr, Y, i, name=f'AC_{i}', model=m))
    for i, j in combinations(range(1, n_lags + 1), 2):
        ss.append(
            elfi.Summary(pairwise_autocorr,
                         Y,
                         i,
                         j,
                         name=f'PW_{i}_{j}',
                         model=m))

    # distance
    elfi.Distance('euclidean', *ss, name='d', model=m)

    return m
Beispiel #8
0
def get_model(n_obs=50, true_params=None, seed_obs=None, stochastic=True):
    """Returns a complete Ricker model in inference task.
    
    This is a simplified example that achieves reasonable predictions. For more extensive treatment
    and description using 13 summary statistics, see:
    
    Wood, S. N. (2010) Statistical inference for noisy nonlinear ecological dynamic systems, 
    Nature 466, 1102–1107.

    Parameters
    ----------
    n_obs : int, optional
        Number of observations.
    true_params : list, optional
        Parameters with which the observed data is generated.
    seed_obs : None, int, optional
        Seed for the observed data generation.
    stochastic : bool, optional
        Whether to use the stochastic or deterministic Ricker model.

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

    if stochastic:
        simulator = partial(stochastic_ricker, n_obs=n_obs)
        if true_params is None:
            true_params = [3.8, 0.3, 10.]

    else:
        simulator = partial(ricker, n_obs=n_obs)
        if true_params is None:
            true_params = [3.8]

    m = elfi.ElfiModel()
    y_obs = simulator(*true_params, n_obs=n_obs, random_state=np.random.RandomState(seed_obs))
    sim_fn = partial(simulator, n_obs=n_obs)
    sumstats = []

    if stochastic:
        elfi.Prior(ss.expon, np.e, 2, model=m, name='t1')
        elfi.Prior(ss.truncnorm, 0, 5, model=m, name='t2')
        elfi.Prior(ss.uniform, 0, 100, model=m, name='t3')
        elfi.Simulator(sim_fn, m['t1'], m['t2'], m['t3'], observed=y_obs, name='Ricker')
        sumstats.append(elfi.Summary(partial(np.mean, axis=1), m['Ricker'], name='Mean'))
        sumstats.append(elfi.Summary(partial(np.var, axis=1), m['Ricker'], name='Var'))
        sumstats.append(elfi.Summary(num_zeros, m['Ricker'], name='#0'))
        elfi.Discrepancy(chi_squared, *sumstats, name='d')

    else:  # very simple deterministic case
        elfi.Prior(ss.expon, np.e, model=m, name='t1')
        elfi.Simulator(sim_fn, m['t1'], observed=y_obs, name='Ricker')
        sumstats.append(elfi.Summary(partial(np.mean, axis=1), m['Ricker'], name='Mean'))
        elfi.Distance('euclidean', *sumstats, name='d')

    return m
    def build(self,
              model,
              pattern,
              prior_pos,
              prior_cov=64,
              r_bound=47.9,
              pmt_mask=np.ones(127)):
        ### Build Priors
        px = elfi.Prior(BoundedNormal_x, r_bound, prior_pos, prior_cov)
        py = elfi.Prior(BoundedNormal_y, px, r_bound, prior_pos, prior_cov)

        ### Build Model
        model = elfi.tools.vectorize(model)
        Y = elfi.Simulator(model, px, py, observed=np.array([pattern]))

        # TODO implement PMT mask here
        #def summarize(data, key):
        #    # Select either energy or time for model output.
        #    return np.array([v[key] for v in data])
        def summarize(data):
            return np.array(
                [list(v['energy']) + list(v['time']) for v in data])

        # Build summary stat for energy and time
        #S1 = elfi.Summary(summarize, Y, 'energy')
        #S2 = elfi.Summary(summarize, Y, 'time')
        S1 = elfi.Summary(summarize, Y)

        d = elfi.Distance('braycurtis', S1)
        log_d = elfi.Operation(np.log, d)

        # set the ELFI model so we can remove it later
        self.model = px.model

        ### Setup BOLFI
        bounds = {'px': (-r_bound, r_bound), 'py': (-r_bound, r_bound)}

        target_model = GPyRegression(log_d.model.parameter_names,
                                     bounds=bounds)

        acquisition_method = ConstraintLCBSC(target_model,
                                             prior=ModelPrior(log_d.model),
                                             noise_var=[1, 1],
                                             exploration_rate=10)

        bolfi = elfi.BOLFI(
            log_d,
            batch_size=1,
            initial_evidence=50,
            update_interval=1,
            # bounds=bounds,  # Not used when using target_model
            target_model=target_model,
            # acq_noise_var=[0.1, 0.1],  # Not used when using acq method
            acquisition_method=acquisition_method,
        )
        return bolfi
Beispiel #10
0
def get_model(true_params=None, seed_obs=None, **kwargs):
    """Return a complete ELFI graph ready for inference.

    Selection of true values, priors etc. follows the approach in

    Numminen, E., Cheng, L., Gyllenberg, M. and Corander, J.: Estimating the transmission dynamics
    of Streptococcus pneumoniae from strain prevalence data, Biometrics, 69, 748-757, 2013.

    and

    Gutmann M U, Corander J (2016). Bayesian Optimization for Likelihood-Free Inference
    of Simulator-Based Statistical Models. JMLR 17(125):1−47, 2016.

    Parameters
    ----------
    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

    """
    logger = logging.getLogger()
    if true_params is None:
        true_params = [3.6, 0.6, 0.1]

    m = elfi.ElfiModel()
    y_obs = daycare(*true_params,
                    random_state=np.random.RandomState(seed_obs),
                    **kwargs)
    sim_fn = partial(daycare, **kwargs)
    priors = []
    sumstats = []

    priors.append(elfi.Prior('uniform', 0, 11, model=m, name='t1'))
    priors.append(elfi.Prior('uniform', 0, 2, model=m, name='t2'))
    priors.append(elfi.Prior('uniform', 0, 1, model=m, name='t3'))

    elfi.Simulator(sim_fn, *priors, observed=y_obs, name='DCC')

    sumstats.append(elfi.Summary(ss_shannon, m['DCC'], name='Shannon'))
    sumstats.append(elfi.Summary(ss_strains, m['DCC'], name='n_strains'))
    sumstats.append(elfi.Summary(ss_prevalence, m['DCC'], name='prevalence'))
    sumstats.append(elfi.Summary(ss_prevalence_multi, m['DCC'], name='multi'))

    elfi.Discrepancy(distance, *sumstats, name='d')

    logger.info(
        "Generated observations with true parameters "
        "t1: %.1f, t2: %.3f, t3: %.1f, ", *true_params)

    return m
Beispiel #11
0
def get_model(n_obs=50, true_params=None, stats_summary=None, seed_obs=None):
    """Return an initialised univariate g-and-k model.

    Parameters
    ----------
    n_obs : int, optional
        The number of the observed points.
    true_params : array_like, optional
        The parameters defining the model.
    stats_summary : array_like, optional
        The chosen summary statistics, expressed as a list of strings.
        Options: ['ss_order'], ['ss_robust'], ['ss_octile'].
    seed_obs : np.random.RandomState, optional

    Returns
    -------
    elfi.ElfiModel

    """
    m = elfi.ElfiModel()

    # Initialising the default parameter settings as given in [2].
    if true_params is None:
        true_params = [3, 1, 2, .5]
    if stats_summary is None:
        stats_summary = ['ss_order']

    # Initialising the default prior settings as given in [2].
    elfi.Prior('uniform', 0, 10, model=m, name='a')
    elfi.Prior('uniform', 0, 10, model=m, name='b')
    elfi.Prior('uniform', 0, 10, model=m, name='g')
    elfi.Prior('uniform', 0, 10, model=m, name='k')

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

    # Defining the simulator.
    fn_sim = partial(GNK, n_obs=n_obs)
    elfi.Simulator(fn_sim, m['a'], m['b'], m['g'], m['k'], observed=y_obs,
                   name='GNK')

    # Initialising the chosen summary statistics.
    fns_summary_all = [ss_order, ss_robust, ss_octile]
    fns_summary_chosen = []
    for fn_summary in fns_summary_all:
        if fn_summary.__name__ in stats_summary:
            summary = elfi.Summary(fn_summary, m['GNK'],
                                   name=fn_summary.__name__)
            fns_summary_chosen.append(summary)

    elfi.Discrepancy(euclidean_multidim, *fns_summary_chosen, name='d')

    return m
Beispiel #12
0
    def test_constructor(self):
        p1 = elfi.Prior('p1', 'uniform', 0, 1)
        p2 = elfi.Prior('p2', 'uniform', 0, 1)
        d = elfi.Discrepancy('d', np.mean, p1, p2)
        abc = elfi.ABCMethod(d, [p1, p2])

        try:
            abc = elfi.ABCMethod()
            abc = elfi.ABCMethod(0.2, None)
            abc = elfi.ABCMethod([d], [p1, p2])
            abc = elfi.ABCMethod(d, p1)
            assert False
        except:
            assert True
Beispiel #13
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]
Beispiel #14
0
    def get_model(self, n_obs=100, true_params=None, seed_obs=None):
        """Return a complete MA2 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

        """
        if true_params is None:
            true_params = [60]

        y_obs = self.func(*true_params,
                          random_state=np.random.RandomState(seed_obs))
        m = elfi.ElfiModel()
        elfi.Prior(ss.uniform, 0, 100, model=m, name='t1')
        elfi.Simulator(self.func, m['t1'], observed=y_obs, name='sim')
        elfi.Distance('euclidean', m['sim'], name='dist')
        return m
Beispiel #15
0
def test_reset_specific_scheduler_keys():
    """This test fails if keys are not different"""
    elfi.env.client(n_workers=2, threads_per_worker=1)
    N = 20
    bs = 10

    y = None
    t = None

    p1 = elfi.Prior('p', 'Uniform')
    sim1 = elfi.Simulator('sim', lambda *args, **kwargs: args[0], p1, observed=1)

    for i in range(10):
        y_prev = y
        t_prev = t

        y = sim1.acquire(N, batch_size=bs).compute()
        t = p1.acquire(N, batch_size=bs).compute()

        if y_prev is not None:
            assert np.all(y != y_prev)
            assert np.all(t != t_prev)

        p1.reset()

    elfi.env.client().shutdown()
def test_single_parameter_linear_adjustment():
    """A regression test against values obtained in the notebook."""
    seed = 20170616
    n_obs = 50
    batch_size = 100
    mu, sigma = (5, 1)

    # Hyperparameters
    mu0, sigma0 = (10, 100)

    y_obs = gauss.Gauss(
        mu, sigma, n_obs=n_obs, batch_size=1, random_state=np.random.RandomState(seed))
    sim_fn = partial(gauss.Gauss, sigma=sigma, n_obs=n_obs)

    # Posterior
    n = y_obs.shape[1]
    mu1 = (mu0 / sigma0**2 + y_obs.sum() / sigma**2) / (1 / sigma0**2 + n / sigma**2)
    sigma1 = (1 / sigma0**2 + n / sigma**2)**(-0.5)

    # Model
    m = elfi.ElfiModel()
    elfi.Prior('norm', mu0, sigma0, model=m, name='mu')
    elfi.Simulator(sim_fn, m['mu'], observed=y_obs, name='Gauss')
    elfi.Summary(lambda x: x.mean(axis=1), m['Gauss'], name='S1')
    elfi.Distance('euclidean', m['S1'], name='d')

    res = elfi.Rejection(m['d'], output_names=['S1'], seed=seed).sample(1000, threshold=1)
    adj = elfi.adjust_posterior(model=m, sample=res, parameter_names=['mu'], summary_names=['S1'])

    assert np.allclose(_statistics(adj.outputs['mu']), (4.9772879640569778, 0.02058680115402544))
Beispiel #17
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
Beispiel #18
0
def test_node_data_sub_slicing():
    mu = elfi.Prior('mu', 'uniform', 0, 4)
    ar1 = mu.acquire(10).compute()
    ar2 = mu.acquire(5).compute()
    assert np.array_equal(ar1[0:5], ar2)

    ar3 = mu.acquire(20).compute()
    assert np.array_equal(ar1, ar3[0:10])
Beispiel #19
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
Beispiel #20
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
Beispiel #21
0
 def test_numerical_grad_logpdf(self):
     # Test gradient with a normal distribution
     loc = 2.2
     scale = 1.1
     x = np.random.rand()
     analytical_grad_logpdf = -(x - loc) / scale**2
     prior_node = elfi.Prior('normal', loc, scale, model=elfi.ElfiModel())
     num_grad = ModelPrior(prior_node.model).gradient_logpdf(x)
     assert np.isclose(num_grad, analytical_grad_logpdf, atol=0.01)
Beispiel #22
0
    def test_basics(self, ma2, distribution_test):
        # A 1D case
        normal = elfi.Prior('normal', 5, model=elfi.ElfiModel())
        normal_prior = ModelPrior(normal.model)
        distribution_test(normal_prior)

        # A 2D case
        prior = ModelPrior(ma2)
        distribution_test(prior)
Beispiel #23
0
 def test_sample(self):
     p1 = elfi.Prior('p1', 'uniform', 0, 1)
     d = elfi.Discrepancy('d', np.mean, p1)
     abc = elfi.ABCMethod(d, [p1])
     try:
         abc.sample()  # NotImplementedError
         assert False
     except:
         assert True
Beispiel #24
0
def get_model(alpha=0.2, delta=0, tau=0.198, N=20, seed_obs=None):
    """Returns the example model used in Lintusaari et al. 2016.

    Here we infer alpha using the summary statistic T1. We expect the executable `bdm` be
    available in the working directory.

    Parameters
    ----------

    alpha : float
        birth rate
    delta : float
        death rate
    tau : float
        mutation rate
    N : int
        size of the population
    seed_obs : None, int
        Seed for the observed data generation. None gives the same data as in
        Lintusaari et al. 2016

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

    if seed_obs is None and N == 20:
        y = np.zeros(N, dtype='int16')
        data = np.array([6, 3, 2, 2, 1, 1, 1, 1, 1, 1, 1], dtype='int16')
        y[0:len(data)] = data

    else:
        y = BDM(alpha,
                delta,
                tau,
                N,
                random_state=np.random.RandomState(seed_obs))

    m = elfi.ElfiModel(name='bdm')
    elfi.Prior('uniform', .005, 2, model=m, name='alpha')
    elfi.Simulator(BDM, m['alpha'], delta, tau, N, observed=y, name='BDM')
    elfi.Summary(T1, m['BDM'], name='T1')
    elfi.Distance('minkowski', m['T1'], p=1, name='d')

    m['BDM'].uses_meta = True

    # Warn the user if the executable is not present
    if not os.path.isfile('bdm') and not os.path.isfile('bdm.exe'):
        cpp_path = get_sources_path()
        warnings.warn(
            "This model uses an external simulator `bdm` implemented in C++ "
            "that needs to be compiled and copied to your working directory. "
            "We could not find it from your current working directory. Please"
            "copy the folder `{}` to your working directory "
            "and compile the source.".format(cpp_path), RuntimeWarning)

    return m
Beispiel #25
0
    def build(self,
              model,
              pattern,
              prior_pos,
              prior_cov=25,
              r_bound=47.9,
              pmt_mask=np.ones(127)):
        ### Build Priors
        px = elfi.Prior(BoundedNormal_x, r_bound, prior_pos, prior_cov)
        py = elfi.Prior(BoundedNormal_y, px, r_bound, prior_pos, prior_cov)

        ### Build Model
        model = elfi.tools.vectorize(model)
        Y = elfi.Simulator(model, px, py, observed=np.array([pattern]))

        # TODO implement PMT mask here
        d = elfi.Distance('braycurtis', Y)
        log_d = elfi.Operation(np.log, d)

        # set the ELFI model so we can remove it later
        self.model = px.model

        ### Setup BOLFI
        bounds = {'px': (-r_bound, r_bound), 'py': (-r_bound, r_bound)}

        target_model = GPyRegression(log_d.model.parameter_names,
                                     bounds=bounds)

        acquisition_method = ConstraintLCBSC(target_model,
                                             prior=ModelPrior(log_d.model),
                                             noise_var=[5, 5],
                                             exploration_rate=10)

        bolfi = elfi.BOLFI(
            log_d,
            batch_size=1,
            initial_evidence=50,
            update_interval=1,
            # bounds=bounds,  # Not used when using target_model
            target_model=target_model,
            # acq_noise_var=[0.1, 0.1],  # Not used when using acq method
            acquisition_method=acquisition_method,
        )
        return bolfi
Beispiel #26
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.ElfiModel()
        if true_params is None and self.env_name == "CartPole-v0":
            # gravity, masscart, masspole, length, force_mag
            true_params = [[9.8], [1.0], [0.1], [0.5], [10.0]]
            elfi.Prior('uniform', 5.0, 10.0, model=m, name='gravity')
            elfi.Prior('uniform', 0.1, 5, model=m, name='masscart')
            elfi.Prior('uniform', 0.1, 5, model=m, name='masspole')
            elfi.Prior('uniform', 0.1, 5, model=m, name='length')
            elfi.Prior('uniform', 5.0, 10.0, model=m, name='force_mag')
            params = [
                m['gravity'], m['masscart'], m['masspole'], m['length'],
                m['force_mag']
            ]

        elif true_params is None and self.env_name == "MountainCar-v0":
            # goal_position, goal_velocity, force, gravity
            true_params = [0.5, 0, 0.001, 0.0025]
            elfi.Prior('uniform', -1.2, 0.6, model=m, name='goal_position')
            elfi.Prior('uniform', 0, 5.0, model=m, name='goal_velocity')
            elfi.Prior('uniform', 0.0001, 0.01, model=m, name='force')
            elfi.Prior('uniform', 0.0001, 0.01, model=m, name='gravity')
            params = [
                m['goal_position'], m['goal_velocity'], m['force'],
                m['gravity']
            ]

        y_obs = self.func(*true_params,
                          random_state=np.random.RandomState(seed_obs))
        elfi.Simulator(self.func, *params, observed=y_obs, name='DGP')
        elfi.Distance('euclidean', m['DGP'], name='d')
        return m
Beispiel #27
0
    def test_ma2(self, seed=0):
        """Identifying the optimal summary statistics combination following the MA2 model.

        Parameters
        ----------
        seed : int, optional

        """
        # Defining summary statistics.
        ss_mean = Gauss.ss_mean
        ss_ac_lag1 = partial(MA2.autocov, lag=1)
        ss_ac_lag1.__name__ = 'ac_lag1'
        ss_ac_lag2 = partial(MA2.autocov, lag=2)
        ss_ac_lag2.__name__ = 'ac_lag2'

        list_ss = [ss_ac_lag1, ss_ac_lag2, ss_mean]

        # Initialising the simulator.
        prior_t1 = elfi.Prior(MA2.CustomPrior1, 2, name='prior_t1')
        prior_t2 = elfi.Prior(MA2.CustomPrior2, prior_t1, 1, name='prior_t2')

        t1_true = .6
        t2_true = .2
        fn_simulator = MA2.MA2
        y_obs = fn_simulator(t1_true,
                             t2_true,
                             random_state=np.random.RandomState(seed))
        simulator = elfi.Simulator(fn_simulator,
                                   prior_t1,
                                   prior_t2,
                                   observed=y_obs)

        # Identifying the optimal summary statistics based on the Two-Stage procedure.
        diagnostics = TwoStageSelection(simulator,
                                        'euclidean',
                                        list_ss=list_ss,
                                        seed=seed)
        set_ss_2stage = diagnostics.run(n_sim=100000, batch_size=10000)

        assert ss_ac_lag1 in set_ss_2stage
        assert ss_ac_lag2 in set_ss_2stage
        assert ss_mean not in set_ss_2stage
Beispiel #28
0
def multivariate_model(request):
    ndim = request.param

    def fun(x, batch_size, random_state):
        return np.sum(x, keepdims=True, axis=1)

    m = elfi.ElfiModel()
    elfi.Prior(ss.multivariate_normal, [0] * ndim, model=m, name='t1')
    elfi.Simulator(fun, m['t1'], observed=np.array([[0]]), model=m, name='sim')
    elfi.Distance('euclidean', m['sim'], model=m, name='d')
    return m
    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
Beispiel #30
0
def multivariate_model(request):
    ndim = request.param
    m = elfi.ElfiModel()
    elfi.Prior(ss.multivariate_normal, [0] * ndim, model=m, name='t1')
    elfi.Simulator(rowsummer,
                   m['t1'],
                   observed=np.array([[0]]),
                   model=m,
                   name='sim')
    elfi.Distance('euclidean', m['sim'], model=m, name='d')
    return m