Пример #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"
Пример #2
0
def test_IndependentJoint_eval():
    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])
    dist = dd.IndependentJoint([u1, u2])
    samples = dist.gen(N)
    logprobs = dist.eval(samples, log=True)
    lpdfval = -np.log((B2[1] - B2[0]) * (B1[1] - B1[0]))
    assert np.isclose(logprobs, lpdfval).all()

    assert samples.shape == (N, 2)
    assert logprobs.shape == (N, )
Пример #3
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)
Пример #4
0
def plot_1d_posterior(env_params=None,
                      p_lower=[0.1, 0.1],
                      p_upper=[2.0, 2.0],
                      params_shape=2,
                      posterior=None):
    p = dd.Uniform(lower=p_lower, upper=p_upper)

    for i in range(params_shape):

        minlim = p_lower[i] - 0.1 * p_lower[i]
        maxlim = p_upper[i] + 0.1 * p_upper[i]

        x_plot = np.arange(minlim, maxlim, 0.001).reshape(-1, 1)
        y_plot = posterior.eval(x_plot, ii=[i], log=False)
        y_plot_prior = p.eval(x_plot, ii=[i], log=False)
        plt.plot(x_plot, y_plot, '-b', label=r'Predicted posterior')
        plt.plot(x_plot, y_plot_prior, '-g', label=r'Uniform prior')
        cur_true_param = true_obs.ravel()[i]
        plt.axvline(cur_true_param,
                    c='r',
                    label=r'True value of ' + env_params[i])
        plt.legend(fontsize=10)
        plt.axis('on')
        plt.xlabel(env_params[i] + ' values')
        # fig.savefig('/tmp/'+env_params[i]+'.pdf', bbox_inches='tight')
        plt.show()
Пример #5
0
def prior(true_params,
          prior_uniform=True,
          prior_extent=False,
          prior_log=False,
          seed=None):
    """Prior"""
    if not prior_extent:
        range_lower = param_transform(prior_log, 0.5 * true_params)
        range_upper = param_transform(prior_log, 1.5 * true_params)
    else:
        range_lower = param_transform(
            prior_log, np.array([.5, 1e-4, 1e-4, 1e-4, 50., 40., 1e-4, 35.]))
        range_upper = param_transform(
            prior_log, np.array([80., 15., .6, .6, 3000., 90., .15, 100.]))

        range_lower = range_lower[0:len(true_params)]
        range_upper = range_upper[0:len(true_params)]

    if prior_uniform:
        prior_min = range_lower
        prior_max = range_upper
        return dd.Uniform(lower=prior_min, upper=prior_max, seed=seed)
    else:
        prior_mn = param_transform(prior_log, true_params)
        prior_cov = np.diag((range_upper - range_lower)**2) / 12
        return dd.Gaussian(m=prior_mn, S=prior_cov, seed=seed)
Пример #6
0
def main(args):
    log = prep_log(args)

    log.info("Running {}.".format(args.name))
    if args.verbose:
        log.info(args)

    prior_lims = np.array([[0, 1], [-10., 10.], [-120., 120.], [0., 2000],
                           [0., 0.5], [0, 0.05], [0., 0.5], [0, 0.05]])

    if args.verbose:
        log.info('prior')
        log.info(prior_lims)

    m = ChannelOmni(third_exp_model=False, seed=args.seed)
    p = dd.Uniform(lower=prior_lims[:, 0], upper=prior_lims[:, 1])
    s = ChannelStats()
    g = Default(model=m, prior=p, summary=s)

    tic = time.time()
    dats = g.gen(args.n_samples)
    toc = time.time()
    log.info('Generation took {}s.'.format(toc - tic))

    np.save('{}/theta.npy'.format(args.bp), dats[0])
    np.save('{}/stats.npy'.format(args.bp), dats[1])
