How to use the elfi.SMC 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 / functional / test_inference.py View on Github external
def test_smc():
    m, true_params = setup_ma2_with_informative_data()

    thresholds = [.5, .25, .1]
    N = 1000
    smc = elfi.SMC(m['d'], batch_size=20000)
    res = smc.sample(N, thresholds=thresholds)

    check_inference_with_informative_data(res.samples, N, true_params)

    # We should be able to carry out the inference in less than six batches
    assert res.populations[-1].n_batches < 6
github elfi-dev / elfi / tests / unit / test_utils.py View on Github external
def test_sample_object_to_dict():
    data_rej = OrderedDict()
    data_smc = OrderedDict()
    m = get_model(n_obs=100, true_params=[.6, .2])
    batch_size, n = 1, 2
    schedule = [0.7, 0.2, 0.05]
    rej = elfi.Rejection(m['d'], batch_size=batch_size)
    res_rej = rej.sample(n, threshold=0.1)
    smc = elfi.SMC(m['d'], batch_size=batch_size)
    res_smc = smc.sample(n, schedule)
    sample_object_to_dict(data_rej, res_rej)
    sample_object_to_dict(data_smc, res_smc, skip='populations')
    assert any(x not in data_rej for x in ['meta', 'output']) is True
    assert any(x not in data_smc for x in ['meta', 'output', 'populations']) is True
github elfi-dev / elfi / tests / functional / test_consistency.py View on Github external
def test_smc(ma2):
    bs = 3
    n_samples = 10
    thresholds = [1, .9, .8]

    smc = elfi.SMC(ma2, 'd', batch_size=bs)
    sample = smc.sample(n_samples, thresholds=thresholds)
    seed = smc.seed

    smc = elfi.SMC(ma2, 'd', batch_size=bs, seed=seed)
    sample_same = smc.sample(n_samples, thresholds=thresholds)

    smc = elfi.SMC(ma2, 'd', batch_size=bs)
    sample_diff = smc.sample(n_samples, thresholds=thresholds)

    check_consistent_sample(sample, sample_diff, sample_same)
github elfi-dev / elfi / tests / functional / test_consistency.py View on Github external
def test_smc(ma2):
    bs = 3
    n_samples = 10
    thresholds = [1, .9, .8]

    smc = elfi.SMC(ma2, 'd', batch_size=bs)
    sample = smc.sample(n_samples, thresholds=thresholds)
    seed = smc.seed

    smc = elfi.SMC(ma2, 'd', batch_size=bs, seed=seed)
    sample_same = smc.sample(n_samples, thresholds=thresholds)

    smc = elfi.SMC(ma2, 'd', batch_size=bs)
    sample_diff = smc.sample(n_samples, thresholds=thresholds)

    check_consistent_sample(sample, sample_diff, sample_same)
github elfi-dev / elfi / tests / functional / test_consistency.py View on Github external
def test_smc(ma2):
    bs = 3
    n_samples = 10
    thresholds = [1, .9, .8]

    smc = elfi.SMC(ma2, 'd', batch_size=bs)
    sample = smc.sample(n_samples, thresholds=thresholds)
    seed = smc.seed

    smc = elfi.SMC(ma2, 'd', batch_size=bs, seed=seed)
    sample_same = smc.sample(n_samples, thresholds=thresholds)

    smc = elfi.SMC(ma2, 'd', batch_size=bs)
    sample_diff = smc.sample(n_samples, thresholds=thresholds)

    check_consistent_sample(sample, sample_diff, sample_same)
github elfi-dev / elfi / tests / unit / test_methods.py View on Github external
def test_smc(ma2):
    thresholds = [.5, .2]
    N = 1000
    smc = elfi.SMC(ma2['d'], batch_size=20000)
    res = smc.sample(N, thresholds=thresholds)
    dens = smc._prior.logpdf(res.samples_array)
    # Test that the density is uniform
    assert np.allclose(dens, dens[0])

    # Test that weighted mean is computed
    w = res.weights
    assert w is not None

    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()