예제 #1
0
def doMCMC(n, nxx, nxy, nyy, x):

    # Optional setting for reproducibility
    use_seed = False

    d = nxx.shape[0]
    ns = 2000
    if use_seed:  # optional setting for reproducibility
        seed = 42

    # Disable printing
    sys.stdout = open(os.devnull, 'w')

    # Sufficient statistics
    NXX = shared(nxx)
    NXY = shared(nxy)
    NYY = shared(nyy)

    # Define model and perform MCMC sampling
    with Model() as model:

        # Fixed hyperparameters for priors
        b0 = Deterministic('b0', th.zeros((d), dtype='float64'))
        ide = Deterministic('ide', th.eye(d, m=d, k=0, dtype='float64'))

        # Priors for parameters
        l0 = Gamma('l0', alpha=2.0, beta=2.0)
        l = Gamma('l', alpha=2.0, beta=2.0)
        b = MvNormal('b', mu=b0, tau=l0 * ide, shape=d)

        # Custom log likelihood
        def logp(xtx, xty, yty):
            return (n / 2.0) * th.log(l / (2 * np.pi)) + (-l / 2.0) * (
                th.dot(th.dot(b, xtx), b) - 2 * th.dot(b, xty) + yty)

        # Likelihood
        delta = DensityDist('delta',
                            logp,
                            observed={
                                'xtx': NXX,
                                'xty': NXY,
                                'yty': NYY
                            })

        # Inference
        print('doMCMC: start NUTS')
        step = NUTS()
        if use_seed:
            trace = sample(ns, step, progressbar=True, random_seed=seed)
        else:
            trace = sample(ns, step, progressbar=True)

    # Enable printing
    sys.stdout = sys.__stdout__

    # Compute prediction over posterior
    return np.mean([np.dot(x, trace['b'][i]) for i in range(ns)], 0)
예제 #2
0
def gaussian(x, y):
    X = x.reshape(-1, 1)
    Z = linspace(-6, 6, 100).reshape(-1, 1)
    with Model() as gp_fit:
        ρ = Gamma('ρ', 1, 1)
        nu = Gamma('nu', 1, 1)
        K = nu * gp.cov.Matern32(1, ρ)
    with gp_fit:
        M = gp.mean.Zero()
        σ = HalfCauchy('σ', 2.5)
    with gp_fit:
        y_obs = gp.GP('y_obs', mean_func=M, cov_func=K, sigma=σ, observed={'X': X, 'Y': y})
    with gp_fit:
        gp_samples = pm.gp.sample_gp(trace[1000:], y_obs, Z, samples=50)
예제 #3
0
def doADVI(n, xx, xy, yy, x):

    d = xx.shape[0]
    ns = 5000
    seed = 42  # for reproducibility

    # Disable printing
    sys.stdout = open(os.devnull, 'w')

    # Sufficient statistics
    NXX = shared(xx)
    NXY = shared(xy)
    NYY = shared(yy)

    # Define model and perform MCMC sampling
    with Model() as model:

        # Fixed hyperparameters for priors
        b0 = Deterministic('b0', th.zeros((d), dtype='float64'))
        ide = Deterministic('ide', th.eye(d, m=d, k=0, dtype='float64'))

        # Priors for parameters
        l0 = Gamma('l0', alpha=2.0, beta=2.0)
        l = Gamma('l', alpha=2.0, beta=2.0)
        b = MvNormal('b', mu=b0, tau=l0 * ide, shape=d)

        # Custom log likelihood
        def logp(xtx, xty, yty):
            return (n / 2.0) * th.log(l / (2 * np.pi)) + (-l / 2.0) * (
                th.dot(th.dot(b, xtx), b) - 2 * th.dot(b, xty) + yty)

        # Likelihood
        delta = DensityDist('delta',
                            logp,
                            observed={
                                'xtx': NXX,
                                'xty': NXY,
                                'yty': NYY
                            })

        # Inference
        v_params = advi(n=ns, random_seed=seed)
        trace = sample_vp(v_params, draws=ns, random_seed=seed)

    # Enable printing
    sys.stdout = sys.__stdout__

    # Compute prediction over posterior
    return np.mean([np.dot(x, trace['b'][i]) for i in range(ns)], 0)
예제 #4
0
    def test_normal_mixture(self):
        with Model() as model:
            w = Dirichlet('w', np.ones_like(self.norm_w))

            mu = Normal('mu', 0., 10., shape=self.norm_w.size)
            tau = Gamma('tau', 1., 1., shape=self.norm_w.size)

            x_obs = NormalMixture('x_obs',
                                  w,
                                  mu,
                                  tau=tau,
                                  observed=self.norm_x)

            step = Metropolis()
            trace = sample(5000,
                           step,
                           random_seed=self.random_seed,
                           progressbar=False)

        assert_allclose(np.sort(trace['w'].mean(axis=0)),
                        np.sort(self.norm_w),
                        rtol=0.1,
                        atol=0.1)
        assert_allclose(np.sort(trace['mu'].mean(axis=0)),
                        np.sort(self.norm_mu),
                        rtol=0.1,
                        atol=0.1)