Пример #7
0
def test_TransformedDistribution(seed=5, nsamples=1000, ndim=2):
    lower = np.random.rand(ndim)
    upper = np.random.rand(ndim) + 2
    dist = dd.Uniform(lower, upper)

    dist.reseed(seed)
    z = dist.gen(nsamples)

    inputscale = lambda x: (x - lower) / (upper - lower)
    bijection = lambda x: logit(inputscale(x)
                                )  # logit function with scaled input
    inverse_bijection = lambda y: expit(y) * (
        upper - lower) + lower  # logistic function with scaled output
    bijection_jac_logD = lambda x: -(np.log(
        inputscale(x) *
        (1 - inputscale(x))) + np.log(upper - lower)).sum(axis=-1)

    dist_transformed = dd.TransformedDistribution(
        distribution=dist,
        bijection=bijection,
        inverse_bijection=inverse_bijection,
        bijection_jac_logD=bijection_jac_logD)
    dist_transformed.reseed(seed)
    z_transformed = dist_transformed.gen(nsamples)
    assert np.allclose(z_transformed, bijection(z), atol=1e-8)

    dist_logistic = dd.Logistic(mu=np.zeros(ndim))

    assert np.allclose(dist_logistic.eval(z_transformed, log=False), dist_transformed.eval(z_transformed, log=False)), \
        "incorrect density"
    assert np.allclose(dist_logistic.eval(z_transformed, log=True), dist_transformed.eval(z_transformed, log=True)), \
        "incorrect log density"
Пример #8
0
def test_uniform_2d():
    N = 1000
    lower = [1., 3.]
    upper = [2., 4.]
    dist = dd.Uniform(lower, upper, seed=seed)
    samples = dist.gen(N)
    logprobs = dist.eval(samples)

    assert samples.shape == (N, 2)
    assert logprobs.shape == (N,)
Пример #9
0
def baseParamInit():

    N = 20  # number of trials at each orientation
    sensitivity_true = 1.2
    #     bias_true = .8
    bias_true = 2

    labels = ['sensitivity', 'bias']
    true_params = [sensitivity_true, bias_true]

    seed_p = 2

    prior_apt = dd.Uniform(lower=np.asarray([0.1, -5]),
                           upper=np.asarray([9.9, 5]),
                           seed=seed_p)

    analyticModel = None
    apt_model = None
    s = None

    hyps = {
        'prior': prior_apt,
        'm': apt_model,
        's': s,
        'n_train': 5000,
        'n_rounds': 3,
        'n_hiddens': [50, 50],
        'pilot_samples': 2000,
        'val_frac': 0.05,
        'minibatch': 500,
        'epochs': 100,
        'density': 'maf',
        'n_mades': 5,
        'seed_inf': 1
    }

    n_steps = 15000

    fignames = ''
    folderName = ''

    param_dict = {
        'N': N,
        'true_params': true_params,
        'hyps': hyps,
        'analyticModel': analyticModel,
        'fignames': fignames,
        'folderName': folderName,
        'n_steps': n_steps
    }

    return param_dict
Пример #10
0
def baseParamInit():

    N = 1000
    alpha_true = .5
    beta_true = -2
    err = 0.4  # err to be fixed
    labels = ['slope', 'intercept']
    true_params = [alpha_true, beta_true]

    seed_p = 2

    prior_apt = dd.Uniform(lower=np.asarray([-10, -10]),
                           upper=np.asarray([10, 10]),
                           seed=seed_p)
    analyticModel = None
    apt_model = None
    s = None

    hyps = {
        'prior': prior_apt,
        'm': apt_model,
        's': s,
        'n_train': 5000,
        'n_rounds': 3,
        'n_hiddens': [50, 50],
        'pilot_samples': 2000,
        'val_frac': 0.05,
        'minibatch': 500,
        'epochs': 100,
        'density': 'maf',
        'n_mades': 5,
        'seed_inf': 1
    }

    n_steps = 15000

    fignames = ''
    folderName = ''

    param_dict = {
        'N': N,
        'true_params': true_params,
        'hyps': hyps,
        'analyticModel': analyticModel,
        'fignames': fignames,
        'folderName': folderName,
        'n_steps': n_steps,
        'err': err
    }

    return param_dict
Пример #11
0
def get_maprf_prior_01(params_ls, seed=None, no_transform=False):
    ## prior over simulation parameters
    prior = collections.OrderedDict()

    if no_transform:
        lims = np.array([[-1.5, -1.1, .001,         0.01,          .001, 0.01, 0.01, -.999, -.999],
                         [ 1.5,  1.1, .999*np.pi, 2.49,   1.999*np.pi, 1.99, 3.99, .999,   .999]]).T
        p = dd.Uniform(lower=lims[:,0], upper=lims[:,1])
        return p, prior

    ## prior over simulation parameters
    if 'bias' in params_ls['glm']:
        prior['bias'] = {'mu' : np.array([-0.57]), 'sigma' : np.array([np.sqrt(1.63)]) }
    if 'gain' in params_ls['kernel']['s']:
