Пример #1
0
def test_rng_repeatability():
    mu = np.atleast_1d([0.0])
    S = np.atleast_2d(1.0)

    # distributions
    pG = dd.Gaussian(m=mu, S=S)
    check_repeatability_dist(pG)

    pMoG = dd.MoG(a=np.array([0.25, 0.75]), ms=[mu, mu], Ss=[S, S])
    check_repeatability_dist(pMoG)

    # simulators
    mG = sims.Gauss()
    check_repeatability_sim(mG, np.zeros(mG.dim_param).reshape(-1, 1))

    mMoG = sims.GaussMixture()
    check_repeatability_sim(mMoG, np.zeros(mMoG.dim_param).reshape(-1, 1))

    # generators
    g = gen.Default(model=mMoG, prior=pMoG, summary=Identity())
    check_repeatability_gen(g)

    # inference methods
    # we're going to create each one with a different deepcopy of g to make
    # sure thre are are no side effects e.g. changes to the proposal
    x0 = g.gen(1, verbose=False)[1]
    inf_opts = dict(obs=x0,
                    n_components=2,
                    n_hiddens=[5, 5],
                    verbose=False,
                    pilot_samples=0)

    yB_nosvi = inf.Basic(deepcopy(g), svi=False, **inf_opts)
    check_repeatability_infer(yB_nosvi)

    yB_svi = inf.Basic(deepcopy(g), svi=True, **inf_opts)
    check_repeatability_infer(yB_svi)

    # skip CDELFI for now since it might crash if we don't use the prior
    #yC = inf.CDELFI(deepcopy(g), **inf_opts)
    #check_repeatability_infer(yC)

    yS = inf.SNPE(deepcopy(g), prior_mixin=0.5, **inf_opts)
    check_repeatability_infer(yS)
Пример #2
0
def test_basic_inference(n_params=2, seed=42):
    m = Gauss(dim=n_params, seed=seed)
    p = dd.Gaussian(m=np.zeros((n_params, )), S=np.eye(n_params), seed=seed)
    s = ds.Identity()
    g = dg.Default(model=m, prior=p, summary=s)

    # set up inference
    res = infer.Basic(g, seed=seed)

    # run with N samples
    out = res.run(1000)

    # check result
    posterior = res.predict(np.array([0., 0.]).reshape(1, -1))
    assert np.allclose(posterior.xs[0].S,
                       np.array([[0.1, 0.0], [0.0, 0.1]]),
                       atol=0.05)
    assert np.allclose(posterior.xs[0].m, np.array([0.0, 0.0]), atol=0.05)
Пример #3
0
def test_basic_inference_inputsamples(n_params=2, seed=42, n_pilot=1000):
    model = Gauss(dim=n_params, seed=seed)
    prior = dd.Gaussian(m=np.zeros((n_params, )),
                        S=np.eye(n_params),
                        seed=seed + 1)
    s = ds.Identity(seed=seed + 2)
    g = dg.Default(model=model, prior=prior, summary=s, seed=seed + 3)
    obs = np.zeros((1, n_params))  # reseed generator etc. (?)
    m_true, S_true = simplegaussprod(obs, model.noise_cov, prior.m, prior.S)

    params, stats = g.gen(n_pilot)
    pilot_samples = (params, stats)

    res = infer.Basic(g, obs=obs, seed=seed + 4, pilot_samples=pilot_samples)
    res.reset(seed=seed + 4)

    out = res.run(n_train=1000)
    posterior = res.predict(res.obs.reshape(1, -1))

    check_gaussian_posterior(posterior, m_true, S_true)