예제 #5
0
    def test_mixture_list_of_normals(self):
        with Model() as model:
            w = Dirichlet('w', floatX(np.ones_like(self.norm_w)))
            mu = Normal('mu', 0., 10., shape=self.norm_w.size)
            tau = Gamma('tau', 1., 1., shape=self.norm_w.size)
            Mixture('x_obs',
                    w, [
                        Normal.dist(mu[0], tau=tau[0]),
                        Normal.dist(mu[1], tau=tau[1])
                    ],
                    observed=self.norm_x)
            step = Metropolis()
            trace = sample(5000,
                           step,
                           random_seed=self.random_seed,
                           progressbar=False,
                           chains=1)

        assert_allclose([
            np.sort(trace['w'].mean(axis=0)),
            np.sort(trace['mu'].mean(axis=0))
        ], [np.sort(self.norm_w), np.sort(self.norm_mu)],
                        rtol=0.1,
                        atol=0.1)
        assert_allclose(np.sort(trace['mu'].mean(axis=0)),
                        np.sort(self.norm_mu),
                        rtol=0.1,
                        atol=0.1)
예제 #6
0
    def test_mixture_list_of_poissons(self):
        with Model() as model:
            w = Dirichlet('w',
                          floatX(np.ones_like(self.pois_w)),
                          shape=self.pois_w.shape)
            mu = Gamma('mu', 1., 1., shape=self.pois_w.size)
            Mixture(
                'x_obs',
                w,
                [Poisson.dist(mu[0]), Poisson.dist(mu[1])],
                observed=self.pois_x)
            step = Metropolis()
            trace = sample(5000,
                           step,
                           random_seed=self.random_seed,
                           progressbar=False,
                           chains=1)

        assert_allclose(np.sort(trace['w'].mean(axis=0)),
                        np.sort(self.pois_w),
                        rtol=0.1,
                        atol=0.1)
        assert_allclose(np.sort(trace['mu'].mean(axis=0)),
                        np.sort(self.pois_mu),
                        rtol=0.1,
                        atol=0.1)
예제 #7
0
    def test_mixture_list_of_normals(self):
        with Model() as model:
            w = Dirichlet("w",
                          floatX(np.ones_like(self.norm_w)),
                          shape=self.norm_w.size)
            mu = Normal("mu", 0.0, 10.0, shape=self.norm_w.size)
            tau = Gamma("tau", 1.0, 1.0, shape=self.norm_w.size)
            Mixture(
                "x_obs",
                w,
                [
                    Normal.dist(mu[0], tau=tau[0]),
                    Normal.dist(mu[1], tau=tau[1])
                ],
                observed=self.norm_x,
            )
            step = Metropolis()
            trace = sample(5000,
                           step,
                           random_seed=self.random_seed,
                           progressbar=False,
                           chains=1)

        assert_allclose(np.sort(trace["w"].mean(axis=0)),
                        np.sort(self.norm_w),
                        rtol=0.1,
                        atol=0.1)
        assert_allclose(np.sort(trace["mu"].mean(axis=0)),
                        np.sort(self.norm_mu),
                        rtol=0.1,
                        atol=0.1)
예제 #8
0
    def test_poisson_mixture(self):
        with Model() as model:
            w = Dirichlet("w", floatX(np.ones_like(self.pois_w)), shape=self.pois_w.shape)
            mu = Gamma("mu", 1.0, 1.0, shape=self.pois_w.size)
            Mixture("x_obs", w, Poisson.dist(mu), observed=self.pois_x)
            step = Metropolis()
            trace = sample(5000, step, random_seed=self.random_seed, progressbar=False, chains=1)

        assert_allclose(np.sort(trace["w"].mean(axis=0)), np.sort(self.pois_w), rtol=0.1, atol=0.1)
        assert_allclose(
            np.sort(trace["mu"].mean(axis=0)), np.sort(self.pois_mu), rtol=0.1, atol=0.1
        )
예제 #9
0
    def test_normal_mixture_nd(self):
        nd, ncomp = 3, 5

        with Model() as model0:
            mus = Normal('mus', shape=(nd, ncomp))
            taus = Gamma('taus', alpha=1, beta=1, shape=(nd, ncomp))
            ws = Dirichlet('ws', np.ones(ncomp))
            mixture0 = NormalMixture('m', w=ws, mu=mus, tau=taus, shape=nd)

        with Model() as model1:
            mus = Normal('mus', shape=(nd, ncomp))
            taus = Gamma('taus', alpha=1, beta=1, shape=(nd, ncomp))
            ws = Dirichlet('ws', np.ones(ncomp))
            comp_dist = [
                Normal.dist(mu=mus[:, i], tau=taus[:, i]) for i in range(ncomp)
            ]
            mixture1 = Mixture('m', w=ws, comp_dists=comp_dist, shape=nd)

        testpoint = model0.test_point
        testpoint['mus'] = np.random.randn(nd, ncomp)
        assert_allclose(model0.logp(testpoint), model1.logp(testpoint))
        assert_allclose(mixture0.logp(testpoint), mixture1.logp(testpoint))