#        prior['A'] = {'mu' : np.zeros(1), 'sigma' : 2 * np.ones(1) }
        prior['log_A'] = {'mu' : np.zeros(1), 'sigma' : np.ones(1) / 2 }  
    if 'phase' in params_ls['kernel']['s']:
        prior['logit_φ']  = {'mu' : np.array([0]), 'sigma' : np.array([1.9]) }
    if 'freq' in params_ls['kernel']['s']:
        prior['log_f']  = {'mu' : np.zeros(1), 'sigma' : np.ones(1) / 2 }
    if 'angle' in params_ls['kernel']['s']:
        prior['logit_θ']  = {'mu' : np.zeros(1), 'sigma' :   np.array([1.78]) }
    if 'ratio' in params_ls['kernel']['s']:
        prior['log_γ']  = {'mu' : np.zeros(1), 'sigma' : np.ones(1) / 2}
    if 'width' in params_ls['kernel']['s']:
        prior['log_b']  = {'mu' : np.zeros(1), 'sigma' : np.ones(1) / 2}
    if 'xo' in params_ls['kernel']['l']:
        prior['logit_xo'] = {'mu' : np.array([0.]), 'sigma' : np.array([1.78]) }
        #prior['xo'] = {'mu' : np.array([0.]), 'sigma' : np.array([1./np.sqrt(4)]) }
    if 'yo' in params_ls['kernel']['l']:
        prior['logit_yo'] = {'mu' : np.array([0.]), 'sigma' : np.array([1.78]) }
        #prior['yo'] = {'mu' : np.array([0.]), 'sigma' : np.array([1./np.sqrt(4)]) }
    L = np.diag(np.concatenate([prior[i]['sigma'] for i in list(prior.keys())]))
    if 'value' in params_ls['kernel']['t']:
        ax_t = m.dt * np.arange(1,len_kt+1)
        Λ =  np.diag(ax_t / 0.075 * np.exp(1 - ax_t / 0.075))
        D = np.eye(ax_t.shape[0]) - np.eye(ax_t.shape[0], k=-1)
        F = np.dot(D, D.T)
        Σ = np.dot(Λ, np.linalg.inv(F).dot(Λ))
        prior['kt'] = {'mu': np.zeros_like(ax_t), 'sigma': np.linalg.inv(D).dot(Λ)}
        L = np.block([[L, np.zeros((L.shape[0], ax_t.size))],
                      [np.zeros((ax_t.size, L.shape[1])), prior['kt']['sigma']]])
    mu  = np.concatenate([prior[i][ 'mu'  ] for i in prior.keys()])
    p = dd.Gaussian(m=mu, S=L.T.dot(L), seed=seed)

    return p, prior
def eval_log_pdf(point, pdf, check_prior):
    if check_prior:
        prior = dd.Uniform(-np.sqrt(3) * np.ones(len(point)),
                           np.sqrt(3) * np.ones(len(point)))
        if not np.all(params_are_bounded(point, prior, normalized=True)):
            return 1e20
        else:
            try:
                output = -pdf.eval([point])
            except:
                output = -pdf.eval(point)
            return output
    else:
        try:
            output = -pdf.eval([point])
        except:
            output = -pdf.eval(point)
        return output
Пример #13
0
def prior(true_params, seed=None, prior_log=False, prior_uniform=False):
    """Prior"""
    range_lower = param_transform(prior_log ,0.5*true_params)
    range_upper = param_transform(prior_log, 1.5*true_params)

    range_lower = range_lower[0:len(true_params)]
    range_upper = range_upper[0:len(true_params)]

    if prior_uniform:
        prior_min = range_lower
        prior_max = range_upper

        return dd.Uniform(lower=prior_min, upper=prior_max,
                               seed=seed)
    else:
        prior_mn = param_transform(prior_log,true_params)
        prior_cov = np.diag((range_upper - range_lower)**2)/12

        return dd.Gaussian(m=prior_mn, S=prior_cov, seed=seed)
import delfi.distribution as dd

################################################################################
# seeding by time stamp
seed1 = time.time()
seed = int((seed1 % 1) * 1e7)
rng = np.random.RandomState(seed=seed)

################################################################################
hyper_params = netio.load_setup("train_31D_R1_BigPaper")

# define prior
seed_p = rng.randint(1e7)
hyper_params.seed = seed_p
num_dim = np.sum(hyper_params.use_membrane) + 7
p = dd.Uniform(-np.sqrt(3) * np.ones(num_dim), np.sqrt(3) * np.ones(num_dim), seed=hyper_params.seed)

################################################################################
# sample from prior and run simulations
pool_size = 28  # 28 worked

