Beispiel #1
0
def test_IndependentJoint_uniform_rejection():
    # check that proposed samples are correctly rejected when using a
    # IndependentJoint prior with some child distributions uniform. We used a
    # Gaussian proposal to generate some samples that need to be rejected.
    N = 1000
    B1 = [-1.0, 1.0]
    B2 = [-2.0, 2.0]
    u1 = dd.Uniform(B1[0], B1[1])
    u2 = dd.Uniform(B2[0], B2[1])
    prior = dd.IndependentJoint([u1, u2])

    m = [0., 0.]
    S = [[
        2.,
        0.,
    ], [
        0.,
        2.,
    ]]
    proposal = dd.Gaussian(m=m, S=S)

    model = Gauss(dim=2)

    s = ds.Identity()

    g = dg.Default(model=model, prior=prior, summary=s)
    g.proposal = proposal

    params, stats = g.gen(N, verbose=False)
    assert (params.min(axis=0) >= np.array([B1[0], B2[0]])).all() and \
        (params.min(axis=0) <= np.array([B1[1], B2[1]])).all(), \
        "rejection failed"
Beispiel #2
0
def dont_test_apt_inference_atomicprop_maf_normalize(n_params, seed=47):
    # normalization test is not finished yet.
    m = Gauss(dim=n_params, noise_cov=0.1)
    p = dd.Uniform(lower=-0.05 * np.ones(n_params),
                   upper=0.05 * np.ones(n_params))
    s = ds.Identity()
    g = dg.Default(model=m, prior=p, summary=s)
Beispiel #3
0
def test_trainer_updates():
    n_components = 1
    n_params = 2
    seed = 42
    svi = True

    m = Gauss(dim=n_params)
    p = dd.Gaussian(m=np.zeros((n_params, )), S=np.eye(n_params))
    s = ds.Identity()
    g = dg.Default(model=m, prior=p, summary=s)

    nn = NeuralNet(
        n_components=n_components,
        n_hiddens=[10],
        n_inputs=n_params,
        n_outputs=n_params,
        seed=seed,
        svi=svi)
    loss = -tt.mean(nn.lprobs)

    trn_inputs = [nn.params, nn.stats]
    trn_data = g.gen(100)  # params, stats
    trn_data = tuple(x.astype(dtype) for x in trn_data)

    t = Trainer(network=nn, loss=loss, trn_data=trn_data, trn_inputs=trn_inputs)

    # single update
    outputs = t.make_update(*trn_data)

    # training
    outputs = t.train(100, 50)
Beispiel #4
0
def runAPT2psychometric(obs0, hyps, labels, true_params, fignames, plot=True):
    """
    obs0:
        The data observation we made
        
    This takes in a dict with the following parameters:
    
    prior       : prior estimate and bounds
    m           : The model we are going to run apt on
    s           : The summary statistics we care about
    n_train     : Number of param-samples drawn during a training round
    n_rounds    : Number of rounds you want to run APT for
    n_hidden    : A Number of Layers x 1 array containing the layer sizes
    pilot_samples : Number of samples drawn during the pilot run
    val_frac    : Fraction of validation samples 
    minibatch   : Size of a batch
    epochs      : Number of training iterations
    density     : 'mog' or 'maf' supported
    n_mades     : number of Mades, an MAF parameter
         
    """

    g = dg.Default(model=hyps['m'], prior=hyps['prior'], summary=hyps['s'])

    # define statsitics summary of observation
    obs_stats = hyps['s'].calc([obs0])

    seed_inf = hyps['seed_inf']

    prior_norm = True

    # MAF parameters
    density = 'maf'
    n_mades = 5  # number of MADES

    # inference object
    res = infer.SNPEC(g,
                      obs=obs_stats,
                      n_hiddens=hyps['n_hiddens'],
                      seed=seed_inf,
                      pilot_samples=hyps['pilot_samples'],
                      n_mades=hyps['n_mades'],
                      prior_norm=prior_norm,
                      density=hyps['density'])
    # train
    log, _, posterior = res.run(
        n_train=hyps['n_train'],
        n_rounds=hyps['n_rounds'],
        minibatch=hyps['minibatch'],
        epochs=hyps['epochs'],
        silent_fail=False,
        proposal='prior',
        val_frac=hyps['val_frac'],
        verbose=True,
    )

    if plot == True:
        plot_APT(posterior, g, labels, true_params, fignames)

    return posterior
Beispiel #5
0
def test_gauss_shape():
    for n_params in range(1, 3):
        m = Gauss(dim=n_params)
        p = dd.Gaussian(m=np.zeros((n_params, )), S=np.eye(n_params))
        s = ds.Identity()

        g = dg.Default(model=m, prior=p, summary=s)

        n_samples = 100
        params, stats = g.gen(n_samples)

        n_summary = n_params
        assert params.shape == (n_samples, n_params)
        assert stats.shape == (n_samples, n_summary)
Beispiel #6
0
def init_all_gaussian(n_params=2,
                      seed=42,
                      inferenceobj=None,
                      **inf_setup_opts):
    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. (?)

    res = inferenceobj(g, obs=obs, seed=seed + 4, **inf_setup_opts)
    res.reset(seed=seed + 4)

    m_true, S_true = simplegaussprod(obs, model.noise_cov, prior.m, prior.S)
    return res, m_true, S_true
Beispiel #7
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)
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
0
def test_snpe_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)

    # observation
    _, obs = g.gen(1)

    # set up inference
    res = infer.SNPE(g, obs=obs)

    # run with N samples
    out = res.run(n_train=1000, n_rounds=1)

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