예제 #10
0
def _indvdl_gg(
    hparams, std_x, n_samples, L_cov, Normal, Gamma, Deterministic, sgn, gamma, 
    floatX, cholesky, tt, verbose):
    # Uniform distribution on sphere
    gs = Normal('gs', np.float32(0.0), np.float32(1.0), 
                shape=(n_samples, 2), dtype=floatX)
    ss = Deterministic('ss', gs + sgn(sgn(gs) + np.float32(1e-10)) * 
                             np.float32(1e-10))
    ns = Deterministic('ns', ss.norm(L=2, axis=1)[:, np.newaxis])
    us = Deterministic('us', ss / ns)

    # Scaling s.t. variance to 1
    n = 2 # dimension
    beta = np.float32(hparams['beta_coeff'])
    m = n * gamma(0.5 * n / beta) \
        / (2 ** (1 / beta) * gamma((n + 2) / (2 * beta)))
    L_cov_ = (np.sqrt(m) * cholesky(L_cov)).astype(floatX)

    # Scaling to v_indvdls
    scale1 = np.float32(std_x[0] * hparams['v_indvdl_1'])
    scale2 = np.float32(std_x[1] * hparams['v_indvdl_2'])
    tt.set_subtensor(L_cov_[0, :], L_cov_[0, :] * scale1, inplace=True)
    tt.set_subtensor(L_cov_[1, :], L_cov_[1, :] * scale2, inplace=True)

    # Draw samples
    ts = Gamma(
        'ts', alpha=np.float32(n / (2 * beta)), beta=np.float32(.5), 
        shape=n_samples, dtype=floatX
    )[:, np.newaxis]
    mus_ = Deterministic(
        'mus_', ts**(np.float32(0.5 / beta)) * us.dot(L_cov_)
    )
    mu1s_ = mus_[:, 0]
    mu2s_ = mus_[:, 1]

    if 10 <= verbose:
        print('GG for individual effect')
        print('gs.dtype = {}'.format(gs.dtype))
        print('ss.dtype = {}'.format(ss.dtype))
        print('ns.dtype = {}'.format(ns.dtype))
        print('us.dtype = {}'.format(us.dtype))
        print('ts.dtype = {}'.format(ts.dtype))

    return mu1s_, mu2s_
예제 #11
0
def run_dirpfa(args):
    tf_vectorizer, docs_tr, docs_te = prepare_sparse_matrix_nonlabel(args.n_tr, args.n_te, args.n_word)
    feature_names = tf_vectorizer.get_feature_names()
    doc_tr_minibatch = pm.Minibatch(docs_tr.toarray(), args.bsz)
    doc_tr = shared(docs_tr.toarray()[:args.bsz])

    def log_prob(beta, theta, n):
        """Returns the log-likelihood function for given documents.

        K : number of topics in the model
        V : number of words (size of vocabulary)
        D : number of documents (in a mini-batch)

        Parameters
        ----------
        beta : tensor (K x V)
            Word distributions.
        theta : tensor (D x K)
            Topic distributions for documents.
        n: tensor (D x 1)
            Expected lengths of each documents
        """

        def ll_docs_f(docs):
            dixs, vixs = docs.nonzero()
            vfreqs = docs[dixs, vixs]
            ll_docs = (vfreqs *
                       (pmmath.logsumexp(tt.log(theta[dixs]) + tt.log(beta.T[vixs]),
                                                  axis=1).ravel() + tt.log(
                n[dixs]).ravel()) - tt.exp(
                pmmath.logsumexp(tt.log(theta[dixs]) + tt.log(beta.T[vixs], ), axis=1)).ravel() * n[
                           dixs].ravel() - pm.distributions.special.gammaln(vfreqs + 1)

                       )
            return tt.sum(ll_docs) / (tt.sum(vfreqs) + 1e-9)

        return ll_docs_f

    with pm.Model() as model:
        n = Gamma("n",
                  alpha=pm.floatX(10. * np.ones((args.bsz, 1))),
                  beta=pm.floatX(0.1 * np.ones((args.bsz, 1))), shape=(args.bsz, 1),
                  total_size=args.n_tr)

        beta = Dirichlet("beta",
                         a=pm.floatX((1. / args.n_topic) * np.ones((args.n_topic, args.n_word))),
                         shape=(args.n_topic, args.n_word), )

        theta = Dirichlet("theta",
                          a=pm.floatX((10. / args.n_topic) * np.ones((args.bsz, args.n_topic))),
                          shape=(args.bsz, args.n_topic),
                          total_size=args.n_tr, )

        doc = pm.DensityDist("doc", log_prob(beta, theta, n), observed=doc_tr)

    encoder = ThetaNEncoder(n_words=args.n_word, n_hidden=100, n_topics=args.n_topic)
    local_RVs = OrderedDict([(theta, encoder.encode(doc_tr)[0]), (n, encoder.encode(doc_tr)[1])])
    encoder_params = encoder.get_params()
    s = shared(args.lr)
    def reduce_rate(a, h, i):
        s.set_value(args.lr / ((i / args.bsz) + 1) ** 0.7)

    with model:
        approx = pm.MeanField(local_rv=local_RVs)
        approx.scale_cost_to_minibatch = False
        inference = pm.KLqp(approx)

    inference.fit(args.n_iter,
                  callbacks=[reduce_rate, pm.callbacks.CheckParametersConvergence(diff="absolute")],
                  obj_optimizer=pm.adam(learning_rate=s), more_obj_params=encoder_params,
                  total_grad_norm_constraint=200,
                  more_replacements={ doc_tr: doc_tr_minibatch }, )

    doc_tr.set_value(docs_tr.toarray())

    inp = tt.matrix(dtype="int64")
    sample_vi_theta = theano.function([inp],
        approx.sample_node(approx.model.theta, args.n_sample, more_replacements={ doc_tr: inp
                                                                                    }), )
    sample_vi_n = theano.function([inp],
        approx.sample_node(approx.model.n, args.n_sample, more_replacements={ doc_tr: inp }))

    test = docs_te.toarray()
    test_n = test.sum(1)

    beta_pymc3 = pm.sample_approx(approx, draws=args.n_sample)['beta']
    theta_pymc3 = sample_vi_theta(test)
    n_pymc3 = sample_vi_n(test)

    assert beta_pymc3.shape == (args.n_sample, args.n_topic, args.n_word)
    assert theta_pymc3.shape == (args.n_sample, args.n_te, args.n_topic)
    assert n_pymc3.shape == (args.n_sample, args.n_te, 1)

    beta_mean = beta_pymc3.mean(0)
    theta_mean = theta_pymc3.mean(0)
    n_mean = n_pymc3.mean(0)

    pred_rate = theta_mean.dot(beta_mean) * n_mean
    pp_test = (test * np.log(pred_rate) - pred_rate - sc.gammaln(test + 1)).sum(1) / test_n

    posteriors = { 'theta': theta_pymc3, 'beta': beta_pymc3, 'n': n_pymc3}

    log_top_words(beta_pymc3.mean(0), feature_names, n_top_words=args.n_top_word)
    save_elbo(approx.hist)
    save_pp(pp_test)
    save_draws(posteriors)
