How to use the elfi.examples.ma2 function in elfi

To help you get started, we’ve selected a few elfi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github elfi-dev / elfi / tests / unit / test_model_selection.py View on Github external
def test_compare_models():
    m = gauss.get_model()
    res1 = elfi.Rejection(m['d']).sample(100)

    # use less informative prior
    m['mu'].become(elfi.Prior('uniform', -10, 50))
    res2 = elfi.Rejection(m['d']).sample(100)

    # use different simulator
    m['gauss'].become(elfi.Simulator(ma2.MA2, m['mu'], m['sigma'], observed=m.observed['gauss']))
    res3 = elfi.Rejection(m['d']).sample(100)

    p = elfi.compare_models([res1, res2, res3])
    assert p[0] > p[1]
    assert p[1] > p[2]
github elfi-dev / elfi / tests / unit / test_document_examples.py View on Github external
def extract_result(self):
            filtered_outputs = self.state['filtered_outputs']
            outputs = {name: np.concatenate(filtered_outputs[name]) for name in self.output_names}

            return Sample(
                method_name='CustomMethod',
                outputs=outputs,
                parameter_names=self.parameter_names,
                discrepancy_name=self.discrepancy_name,
                n_sim=self.state['n_sim'],
                threshold=self.threshold)

    # Below is from the part where we demonstrate iterative advancing

    # Run it
    m = ma2.get_model()
    custom_method = CustomMethod(m, 'd', threshold=.1, batch_size=1000)

    # Continue inference from the previous state (with n_sim=2000)
    custom_method.infer(n_sim=4000)

    # Or use it iteratively
    custom_method.set_objective(n_sim=6000)

    custom_method.iterate()
    assert custom_method.finished == False

    # Investigate the current state
    custom_method.extract_result()

    custom_method.iterate()
    assert custom_method.finished
github elfi-dev / elfi / tests / unit / test_elfi_model.py View on Github external
def test_observed():
    true_params = [.6, .2]
    m = ema2.get_model(100, true_params=true_params)
    y = m.observed['MA2']
    S1 = m.get_reference('S1')
    S2 = m.get_reference('S2')

    S1_observed = ema2.autocov(y)
    S2_observed = ema2.autocov(y, 2)

    assert np.array_equal(S1.observed, S1_observed)
    assert np.array_equal(S2.observed, S2_observed)
github elfi-dev / elfi / tests / functional / test_serialization.py View on Github external
def test_pickle_ma2():
    m = ma2.get_model()
    d = m.get_reference('d')

    np.random.seed(0)
    res1 = d.generate(10)

    serialized = pickle.dumps(m)
    m = pickle.loads(serialized)
    d = m.get_reference('d')

    np.random.seed(0)
    res2 = d.generate(10)

    assert np.array_equal(res1, res2)
github elfi-dev / elfi / tests / functional / test_inference.py View on Github external
def setup_ma2_with_informative_data():
    true_params = OrderedDict([('t1', .6), ('t2', .2)])
    n_obs = 100

    # In our implementation, seed 4 gives informative (enough) synthetic observed
    # data of length 100 for quite accurate inference of the true parameters using
    # posterior mean as the point estimate
    m = ma2.get_model(n_obs=n_obs, true_params=true_params.values(), seed_obs=4)
    return m, true_params
github elfi-dev / elfi / tests / unit / test_elfi_model.py View on Github external
def test_observed():
    true_params = [.6, .2]
    m = ema2.get_model(100, true_params=true_params)
    y = m.observed['MA2']
    S1 = m.get_reference('S1')
    S2 = m.get_reference('S2')

    S1_observed = ema2.autocov(y)
    S2_observed = ema2.autocov(y, 2)

    assert np.array_equal(S1.observed, S1_observed)
    assert np.array_equal(S2.observed, S2_observed)
github elfi-dev / elfi / tests / unit / test_elfi_model.py View on Github external
def test_become_with_simulators(self, ma2):
        y_obs = np.zeros(100)
        new_sim = elfi.Simulator(ema2.MA2, ma2['t1'], ma2['t2'], observed=y_obs)
        ma2['MA2'].become(new_sim)

        # Test that observed data is changed
        assert np.array_equal(ma2.observed['MA2'], y_obs)

        # Test that inference still works
        r = elfi.Rejection(ma2, 'd')
        r.sample(10)
github elfi-dev / elfi / tests / functional / test_inference.py View on Github external
x = np.random.random((1, len(true_params)))
    bolfi.target_model.is_sampling = True

    pred_mu, pred_var = bolfi.target_model._gp.predict(x)
    pred_cached_mu, pred_cached_var = bolfi.target_model.predict(x)
    assert (np.allclose(pred_mu, pred_cached_mu))
    assert (np.allclose(pred_var, pred_cached_var))

    grad_mu, grad_var = bolfi.target_model._gp.predictive_gradients(x)
    grad_cached_mu, grad_cached_var = bolfi.target_model.predictive_gradients(x)
    assert (np.allclose(grad_mu[:, :, 0], grad_cached_mu))
    assert (np.allclose(grad_var, grad_cached_var))

    # test calculation of prior logpdfs
    true_logpdf_prior = ma2.CustomPrior1.logpdf(x[0, 0], 2)
    true_logpdf_prior += ma2.CustomPrior2.logpdf(x[0, 1], x[0, 0, ], 1)

    assert np.isclose(true_logpdf_prior, post.prior.logpdf(x[0, :]))
github elfi-dev / elfi / tests / unit / test_methods.py View on Github external
samples = res.samples_array
    means = res.sample_means_array
    assert np.allclose(means, np.average(samples, 0, w))
    assert not np.allclose(means, np.mean(samples, 0))

    # Test result summaries
    res.summary()
    res.summary(all=True)

    res.sample_means_summary()
    res.sample_means_summary(all=True)

    # Ensure prior pdf > 0 for samples
    assert np.all(exma2.CustomPrior1.pdf(samples[:, 0], 2) > 0)
    assert np.all(exma2.CustomPrior2.pdf(samples[:, 1], samples[:, 0], 1) > 0)