import dill as pickle

with open('results/31D_nets/191001_seed1_Exper11deg.pkl', 'rb') as file:
    inf_SNPE_MAF, log, params = pickle.load(file)

summstats_experimental = np.load('results/31D_experimental/190807_summstats_prep845_082_0044.npz')['summ_stats']
posterior_prev_round = inf_SNPE_MAF.predict([summstats_experimental])


# sample from prior and run simulations
Пример #15
0
################################################################################
#                                   Load data                                  #
################################################################################

filedir = "results/31D_samples/pyloricsamples_31D_noNaN_3.npz"
pilot_data, _, params_mean, params_std = tu.load_trn_data_normalize(
    filedir, params)
print('We use', len(pilot_data[0]), 'training samples.')

################################################################################
#                                  Define prior                                #
################################################################################

# create standard prior for the actual conductances
prior = netio.create_prior(params, log=True)
prior_norm = dd.Uniform(lower=-np.sqrt(3) * np.ones(dimensions),
                        upper=np.sqrt(3) * np.ones(dimensions))

################################################################################
#                                  Load pairs                                  #
################################################################################

xo_stats = np.load(
    'results/31D_experimental/190807_summstats_prep845_082_0044.npz'
)['summ_stats']
obs = xo_stats

################################################################################
#                               Define ss and g                                #
################################################################################