예제 #12
0
def hmetad_rm1way(data: dict, sample_model: bool = True, **kwargs: int):
    """Compute hierachical meta-d' at the subject level.

    This is an internal function. The repeated measures model must be
    called using :py:func:`metadPy.hierarchical.hmetad`.

    Parameters
    ----------
    data : dict
        Response data.
    sample_model : boolean
        If `False`, only the model is returned without sampling.
    **kwargs : keyword arguments
        All keyword arguments are passed to `func::pymc3.sampling.sample`.

    Returns
    -------
    model : :py:class:`pymc3.Model` instance
        The pymc3 model. Encapsulates the variables and likelihood factors.
    trace : :py:class:`pymc3.backends.base.MultiTrace` or
        :py:class:`arviz.InferenceData`
        A `MultiTrace` or `ArviZ InferenceData` object that contains the
        samples.

    References
    ----------
    .. [#] Fleming, S.M. (2017) HMeta-d: hierarchical Bayesian estimation
    of metacognitive efficiency from confidence ratings, Neuroscience of
    Consciousness, 3(1) nix007, https://doi.org/10.1093/nc/nix007
    """
    nSubj = data["nSubj"]
    nCond = data["nCond"]
    nRatings = data["nRatings"]
    hits = data["hits"].reshape(nSubj, 2)
    falsealarms = data["falsealarms"].reshape(nSubj, 2)
    counts = data["counts"]
    Tol = data["Tol"]
    cr = data["cr"].reshape(nSubj, 2)
    m = data["m"].reshape(nSubj, 2)
    c1 = data["c1"].reshape(nSubj, 2, 1)
    d1 = data["d1"].reshape(nSubj, 2, 1)

    with Model() as model:

        #############
        # Hyperpriors
        #############
        mu_c2 = Normal("mu_c2",
                       tau=0.01,
                       shape=(1, ),
                       testval=np.random.rand() * 0.1)
        sigma_c2 = HalfNormal("sigma_c2",
                              tau=0.01,
                              shape=(1, ),
                              testval=np.random.rand() * 0.1)

        mu_D = Normal("mu_D",
                      tau=0.001,
                      shape=(1),
                      testval=np.random.rand() * 0.1)
        sigma_D = HalfNormal("sigma_D",
                             tau=0.1,
                             shape=(1),
                             testval=np.random.rand() * 0.1)

        mu_Cond1 = Normal("mu_Cond1",
                          mu=0,
                          tau=0.001,
                          shape=(1),
                          testval=np.random.rand() * 0.1)
        sigma_Cond1 = HalfNormal("sigma_Cond1",
                                 tau=0.1,
                                 shape=(1),
                                 testval=np.random.rand() * 0.1)

        #############################
        # Hyperpriors - Subject level
        #############################
        dbase_tilde = Normal(
            "dbase_tilde",
            mu=0,
            sigma=1,
            shape=(nSubj, 1, 1),
        )
        dbase = Deterministic("dbase", mu_D + sigma_D * dbase_tilde)

        Bd_Cond1_tilde = Normal(
            "Bd_Cond1_tilde",
            mu=0,
            sigma=1,
            shape=(nSubj, 1, 1),
        )

        Bd_Cond1 = Deterministic(
            "Bd_Cond1",
            mu_Cond1 + sigma_Cond1 * Bd_Cond1_tilde,
        )

        lambda_logMratio = Gamma(
            "lambda_logMratio",
            alpha=0.001,
            beta=0.001,
            shape=(nSubj, 1, 1),
        )
        sigma_logMratio = Deterministic("sigma_logMratio",
                                        1 / math.sqrt(lambda_logMratio))

        ###############################
        # Hypterprior - Condition level
        ###############################
        mu_regression = [dbase + (Bd_Cond1 * c) for c in range(nCond)]

        log_mRatio_tilde = Normal("log_mRatio_tilde",
                                  mu=0,
                                  sigma=1,
                                  shape=(nSubj, 1, 1))
        log_mRatio = Deterministic(
            "log_mRatio",
            tt.stack(mu_regression, axis=1)[:, :, :, 0] +
            tt.tile(log_mRatio_tilde,
                    (1, 2, 1)) * tt.tile(sigma_logMratio, (1, 2, 1)),
        )

        mRatio = Deterministic("mRatio", tt.exp(log_mRatio))

        # Means of SDT distributions
        metad = Deterministic("metad", mRatio * d1)
        S2mu = Deterministic("S2mu", metad / 2)
        S1mu = Deterministic("S1mu", -metad / 2)

        # TYPE 2 SDT MODEL (META-D)
        # Multinomial likelihood for response counts
        # Specify ordered prior on criteria
        # bounded above and below by Type 1 c
        cS1_hn = Normal(
            "cS1_hn",
            mu=0,
            sigma=1,
            shape=(nSubj, nCond, nRatings - 1),
            testval=np.linspace(-1.5, -0.5, nRatings - 1).reshape(
                1, 1, nRatings - 1).repeat(nSubj, axis=0).repeat(nCond,
                                                                 axis=1),
        )
        cS1 = Deterministic("cS1", -mu_c2 + (cS1_hn * sigma_c2))

        cS2_hn = Normal(
            "cS2_hn",
            mu=0,
            sigma=1,
            shape=(nSubj, nCond, nRatings - 1),
            testval=np.linspace(0.5, 1.5, nRatings - 1).reshape(
                1, 1, nRatings - 1).repeat(nSubj, axis=0).repeat(nCond,
                                                                 axis=1),
        )
        cS2 = Deterministic("cS2", mu_c2 + (cS2_hn * sigma_c2))

        # Calculate normalisation constants
        C_area_rS1 = cumulative_normal(c1 - S1mu)
        I_area_rS1 = cumulative_normal(c1 - S2mu)
        C_area_rS2 = 1 - cumulative_normal(c1 - S2mu)
        I_area_rS2 = 1 - cumulative_normal(c1 - S1mu)

        # Get nC_rS1 probs
        nC_rS1 = cumulative_normal(cS1 - S1mu) / C_area_rS1
        nC_rS1 = Deterministic(
            "nC_rS1",
            math.concatenate(
                ([
                    cumulative_normal(cS1[:, :, 0].reshape((nSubj, 2, 1)) -
                                      S1mu) / C_area_rS1,
                    nC_rS1[:, :, 1:] - nC_rS1[:, :, :-1],
                    ((cumulative_normal(c1 - S1mu) -
                      cumulative_normal(cS1[:, :, (nRatings - 2)].reshape(
                          (nSubj, 2, 1)) - S1mu)) / C_area_rS1),
                ]),
                axis=2,
            ),
        )

        # Get nI_rS2 probs
        nI_rS2 = (1 - cumulative_normal(cS2 - S1mu)) / I_area_rS2
        nI_rS2 = Deterministic(
            "nI_rS2",
            math.concatenate(
                ([
                    ((1 - cumulative_normal(c1 - S1mu)) -
                     (1 - cumulative_normal(cS2[:, :, 0].reshape(
                         (nSubj, nCond, 1)) - S1mu))) / I_area_rS2,
                    nI_rS2[:, :, :-1] -
                    (1 - cumulative_normal(cS2[:, :, 1:] - S1mu)) / I_area_rS2,
                    (1 - cumulative_normal(cS2[:, :, nRatings - 2].reshape(
                        (nSubj, nCond, 1)) - S1mu)) / I_area_rS2,
                ]),
                axis=2,
            ),
        )

        # Get nI_rS1 probs
        nI_rS1 = (-cumulative_normal(cS1 - S2mu)) / I_area_rS1
        nI_rS1 = Deterministic(
            "nI_rS1",
            math.concatenate(
                ([
                    cumulative_normal(cS1[:, :, 0].reshape((nSubj, nCond, 1)) -
                                      S2mu) / I_area_rS1,
                    nI_rS1[:, :, :-1] +
                    (cumulative_normal(cS1[:, :, 1:] - S2mu)) / I_area_rS1,
                    (cumulative_normal(c1 - S2mu) -
                     cumulative_normal(cS1[:, :, nRatings - 2].reshape(
                         (nSubj, nCond, 1)) - S2mu)) / I_area_rS1,
                ]),
                axis=2,
            ),
        )

        # Get nC_rS2 probs
        nC_rS2 = (1 - cumulative_normal(cS2 - S2mu)) / C_area_rS2
        nC_rS2 = Deterministic(
            "nC_rS2",
            math.concatenate(
                ([
                    ((1 - cumulative_normal(c1 - S2mu)) -
                     (1 - cumulative_normal(cS2[:, :, 0].reshape(
                         (nSubj, nCond, 1)) - S2mu))) / C_area_rS2,
                    nC_rS2[:, :, :-1] -
                    ((1 - cumulative_normal(cS2[:, :, 1:] - S2mu)) /
                     C_area_rS2),
                    (1 - cumulative_normal(cS2[:, :, nRatings - 2].reshape(
                        (nSubj, nCond, 1)) - S2mu)) / C_area_rS2,
                ]),
                axis=2,
            ),
        )

        # Avoid underflow of probabilities
        nC_rS1 = math.switch(nC_rS1 < Tol, Tol, nC_rS1)
        nI_rS2 = math.switch(nI_rS2 < Tol, Tol, nI_rS2)
        nI_rS1 = math.switch(nI_rS1 < Tol, Tol, nI_rS1)
        nC_rS2 = math.switch(nC_rS2 < Tol, Tol, nC_rS2)

        for c in range(nCond):
            Multinomial(
                f"CR_counts_{c}",
                n=cr[:, c],
                p=nC_rS1[:, c, :],
                observed=counts[:, c, :nRatings],
                shape=(nSubj, nRatings),
            )
            Multinomial(
                f"H_counts_{c}",
                n=hits[:, c],
                p=nC_rS2[:, c, :],
                observed=counts[:, c, nRatings * 3:nRatings * 4],
                shape=(nSubj, nRatings),
            )
            Multinomial(
                f"FA_counts_{c}",
                n=falsealarms[:, c],
                p=nI_rS2[:, c, :],
                observed=counts[:, c, nRatings:nRatings * 2],
                shape=(nSubj, nRatings),
            )
            Multinomial(
                f"M_counts_{c}",
                n=m[:, c],
                p=nI_rS1[:, c, :],
                observed=counts[:, c, nRatings * 2:nRatings * 3],
                shape=(nSubj, nRatings),
            )

        if sample_model is True:

            trace = sample(return_inferencedata=True, **kwargs)

            return model, trace

        else:
            return model
예제 #13
0
#Get the temperatures
T = data['T']
T = T.as_matrix()
T = T.flatten()

size = len(Y)
#TRANSFORM FROM DATAFRAME TO NP ARRAY FORMAT

# Specifing the parameters that control the MCMC (these will be used throughout the code).

hypers = hyperparam['EFD']

basic_model_EFD = Model()
print(hypers)

bruh = Gamma('bruh', 2.475, 0.0158)

with basic_model_EFD:
    #priors for unknown model parameters

    c = Gamma('c', 2.475, 0.0158)
    Tm = Gamma('Tm', 1.82580286, 4.67203779)
    T0 = Gamma('T0', 180.908498, 0.169325317)
    tau = Gamma('tau', 32.86091663, 0.56949197)

    #c = Gamma('c',1,10)
    #Tm= Uniform('Tm',30,45)
    #T0= Uniform('T0',0,20)
    #tau= Gamma('tau',0.0001, 0.0001)

    mu_temp = c * T * ((T - T0) * (T0 <= T)) * np.sqrt((Tm - T) * (Tm >= T))
from pymc3 import Gamma

jul_rain = nash_precip.Jul
jan_rain = nash_precip.Jan

with Model() as rainfall_model:

    σ_jan = Uniform('σ_jan', 0, 1000)
    σ_jul = Uniform('σ_jul', 0, 1000)

    mu_jan = Uniform('mu_jan', 0, 25)
    mu_jul = Uniform('mu_jul', 0, 25)

    jan = Gamma('jan', mu=mu_jan, sd=σ_jan, observed=jan_rain)
    jul = Gamma('jul', mu=mu_jul, sd=σ_jul, observed=jul_rain)

    d = Deterministic('d', mu_jan - mu_jul)

    samples = fit(20000).sample(1000)