# Using the 'PrinzStats' here, defined in summstats.py. Inferring the summstats
Пример #16
0
def create_prior(params, log=False):

    if params.novel_prior:
        assert params.comp_neurons is None, "Is you are using a novel prior, you can not use comp_neurons"

        # rows:    LP, PY, PD
        # columns: the eight membrane conductances
        # contains the minimal values that were used by Prinz et al.
        low_val = 0.0
        membrane_cond_mins = np.asarray([
            [100, 2.5, 2, 10, 5, 50, 0.01, low_val],  # PM
            [100, low_val, 4, 20, low_val, 25, low_val, 0.02],  # LP
            [100, 2.5, low_val, 40, low_val, 75, low_val, low_val]
        ]) * 0.628e-3  # PY

        # contains the maximal values that were used by Prinz et al.
        membrane_cond_maxs = np.asarray([
            [400, 5.0, 6, 50, 10, 125, 0.01, low_val],  # PM
            [100, low_val, 10, 50, 5, 100, 0.05, 0.03],  # LP
            [500, 10, 2, 50, low_val, 125, 0.05, 0.03]
        ]) * 0.628e-3  # PY

        ranges = np.asarray([100, 2.5, 2, 10, 5, 25, 0.01, 0.01]) * 0.628e-3
        membrane_cond_mins = membrane_cond_mins - ranges
        membrane_cond_maxs = membrane_cond_maxs + ranges

        membrane_cond_mins[membrane_cond_mins < 0.0] = 0.0

        use_membrane = np.asarray(params.use_membrane)
        membrane_used_mins = membrane_cond_mins[use_membrane == True].flatten()
        membrane_used_maxs = membrane_cond_maxs[use_membrane == True].flatten()

        # proctolin
        proctolin_gbar_mins = [0.0, 0.0, 0.0]
        proctolin_gbar_maxs = np.asarray([6.0, 8.0, 0.0]) * 1e-6
        use_proctolin = params.use_proctolin

        syn_dim_mins = np.ones_like(
            params.true_params
        ) * params.syn_min  # syn_min is the start of uniform interval
        syn_dim_maxs = np.ones_like(
            params.true_params
        ) * params.syn_max  # syn_max is the end of uniform interval
        syn_dim_maxs[0] *= 10.0

        if log:
            syn_dim_mins = np.log(syn_dim_mins)
            syn_dim_maxs = np.log(syn_dim_maxs)

        # q10 values for synapses
        gbar_q10_syn_mins = np.asarray([1.0, 1.0])
        gbar_q10_syn_maxs = np.asarray([2.0, 2.0])
        tau_q10_syn_mins = np.asarray([1.0, 1.0])
        tau_q10_syn_maxs = np.asarray([4.0, 4.0])
        use_gbar_syn = np.asarray(params.Q10_gbar_syn)
        use_tau_syn = np.asarray(params.Q10_tau_syn)
        gbar_q10_syn_used_mins = gbar_q10_syn_mins[use_gbar_syn].flatten()
        gbar_q10_syn_used_maxs = gbar_q10_syn_maxs[use_gbar_syn].flatten()
        tau_q10_syn_used_mins = tau_q10_syn_mins[use_tau_syn].flatten()
        tau_q10_syn_used_maxs = tau_q10_syn_maxs[use_tau_syn].flatten()

        # q10 values for membrane
        gbar_q10_mem_mins = np.asarray(
            [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
        gbar_q10_mem_maxs = np.asarray(
            [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0])
        use_gbar_mem = np.asarray(params.Q10_gbar_mem)
        gbar_q10_mem_used_mins = gbar_q10_mem_mins[use_gbar_mem].flatten()
        gbar_q10_mem_used_maxs = gbar_q10_mem_maxs[use_gbar_mem].flatten()

        if use_proctolin:
            membrane_and_sny_mins = np.concatenate(
                (membrane_used_mins, proctolin_gbar_mins, syn_dim_mins,
                 gbar_q10_syn_used_mins, tau_q10_syn_used_mins,
                 gbar_q10_mem_used_mins))
            membrane_and_sny_maxs = np.concatenate(
                (membrane_used_maxs, proctolin_gbar_maxs, syn_dim_maxs,
                 gbar_q10_syn_used_maxs, tau_q10_syn_used_maxs,
                 gbar_q10_mem_used_maxs))
        else:
            membrane_and_sny_mins = np.concatenate(
                (membrane_used_mins, syn_dim_mins, gbar_q10_syn_used_mins,
                 tau_q10_syn_used_mins, gbar_q10_mem_used_mins))
            membrane_and_sny_maxs = np.concatenate(
                (membrane_used_maxs, syn_dim_maxs, gbar_q10_syn_used_maxs,
                 tau_q10_syn_used_maxs, gbar_q10_mem_used_maxs))
    else:
        if params.comp_neurons is not None:
            neuron_1 = np.asarray(create_neurons(params.comp_neurons[0]))
            neuron_2 = np.asarray(create_neurons(params.comp_neurons[1]))
            membrane_cond_mins = np.minimum(neuron_1, neuron_2)
            membrane_cond_maxs = np.maximum(neuron_1, neuron_2)
            membrane_cond_mins[membrane_cond_mins == 0.0] += 1e-20
            membrane_cond_maxs[membrane_cond_maxs == 0.0] += 1e-20
            params.use_membrane = membrane_cond_mins != membrane_cond_maxs
        else:
            # rows:    LP, PY, PD
            # columns: the eight membrane conductances
            # contains the minimal values that were used by Prinz et al.
            low_val = 0.0
            membrane_cond_mins = np.asarray([
                [100, 2.5, 2, 10, 5, 50, 0.01, low_val],  # PM
                [100, low_val, 4, 20, low_val, 25, low_val, 0.02],  # LP
                [100, 2.5, low_val, 40, low_val, 75, low_val, low_val]
            ]) * 0.628e-3  # PY

            # contains the maximal values that were used by Prinz et al.
            membrane_cond_maxs = np.asarray([
                [400, 5.0, 6, 50, 10, 125, 0.01, low_val],  # PM
                [100, low_val, 10, 50, 5, 100, 0.05, 0.03],  # LP
                [500, 10, 2, 50, low_val, 125, 0.05, 0.03]
            ]) * 0.628e-3  # PY

        security_factor = 1.25  # factor that we increase the margin used by Prinz et al. with
        membrane_cond_mins *= (2.0 - security_factor)
        membrane_cond_maxs *= security_factor

        membrane_cond_maxs[membrane_cond_maxs == 0.0] = np.asarray(
            [1.0, 1.0, 0.01]) * 0.628e-3

        use_membrane = np.asarray(params.use_membrane)
        membrane_used_mins = membrane_cond_mins[use_membrane == True].flatten()
        membrane_used_maxs = membrane_cond_maxs[use_membrane == True].flatten()

        syn_dim_mins = np.ones_like(
            params.true_params
        ) * params.syn_min  # syn_min is the start of uniform interval
        syn_dim_maxs = np.ones_like(
            params.true_params
        ) * params.syn_max  # syn_max is the end of uniform interval

        if log:
            syn_dim_mins = np.log(syn_dim_mins)
            syn_dim_maxs = np.log(syn_dim_maxs)

        membrane_and_sny_mins = np.concatenate(
            (membrane_used_mins, syn_dim_mins))
        membrane_and_sny_maxs = np.concatenate(
            (membrane_used_maxs, syn_dim_maxs))

    return dd.Uniform(membrane_and_sny_mins,
                      membrane_and_sny_maxs,
                      seed=int(params.seed))