예제 #15
0
#Get the temperatures
T = data['T']
T = T.as_matrix()
T = T.flatten()

size = len(Y)
#TRANSFORM FROM DATAFRAME TO NP ARRAY FORMAT

# Specifing the parameters that control the MCMC (these will be used throughout the code).

basic_model_GCR = Model()

with basic_model_GCR:
    #priors for unknown model parameters
    c = Gamma('c', 1, 10)
    Tm = Uniform('Tm', 25, 45)
    T0 = Uniform('T0', 0, 24)
    tau = Gamma('tau', 0.0001, 0.0001)

    mu_temp = c * T * ((T - T0) * (T0 < T)) * np.sqrt((Tm - T) * (Tm > T))
    mu = 0 * (mu_temp < 0) + mu_temp * (mu_temp > 0)

    Y_obs = Normal('Y_obs', mu=mu, sd=tau, observed=Y)

from pymc3 import Metropolis, sample, find_MAP
from scipy import optimize

with basic_model_GCR:

    # obtain starting values via MAP
예제 #16
0
            last_time = sim.show_time()
        except simpy.core.EmptySchedule:  #if you run out of actions, break
            last_time = 10.0  #some high value time so it is clear that this is not the right way to end
            break
        if not counting.goal:  #if goal cleared (as should happen when you finish the task correctly and reach stop, break)
            break

    return np.repeat(np.array(1000 * last_time), size)  # we return time in ms


basic_model = Model()

with basic_model:

    # Priors for unknown model parameters
    lf = Gamma('lf', alpha=2, beta=4)

    sigma = Uniform('sigma', lower=0.1, upper=50)

    #you can print searched values from every draw
    #lf_print = T.printing.Print('lf')(lf)

    #Deterministic value (RT in ms) established by the ACT-R model
    mu = model(lf)

    # Likelihood (sampling distribution) of observations
    Normal('Y_obs', mu=mu, sd=sigma, observed=Y)

    #Metropolis algorithm for steps in simulation
    step = Metropolis(basic_model.vars)
예제 #17
0
N_GROUPS = comm.Get_size(
) - 1  #Groups used for simulation - one less than used cores

if rank == 0:  #master
    print("CHAIN ", CHAIN)
    print("DRAWS", NDRAWS)

    print("How many sentences used?", n_sentences)
    basic_model = Model()

    #Below we specify the Bayesian model
    with basic_model:

        #Priors
        le = HalfNormal('le', sd=0.5, testval=abs(np.random.randn()) / 2)
        lf = Gamma('lf', alpha=1, beta=5, testval=abs(np.random.randn() / 4))
        #emvt = Gamma('emvt', alpha=1, beta=3, testval=abs(np.random.randn()))
        emap = HalfNormal('emap', sd=1.0, testval=abs(np.random.randn() / 2))

        intercept = Normal('intercept', mu=50, sd=25)

        sigma = HalfNormal('sigma', sd=20)

        # Expected value of outcome
        mu = intercept + model(lf, le, emap, intercept, sigma)

        # Likelihood (sampling distribution) of observations
        Normal('Y_obs', mu=mu, sd=50, observed=Y)

        step = Metropolis(basic_model.vars)
T=T.as_matrix()
T= T.flatten()


size= len(Y)
#TRANSFORM FROM DATAFRAME TO NP ARRAY FORMAT


# Specifing the parameters that control the MCMC (these will be used throughout the code). 

basic_model_GCR = Model()


with basic_model_GCR: 
	#priors for unknown model parameters
	c = Gamma('c',1,10)
	Tm= Uniform('Tm',25,45)
	T0= Uniform('T0',0,24)
	tau= Gamma('tau',0.0001, 0.0001)

	mu_temp= c*T*((T-T0)*(T0<T))*np.sqrt((Tm-T)*(Tm>T))
	mu= 0*(mu_temp<0) + mu_temp*(mu_temp>0)

	Y_obs = Normal('Y_obs',mu=mu, sd=tau, observed= Y)


from pymc3 import Metropolis, sample, find_MAP
from scipy import optimize

with basic_model_GCR:  
    else:
        testval_lf = past_simulations['lf'].iloc[-1]
        testval_rf = past_simulations['rf'].iloc[-1]
        testval_le = past_simulations['le'].iloc[-1]
        testval_weight = past_simulations['weight'].iloc[-1]
        testval_std = past_simulations['std'].iloc[-1]

    # the model starts here
    parser_with_bayes = pm.Model()

    with parser_with_bayes:
        # prior for activation
        #decay = Uniform('decay', lower=0, upper=1) #currently, ignored because it leads to problems in sampling
        # priors for latency
        std = Uniform('std', lower=1,upper=50, testval=testval_std)
        lf = Gamma('lf', alpha=2, beta=20, testval=testval_lf) # to get 
        le = Gamma('le', alpha=2,beta=4, testval=testval_le)
        rf = Gamma('rf', alpha=2,beta=30, testval=testval_rf)
        weight = Uniform('weight', lower=1,upper=100, testval=testval_weight)
        # latency likelihood -- this is where pyactr is used
        pyactr_rt = actrmodel_latency(lf, le, rf, weight)
        subj_mu_rt = Deterministic('subj_mu_rt', pyactr_rt[0])
        subj_rt_observed = Normal('subj_rt_observed', mu=subj_mu_rt, sd=std, observed=subj_extraction['rt'])
        obj_mu_rt = Deterministic('obj_mu_rt', pyactr_rt[1])
        obj_rt_observed = Normal('obj_rt_observed', mu=obj_mu_rt, sd=std, observed=obj_extraction['rt'])
        step = Metropolis()
        db = Text('gg_6words_final_chain' + str(CHAIN))
        trace = sample(draws=NDRAWS, trace=db, chains=1, step=step, init='auto', tune=1)

    posterior_checks_subj = pd.DataFrame.from_records(posterior_checks['subj_rt_observed'])
    posterior_checks_obj = pd.DataFrame.from_records(posterior_checks['obj_rt_observed'])
예제 #20
0
    def test_normal_mixture_nd(self, nd, ncomp):
        nd = to_tuple(nd)
        ncomp = int(ncomp)
        comp_shape = nd + (ncomp, )
        test_mus = np.random.randn(*comp_shape)
        test_taus = np.random.gamma(1, 1, size=comp_shape)
        observed = generate_normal_mixture_data(w=np.ones(ncomp) / ncomp,
                                                mu=test_mus,
                                                sd=1 / np.sqrt(test_taus),
                                                size=10)

        with Model() as model0:
            mus = Normal("mus", shape=comp_shape)
            taus = Gamma("taus", alpha=1, beta=1, shape=comp_shape)
            ws = Dirichlet("ws", np.ones(ncomp), shape=(ncomp, ))
            mixture0 = NormalMixture("m",
                                     w=ws,
                                     mu=mus,
                                     tau=taus,
                                     shape=nd,
                                     comp_shape=comp_shape)
            obs0 = NormalMixture("obs",
                                 w=ws,
                                 mu=mus,
                                 tau=taus,
                                 shape=nd,
                                 comp_shape=comp_shape,
                                 observed=observed)

        with Model() as model1:
            mus = Normal("mus", shape=comp_shape)
            taus = Gamma("taus", alpha=1, beta=1, shape=comp_shape)
            ws = Dirichlet("ws", np.ones(ncomp), shape=(ncomp, ))
            comp_dist = [
                Normal.dist(mu=mus[..., i], tau=taus[..., i], shape=nd)
                for i in range(ncomp)
            ]
            mixture1 = Mixture("m", w=ws, comp_dists=comp_dist, shape=nd)
            obs1 = Mixture("obs",
                           w=ws,
                           comp_dists=comp_dist,
                           shape=nd,
                           observed=observed)

        with Model() as model2:
            # Expected to fail if comp_shape is not provided,
            # nd is multidim and it does not broadcast with ncomp. If by chance
            # it does broadcast, an error is raised if the mixture is given
            # observed data.
            # Furthermore, the Mixture will also raise errors when the observed
            # data is multidimensional but it does not broadcast well with
            # comp_dists.
            mus = Normal("mus", shape=comp_shape)
            taus = Gamma("taus", alpha=1, beta=1, shape=comp_shape)
            ws = Dirichlet("ws", np.ones(ncomp), shape=(ncomp, ))
            if len(nd) > 1:
                if nd[-1] != ncomp:
                    with pytest.raises(ValueError):
                        NormalMixture("m", w=ws, mu=mus, tau=taus, shape=nd)
                    mixture2 = None
                else:
                    mixture2 = NormalMixture("m",
                                             w=ws,
                                             mu=mus,
                                             tau=taus,
                                             shape=nd)
            else:
                mixture2 = NormalMixture("m", w=ws, mu=mus, tau=taus, shape=nd)
            observed_fails = False
            if len(nd) >= 1 and nd != (1, ):
                try:
                    np.broadcast(np.empty(comp_shape), observed)
                except Exception:
                    observed_fails = True
            if observed_fails:
                with pytest.raises(ValueError):
                    NormalMixture("obs",
                                  w=ws,
                                  mu=mus,
                                  tau=taus,
                                  shape=nd,
                                  observed=observed)
                obs2 = None
            else:
                obs2 = NormalMixture("obs",
                                     w=ws,
                                     mu=mus,
                                     tau=taus,
                                     shape=nd,
                                     observed=observed)

        testpoint = model0.test_point
        testpoint["mus"] = test_mus
        testpoint["taus"] = test_taus
        assert_allclose(model0.logp(testpoint), model1.logp(testpoint))
        assert_allclose(mixture0.logp(testpoint), mixture1.logp(testpoint))
        assert_allclose(obs0.logp(testpoint), obs1.logp(testpoint))
        if mixture2 is not None and obs2 is not None:
            assert_allclose(model0.logp(testpoint), model2.logp(testpoint))
        if mixture2 is not None:
            assert_allclose(mixture0.logp(testpoint), mixture2.logp(testpoint))
        if obs2 is not None:
            assert_allclose(obs0.logp(testpoint), obs2.logp(testpoint))
T=T.as_matrix()
T= T.flatten()


size= len(Y)
#TRANSFORM FROM DATAFRAME TO NP ARRAY FORMAT


# Specifing the parameters that control the MCMC (these will be used throughout the code). 

basic_model = Model()


with basic_model: 
	#priors for unknown model parameters
	c = Gamma('c',10,1)
	Tm= Uniform('Tm',31,45)
	T0= Uniform('T0',5,24)
	tau= Gamma('tau',0.0001, 0.0001)

	mu_temp= c*T*((T-T0)*(T0<T))*np.sqrt((Tm-T)*(Tm>T))
	mu= 0*(mu_temp<0) + mu_temp*(mu_temp>0)

	Y_obs = Normal('Y_obs',mu=mu, sd=tau, observed= Y)


from pymc3 import Metropolis, sample, find_MAP
from scipy import optimize
trace_copy= {}
with basic_model:  
예제 #22
0
pathways, features, path_dict, reverse_path_dict, evidence, metfrag_evidence = data
for c, v in evidence.items():
    print(c, v)
for c, v in metfrag_evidence.items():
    print(c, v)
print("num_pathways:", len(pathways))
print("num_features:", len(features))
print("num_evidence:", len(evidence))
print("num_metfrag: ", len(metfrag_evidence))
rate_prior = 0.5
import theano.tensor as T
#eps = Beta('eps', 0.005, 1)
model = Model()
with model:
    eps = 0.0001
    ap = {p: Gamma('p_' + p, rate_prior, 1) for p in pathways}
    bmp = {
        p: {
            feat: Gamma('b_{' + p + ',' + feat + '}', ap[p], 1)
            for feat in path_dict[p]
        }
        for p in pathways
    }
    y_bmp = {}
    g = {}

    def logp_f(f, b, eps):
        if f in evidence:
            return T.log(1 - math.e**(-1 * b) + epsilon)
        if f in metfrag_evidence:
            a_p = (1.0 / (1 - metfrag_evidence[f])) - 1
        testval_le = past_simulations['le'].iloc[-1]
        testval_threshold = past_simulations['threshold'].iloc[-1]
        testval_emma_prep_time = past_simulations['emma_prep_time'].iloc[-1]
        testval_prob_regression = past_simulations['prob_regression'].iloc[-1]
        testval_std = past_simulations['std'].iloc[-1]

    # here the model starts

    parser_with_bayes = pm.Model()

    with parser_with_bayes:
        # prior for activation
        #decay = Uniform('decay', lower=0, upper=1) #currently, ignored because it leads to problems in sampling
        # priors for latency
        std = Uniform('std', lower=1, upper=60, testval=testval_std)
        lf = Gamma('lf', alpha=2, beta=20, testval=testval_lf)
        le = Gamma('le', alpha=2, beta=4, testval=testval_le)
        threshold = Normal('threshold', mu=0, sd=10, testval=testval_threshold)
        emma_prep_time = Gamma('emma_prep_time',
                               alpha=4,
                               beta=30,
                               testval=testval_emma_prep_time)
        prob_regression = Uniform('prob_regression',
                                  lower=0.01,
                                  upper=0.5,
                                  testval=testval_prob_regression)
        # latency likelihood -- this is where pyactr is used
        pyactr_rt = actrmodel_latency(lf, le, emma_prep_time, prob_regression,
                                      threshold)
        #RTs
        mu_rt = Deterministic('mu_rt', pyactr_rt[0])