Exemplo n.º 1
0
plt.style.use('seaborn-darkgrid')

np.random.seed(314)
N = 100
alfa_real = 2.5
beta_real = 0.9
eps_real = np.random.normal(0, 0.5, size= N)

x = np.random.normal(10, 1, N)
y_real = alfa_real + beta_real * x
y = y_real + eps_real

data = np.stack((x, y)).T
#print(data)

with pm.Model() as pearson_model:
	mu = pm.Normal('mu', mu=data.mean(0), sd = 10, shape = 2)
	sigma_1 = pm.HalfNormal('sigma_1', 10)
	sigma_2 = pm.HalfNormal('sigma_2', 10)
	rho = pm.Uniform('rho', -1, 1)
	cov = pm.math.stack(([sigma_1**2, sigma_1 * sigma_2 * rho], [sigma_1 * sigma_2 * rho, sigma_2**2]))
	y_pred = pm.MvNormal('y_pres', mu=mu, cov = cov, observed=data)
	
	start = pm.find_MAP()
	step = pm.NUTS(scaling= start)
	trace_p = pm.sample(1000, step = step, stsrt = start, njobs=1)

pm.traceplot(trace_p)
pm.summary(trace_p)
plt.savefig('img413.png')
Exemplo n.º 2
0
model = pm.Model()
n_data = social_distance.shape[0]
t = np.arange(n_data)

with model:
    # Weakly informative priors on the parameters
    gamma = pm.Normal('gamma', mu=0, sigma=10, shape=(4, 3))
    mu = pm.Normal('mu', mu=0, sigma=10, shape=3)
    sigma = pm.MatrixNormal('sigma',
                            mu=np.eye(3),
                            rowcov=np.eye(3) * 10,
                            colcov=np.eye(3) * 10,
                            shape=(3, 3))

    # Define relationships amongst the params
    eta = pm.MvNormal('eta', mu=np.zeros(3), cov=sigma, shape=3)

    mat = social_distance @ gamma

    beta_0 = mu[0] + mat[:, 0] + eta[0]
    beta_1 = mu[1] + mat[:, 1] + eta[1]
    beta_2 = mu[2] + mat[:, 2] + eta[2]

    lamb = np.exp(beta_0 + beta_1 * t + beta_2 * (t**2))

    Y_obs = pm.NegativeBinomial('Y_obs', mu=lamb, alpha=1, observed=death_data)

    for RV in model.basic_RVs:
        print(RV.name, RV.logp(model.test_point))

    trace = pm.sample()
Exemplo n.º 3
0
    def __init__(self, name, votes, polls, party_groups, cholesky_matrix,
                 test_results, house_effects_model, min_polls_per_pollster,
                 adjacent_day_fn):
        super(ElectionDynamicsModel, self).__init__(name)

        self.votes = votes
        self.polls = polls
        self.party_groups = party_groups

        self.num_parties = polls.num_parties
        self.num_days = polls.num_days
        self.num_pollsters = polls.num_pollsters
        self.max_poll_days = polls.max_poll_days
        self.num_party_groups = max(self.party_groups) + 1
        self.cholesky_matrix = cholesky_matrix
        if type(adjacent_day_fn) in [int, float]:
            self.adjacent_day_fn = lambda diff: (1. + diff)**adjacent_day_fn
        else:
            self.adjacent_day_fn = adjacent_day_fn

        self.test_results = (polls.get_last_days_average(10)
                             if test_results is None else test_results)

        # The base polls model. House-effects models
        # are optionally set up based on this model.

        # The innovations are multivariate normal with the same
        # covariance/cholesky matrix as the polls' MvStudentT
        # variable. The assumption is that the parties' covariance
        # is invariant throughout the election campaign and
        # influences polls, evolving support and election day
        # vote.
        self.innovations = pm.MvNormal(
            'innovations',
            mu=np.zeros([self.num_days, self.num_parties]),
            chol=self.cholesky_matrix,
            shape=[self.num_days, self.num_parties],
            testval=np.zeros([self.num_days, self.num_parties]))

        # The random walk itself is a cumulative sum of the innovations.
        self.walk = pm.Deterministic('walk', self.innovations.cumsum(axis=0))

        # The modeled support of the various parties over time is the sum
        # of both the election-day votes and the innovations that led up to it.
        # The support at day 0 is the election day vote.
        self.support = pm.Deterministic('support', self.votes + self.walk)

        # In some cases, we might want to filter pollsters without a minimum
        # number of polls. Because these pollsters produced only a few polls,
        # we cannot determine whether their results are biased or not.
        polls_per_pollster = {
            pollster_id:
            sum(1 for p in self.polls if p.pollster_id == pollster_id)
            for pollster_id in range(self.num_pollsters)
        }

        self.min_polls_per_pollster = min_polls_per_pollster

        self.num_pollsters_in_model = 0
        self.pollster_mapping = {}

        for pollster_id, count in polls_per_pollster.items():
            if count >= self.min_polls_per_pollster:
                self.pollster_mapping[
                    pollster_id] = self.num_pollsters_in_model
                self.num_pollsters_in_model += 1
            else:
                self.pollster_mapping[pollster_id] = None

        self.filtered_polls = [
            p for p in self.polls
            if polls_per_pollster[p.pollster_id] >= self.min_polls_per_pollster
        ]

        if self.min_polls_per_pollster > 1:
            print(
                "Some polls were filtered out. Provided polls: %d, filtered: %d, final total: %d"
                % (len(self.polls), len(self.polls) - len(self.filtered_polls),
                   len(self.filtered_polls)))

        # Group polls by number of days. This is necessary to allow generating
        # a different cholesky matrix for each. This corresponds to the
        # average of the modeled support used for multi-day polls.
        group_polls = lambda poll: poll.num_poll_days

        # Group the polls and create the likelihood variable.
        self.grouped_polls = [
            (num_poll_days, [p for p in polls])
            for num_poll_days, polls in itertools.groupby(
                sorted(self.filtered_polls, key=group_polls), group_polls)
        ]

        # To handle multiple-day polls, we average the party support for the
        # relevant days
        def expected_poll_outcome(p):
            if p.num_poll_days > 1:
                poll_days = [d for d in range(p.end_day, p.start_day + 1)]
                return self.walk[poll_days].mean(axis=0)
            else:
                return self.walk[p.start_day]

        def expected_polls_outcome(polls):
            if self.adjacent_day_fn is None:
                return [expected_poll_outcome(p) for p in polls] + self.votes
            else:
                weights = np.asarray([[
                    sum(
                        self.adjacent_day_fn(abs(d - poll_day))
                        for poll_day in range(p.end_day, p.start_day + 1))
                    for d in range(self.num_days)
                ] for p in polls])
                return tt.dot(weights / weights.sum(axis=1, keepdims=True),
                              self.walk + self.votes)

        self.mus = {
            num_poll_days: expected_polls_outcome(polls)
            for num_poll_days, polls in self.grouped_polls
        }

        self.create_house_effects(house_effects_model)

        self.likelihoods = [
            # The Multivariate Student-T variable that models the polls.
            #
            # The polls are modeled as a MvStudentT distribution which allows to
            # take into consideration the number of people polled as well as the
            # cholesky-covariance matrix that is central to the model.

            # Because we average the support over the number of poll days n, we
            # also need to appropriately factor the cholesky matrix. We assume
            # no correlation between different days, so the factor is 1/n for
            # the variance, and 1/sqrt(n) for the cholesky matrix.
            pm.MvStudentT('polls_%d_days' % num_poll_days,
                          nu=[p.num_polled - 1 for p in polls],
                          mu=self.mus[num_poll_days],
                          chol=self.cholesky_matrix / np.sqrt(num_poll_days),
                          testval=test_results,
                          shape=[len(polls), self.num_parties],
                          observed=[p.percentages for p in polls])
            for num_poll_days, polls in self.grouped_polls
        ]
Exemplo n.º 4
0
def run_model(index, in_dir, out_dir, data_filename, func_filename,
              struct_filename, dist_filename, kernel, n, sample_size,
              tune_size):
    """
    index: data
    in_dir: set up work directory
    out_dir: save the trace as csv in the out directory
    data_filename: filename for time series data
    func_filename: filename for functional connectivity
    struct_filename: filename for structural connectivity
    dist_filename: filename for distribution matrix of n ROIs 
    kernel: "exponential" or "gaussian" or "matern52" or "matern32"
    n: ROI number
    sample_size: NUTS number
    tune_size: burning number
    """
    os.chdir(in_dir + str(index))
    Y = get_data(data_filename)
    mFunc = get_func(func_filename, n)
    Struct = get_struct(struct_filename, n)
    Dist = get_dist(dist_filename, n)
    m = Dist[0].shape[0]
    k = Y.shape[1]
    n_vec = n * (n + 1) // 2
    Y_mean = []
    for i in range(n):
        Y_mean.append(np.mean(Y[i * m:(i + 1) * m, 0]))
    Y_mean = np.array(Y_mean)

    with pm.Model() as model_generator:

        # convariance matrix
        log_Sig = pm.Uniform("log_Sig", -8, 8, shape=(n, ))
        SQ = tt.diag(tt.sqrt(tt.exp(log_Sig)))
        Func_Covm = tt.dot(tt.dot(SQ, mFunc), SQ)
        Struct_Convm = tt.dot(tt.dot(SQ, Struct), SQ)

        # double fusion of structural and FC
        L_fc_vec = tt.reshape(
            tt.slinalg.cholesky(tt.squeeze(Func_Covm)).T[np.triu_indices(n)],
            (n_vec, ))
        L_st_vec = tt.reshape(
            tt.slinalg.cholesky(
                tt.squeeze(Struct_Convm)).T[np.triu_indices(n)], (n_vec, ))
        Struct_vec = tt.reshape(Struct[np.triu_indices(n)], (n_vec, ))
        lambdaw = pm.Beta("lambdaw", alpha=1, beta=1, shape=(n_vec, ))
        Kf = pm.Beta("Kf", alpha=1, beta=1, shape=(n_vec, ))
        rhonn = Kf*( (1-lambdaw)*L_fc_vec + lambdaw*L_st_vec ) + \
            (1-Kf)*( (1-Struct_vec*lambdaw)*L_fc_vec + Struct_vec*lambdaw*L_st_vec )

        # correlation
        Cov_temp = tt.triu(tt.ones((n, n)))
        Cov_temp = tt.set_subtensor(Cov_temp[np.triu_indices(n)], rhonn)
        Cov_mat_v = tt.dot(Cov_temp.T, Cov_temp)
        d = tt.sqrt(tt.diagonal(Cov_mat_v))
        rho = (Cov_mat_v.T / d).T / d
        rhoNew = pm.Deterministic("rhoNew", rho[np.triu_indices(n, 1)])

        # temporal correlation AR(1)
        phi_T = pm.Uniform("phi_T", 0, 1, shape=(n, ))
        sigW_T = pm.Uniform("sigW_T", 0, 100, shape=(n, ))
        B = pm.Normal("B", 0, 100, shape=(n, ))
        muW1 = Y_mean - B  # get the shifted mean
        mean_overall = muW1 / (1.0 - phi_T)  # AR(1) mean
        tau_overall = (1.0 - tt.sqr(phi_T)) / tt.sqr(sigW_T)  # AR (1) variance
        W_T = pm.MvNormal("W_T",
                          mu=mean_overall,
                          tau=tt.diag(tau_overall),
                          shape=(k, n))

        # add all parts together
        one_m_vec = tt.ones((m, 1))
        one_k_vec = tt.ones((1, k))
        D = pm.MvNormal("D", mu=tt.zeros(n), cov=Cov_mat_v, shape=(n, ))
        phi_s = pm.Uniform("phi_s", 0, 20, shape=(n, ))
        spat_prec = pm.Uniform("spat_prec", 0, 100, shape=(n, ))
        H_base = pm.Normal("H_base", 0, 1, shape=(m, n))

        Mu_all = tt.zeros((m * n, k))
        if kernel == "exponential":
            for i in range(n):
                r = Dist[i] * phi_s[i]
                H_temp = tt.sqr(spat_prec[i]) * tt.exp(-r)
                L_H_temp = tt.slinalg.cholesky(H_temp)
                Mu_all_update = tt.set_subtensor(Mu_all[m*i:m*(i+1), :], B[i] + D[i] + one_m_vec*W_T[:,i] + \
                    tt.dot(L_H_temp, tt.reshape(H_base[:,i], (m, 1)))*one_k_vec)
                Mu_all = Mu_all_update
        elif kernel == "gaussian":
            for i in range(n):
                r = Dist[i] * phi_s[i]
                H_temp = tt.sqr(spat_prec[i]) * tt.exp(-tt.sqr(r) * 0.5)
                L_H_temp = tt.slinalg.cholesky(H_temp)
                Mu_all_update = tt.set_subtensor(Mu_all[m*i:m*(i+1), :], B[i] + D[i] + one_m_vec*W_T[:,i] + \
                    tt.dot(L_H_temp, tt.reshape(H_base[:,i], (m, 1)))*one_k_vec)
                Mu_all = Mu_all_update
        elif kernel == "matern52":
            for i in range(n):
                r = Dist[i] * phi_s[i]
                H_temp = tt.sqr(spat_prec[i]) * (
                    (1.0 + tt.sqrt(5.0) * r + 5.0 / 3.0 * tt.sqr(r)) *
                    tt.exp(-1.0 * tt.sqrt(5.0) * r))
                L_H_temp = tt.slinalg.cholesky(H_temp)
                Mu_all_update = tt.set_subtensor(Mu_all[m*i:m*(i+1), :], B[i] + D[i] + one_m_vec*W_T[:,i] + \
                    tt.dot(L_H_temp, tt.reshape(H_base[:,i], (m, 1)))*one_k_vec)
                Mu_all = Mu_all_update
        elif kernel == "matern32":
            for i in range(n):
                r = Dist[i] * phi_s[i]
                H_temp = tt.sqr(spat_prec[i]) * (
                    1.0 + tt.sqrt(3.0) * r) * tt.exp(-tt.sqrt(3.0) * r)
                L_H_temp = tt.slinalg.cholesky(H_temp)
                Mu_all_update = tt.set_subtensor(Mu_all[m*i:m*(i+1), :], B[i] + D[i] + one_m_vec*W_T[:,i] + \
                    tt.dot(L_H_temp, tt.reshape(H_base[:,i], (m, 1)))*one_k_vec)
                Mu_all = Mu_all_update

        sigma_error_prec = pm.Uniform("sigma_error_prec", 0, 100)
        Y1 = pm.Normal("Y1", mu=Mu_all, sd=sigma_error_prec, observed=Y)

    with model_generator:
        step = pm.NUTS()
        trace = pm.sample(sample_size, step=step, tune=tune_size, chains=1)

    # save as pandas format and output the csv file
    save_trace = pm.trace_to_dataframe(trace)
    save_trace.to_csv(out_dir + date.today().strftime("%m_%d_%y") + \
        "_sample_size_" + str(sample_size) + "_index_" + str(index) + ".csv")
Exemplo n.º 5
0
plt.xlabel('$x$', fontsize=16)
plt.ylabel('$f(x)$', fontsize=16, rotation=0)
plt.savefig('img808.png', dpi=300, figsize=[5.5, 5.5])

plt.figure()

with pm.Model() as GP:
    mu = np.zeros(N)
    eta = pm.HalfCauchy('eta', 5)
    rho = pm.HalfCauchy('rho', 5)
    sigma = pm.HalfCauchy('sigma', 5)

    D = squared_distance(x, x)
    K = tt.fill_diagonal(eta * pm.math.exp(-rho * D), eta + sigma)

    obs = pm.MvNormal('obs', mu, cov=K, observed=y)
    test_points = np.linspace(0, 10, 100)
    D_pred = squared_distance(test_points, test_points)
    D_off_diag = squared_distance(x, test_points)

    K_oo = eta * pm.math.exp(-rho * D_pred)
    K_o = eta * pm.math.exp(-rho * D_off_diag)

    mu_post = pm.Deterministic(
        'mu_post',
        pm.math.dot(pm.math.dot(K_o, tt.nlinalg.matrix_inverse(K)), y))
    SIGMA_post = pm.Deterministic(
        'SIGMA_post', K_oo -
        pm.math.dot(pm.math.dot(K_o, tt.nlinalg.matrix_inverse(K)), K_o.T))

    trace = pm.sample(1000, njobs=1)
Exemplo n.º 6
0
def PPCs(stat, model):
    sx, sy, st, sdx, sdy, sdt = stat

    if model == "bm":
        bm = pm.Model()

        with bm:
            D = pm.Lognormal("D", 0, 1)
            pm.Normal(
                "like_x", mu=0, sd=tt.sqrt(2 * D * sdt), observed=sdx
            )
            pm.Normal(
                "like_y", mu=0, sd=tt.sqrt(2 * D * sdt), observed=sdy
            )

            trace_bm = pm.sample(2000, chains=2, cores=1, progressbar=False)

        ppc_bm = pm.sample_posterior_predictive(
            trace_bm, model=bm, progressbar=False
        )
        simulated_dx_bm = ppc_bm[bm.observed_RVs[0].name]
        simulated_dy_bm = ppc_bm[bm.observed_RVs[1].name]
        simulated_x_bm = np.insert(
            np.cumsum(simulated_dx_bm, axis=1), 0, 0, axis=1
        )
        simulated_y_bm = np.insert(
            np.cumsum(simulated_dy_bm, axis=1), 0, 0, axis=1
        )

        # ppc in autocorrX, lag=1
        pxstd = []
        for i in range(4000):
            pxstd.append(calAutoCorr(simulated_x_bm[i, :], 1)[-1])

        fig, axes = plt.subplots(1, 3, figsize=(16, 4))
        for i, j in zip(simulated_x_bm[::2, :], simulated_y_bm[::2, :]):
            axes[0].plot(i, j, alpha=0.2)
        axes[0].plot(sx, sy, c="k", label="True data")
        axes[1].hist(simulated_x_bm.std(axis=1), bins=30)
        axes[2].hist(pxstd, bins=30)
        axes[2].axvline(
            x=autoCorrFirstX(sx, sdt), ls="--", c="r", label="data autocorrX"
        )
        axes[1].axvline(x=sx.std(), ls="--", c="r", label="data std in x")
        axes[0].legend()
        axes[1].legend()
        axes[2].legend()
        axes[0].set_title("PP Samples from BM model and True track")
        axes[1].set_title("PP Samples std in x")
        axes[2].set_title("PP Samples autocorrX")

    if model == "me":

        model_stick = pm.Model()
        with model_stick:

            me = pm.Lognormal("me", 0, 1)
            _, sig = sticking_covariance(len(sx) - 1, 0, me)

            pm.MvNormal("likex", mu=0, cov=sig, observed=sx)
            pm.MvNormal("likey", mu=0, cov=sig, observed=sy)

        with model_stick:
            trace_stick = pm.sample(2000, chains=2, cores=1, progressbar=False)

        # manually generate ppc samples
        simulated_x_stick, simulated_y_stick = (
            np.zeros((4000, len(sx))),
            np.zeros((4000, len(sx))),
        )
        for i in range(4000):
            mu1, Sigma1 = sticking_covariance(
                len(sx) - 1, 0, trace_stick["me"][i]
            )
            x = np.random.multivariate_normal(mu1, Sigma1)
            mu2, Sigma2 = sticking_covariance(
                len(sx) - 1, 0, trace_stick["me"][i]
            )
            y = np.random.multivariate_normal(mu2, Sigma2)

            simulated_x_stick[i, :], simulated_y_stick[i, :] = x, y

        # ppc in autocorrX, lag=1
        pxstd = []
        for i in range(4000):
            pxstd.append(calAutoCorr(simulated_x_stick[i, :], 1)[-1])

        fig, axes = plt.subplots(1, 3, figsize=(16, 4))
        for i, j in zip(simulated_x_stick[::2, :], simulated_y_stick[::2, :]):
            axes[0].plot(i, j, alpha=0.2)
        axes[0].plot(sx, sy, c="k", label="True data")
        axes[1].hist(pxstd, bins=30)
        axes[2].hist(simulated_x_stick.std(axis=1), bins=30)
        axes[1].axvline(
            x=autoCorrFirstX(sx, sdt), ls="--", c="r", label="data autocorrX"
        )
        axes[2].axvline(x=sx.std(), ls="--", c="r", label="data std in x")
        axes[0].legend()
        axes[1].legend()
        axes[2].legend()
        axes[0].set_title("PP Samples from Stuck model and True track")
        axes[1].set_title("PP Samples autocorrX")
        axes[2].set_title("PP Samples std in x")
        plt.show()

    if model == "hpw":
        model_hpw = pm.Model()
        with model_hpw:
            D = pm.Lognormal("D", 0, 1)
            k = pm.Lognormal("k", 0, 1)

            mean_x = (-sx[:-1]) * (1 - tt.exp(-k * sdt))
            mean_y = (-sy[:-1]) * (1 - tt.exp(-k * sdt))
            std = tt.sqrt(D * (1 - tt.exp(-2 * k * sdt)) / k)

            pm.Normal("like_x", mu=mean_x, sd=std, observed=sdx)
            pm.Normal("like_y", mu=mean_y, sd=std, observed=sdy)

        with model_hpw:
            trace_hpw = pm.sample(
                2000, tune=2000, chains=2, cores=1, progressbar=False
            )

        simulated_x_hpw, simulated_y_hpw = (
            np.zeros((4000, len(sx))),
            np.zeros((4000, len(sx))),
        )
        for i in range(trace_hpw["D"].shape[0]):
            base = base_HPW_D(
                [0, 0],
                [i for i in range(len(sx))],
                trace_hpw["D"][i],
                [0, 0],
                trace_hpw["k"][i],
            )
            simulated_x_hpw[i, :], simulated_y_hpw[i, :] = base[0], base[1]

        pxstd = []
        for i in range(4000):
            pxstd.append(calAutoCorr(simulated_x_hpw[i, :], 1)[-1])

        fig, axes = plt.subplots(1, 3, figsize=(16, 4))
        for i, j in zip(simulated_x_hpw[::2, :], simulated_y_hpw[::2, :]):
            axes[0].plot(i, j, alpha=0.2)
        axes[0].plot(sx, sy, c="k", label="True data")
        axes[1].hist(pxstd, bins=30)
        axes[2].hist(simulated_x_hpw.std(axis=1), bins=30)
        axes[1].axvline(
            x=autoCorrFirstX(sx, sdt), ls="--", c="r", label="data autocorrX"
        )
        axes[2].axvline(x=sx.std(), ls="--", c="r", label="data std in x")
        axes[0].legend()
        axes[1].legend()
        axes[2].legend()
        axes[0].set_title("PP Samples from Stuck model and True track")
        axes[1].set_title("PP Samples autocorrX")
        axes[2].set_title("PP Samples std in x")
        plt.show()
def flat_bnn(annInput, errAnnInput, annTarget, errAnnTarget):

    annInput = theano.shared(annInput)
    errAnnInput = theano.shared(errAnnInput)
    annTarget = theano.shared(annTarget)
    errAnnTarget = theano.shared(errAnnTarget)

    n_samples = samples['w_in_1_grp'].shape[0]

    prior_1_mu = samples['w_in_1_grp'].mean(axis=0)
    bias_1_mu = samples['b_in_1_grp'].mean(axis=0)
    prior_1_cov = np.cov(samples['w_in_1_grp'].reshape((n_samples, -1)).T)
    bias_1_cov = np.cov(samples['b_in_1_grp'].reshape((n_samples, -1)).T)

    prior_1_in_2_mu = samples['w_1_in_2_grp'].mean(axis=0)
    bias_1_in_2_mu = samples['b_1_in_2_grp'].mean(axis=0)
    prior_1_in_2_cov = np.cov(samples['w_1_in_2_grp'].reshape(
        (n_samples, -1)).T)
    bias_1_in_2_cov = np.cov(samples['b_1_in_2_grp'].reshape(
        (n_samples, -1)).T)

    prior_out_mu = samples['w_2_out_grp'].mean(axis=0)
    bias_out_mu = samples['b_2_out_grp'].mean(axis=0)
    prior_out_cov = np.cov(samples['w_2_out_grp'].reshape((n_samples, -1)).T)
    bias_out_cov = np.cov(samples['b_2_out_grp'].reshape((n_samples, -1)).T)

    with pm.Model() as flat_neural_network:

        Xs_true = pm.Normal('xtrue',
                            mu=0.,
                            sd=20.,
                            shape=annInput.shape.eval(),
                            testval=annInput.eval())

        # In order to model the correlation structure between the 2D weights,
        # we flatten them first. Now here we have to reshape to give them their
        # original 2D shape.
        weights_in_1 = (pm.MvNormal('w_in_1',
                                    prior_1_mu.flatten(),
                                    cov=prior_1_cov,
                                    shape=prior_1_cov.shape[0]).reshape(
                                        (n_data, n_hidden_1)))
        print(weights_in_1.tag.test_value.shape)

        bias_in_1 = (pm.MvNormal('bias_in_1',
                                 bias_1_mu.flatten(),
                                 cov=bias_1_cov,
                                 shape=bias_1_cov.shape[0]).reshape(
                                     (n_hidden_1, )))
        print(weights_in_1.tag.test_value.shape)

        # Layer 2
        weights_1_in_2 = (pm.MvNormal('w_1_in_2',
                                      prior_1_in_2_mu.flatten(),
                                      cov=prior_1_in_2_cov,
                                      shape=prior_1_in_2_cov.shape[0]).reshape(
                                          (n_hidden_1, n_hidden_2)))

        print(weights_1_in_2.tag.test_value.shape)

        bias_1_in_2 = (pm.MvNormal('bias_1_in_2',
                                   bias_1_in_2_mu.flatten(),
                                   cov=bias_1_in_2_cov,
                                   shape=bias_1_in_2_cov.shape[0]).reshape(
                                       (n_hidden_2, )))

        print(bias_1_in_2.tag.test_value.shape)

        # Weights from hidden layer to output
        weights_2_out = (pm.MvNormal('w_2_out',
                                     prior_out_mu.flatten(),
                                     cov=prior_out_cov,
                                     shape=prior_out_cov.shape[0]).reshape(
                                         (n_hidden_2, )))

        bias_2_out = (pm.Normal('bias_2_out',
                                mu=bias_out_mu.flatten(),
                                sd=bias_out_cov,
                                shape=(ntargets)).reshape((ntargets, )))

        # Build neural-network using relu (informed by hyperas on 2 layers) activation function
        act_1 = tt.nnet.relu(tt.dot(Xs_true, weights_in_1) + bias_in_1)
        act_2 = tt.nnet.relu(tt.dot(act_1, weights_1_in_2) + bias_1_in_2)

        act_out = tt.dot(act_2, weights_2_out) + bias_2_out
        pred = pm.Deterministic('pred', act_out)
        likelihood_x = pm.Normal('x',
                                 mu=Xs_true,
                                 sd=errAnnInput,
                                 observed=annInput)

        out = pm.Normal('out', pred, observed=annTarget, sd=errAnnTarget)

    return flat_neural_network
Exemplo n.º 8
0
jpfont = FontProperties(fname=FontPath)
#%% ロジット・モデルからのデータ生成
n = 500
np.random.seed(99)
x1 = st.uniform.rvs(loc=-np.sqrt(3.0), scale=2.0 * np.sqrt(3.0), size=n)
x2 = st.uniform.rvs(loc=-np.sqrt(3.0), scale=2.0 * np.sqrt(3.0), size=n)
q = st.logistic.cdf(0.5 * x1 - 0.5 * x2)
y = st.bernoulli.rvs(q)
X = np.stack((np.ones(n), x1, x2), axis=1)
#%% ロジット・モデルの係数の事後分布の設定
n, k = X.shape
b0 = np.zeros(k)
A0 = 0.01 * np.eye(k)
logit_model = pm.Model()
with logit_model:
    b = pm.MvNormal('b', mu=b0, tau=A0, shape=k)
    idx = pm.math.dot(X, b)
    likelihood = pm.Bernoulli('y', logit_p=idx, observed=y)
#%% 事後分布からのサンプリング
n_draws = 5000
n_chains = 4
n_tune = 1000
with logit_model:
    trace = pm.sample(draws=n_draws,
                      chains=n_chains,
                      tune=n_tune,
                      random_seed=123)
print(pm.summary(trace))
#%% 事後分布のグラフの作成
fig, ax = plt.subplots(k, 2, num=1, figsize=(8, 1.5 * k), facecolor='w')
for index in range(k):
Exemplo n.º 9
0
"""
https://twitter.com/junpenglao/status/928206574845399040
"""
import pymc3 as pm
import numpy as np
import matplotlib.pylab as plt

L = np.array([[2, 1]]).T
Sigma = L.dot(L.T) + np.diag([1e-2, 1e-2])
L_chol = np.linalg.cholesky(Sigma)

with pm.Model() as model:
    y = pm.MvNormal('y', mu=np.zeros(2), chol=L_chol, shape=2)
    tr0 = pm.sample(500, chains=1)
    tr1 = pm.fit(method='advi').sample(500)
    tr2 = pm.fit(method='fullrank_advi').sample(500)
    tr3 = pm.fit(method='svgd').sample(500)


plt.figure()
plt.plot(tr0['y'][:,0], tr0['y'][:,1], 'o', alpha=.1, label='NUTS')
plt.plot(tr1['y'][:,0], tr1['y'][:,1], 'o', alpha=.1, label='ADVI')
plt.plot(tr2['y'][:,0], tr2['y'][:,1], 'o', alpha=.1, label='FullRank')
plt.plot(tr3['y'][:,0], tr3['y'][:,1], 'o', alpha=.1, label='SVGD')
plt.legend();


"""
https://twitter.com/junpenglao/status/930826259734638598
"""
import matplotlib.pylab as plt
Exemplo n.º 10
0
def test_show_ld_pymc3(theano_config):
    with pm.Model(**theano_config) as model:
        map = starry.Map(udeg=2)
        map[1:] = pm.MvNormal("u", [0.5, 0.25], np.eye(2), shape=(2, ))
        map.show(file="tmp.pdf", point=model.test_point)
        os.remove("tmp.pdf")
Exemplo n.º 11
0
        plt.savefig('data.png')
    else:
        plt.show()

# ## First lets fit the background alone...

# In[15]:

if cpu != 'bear':
    pm_model = pm.Model()

    with pm_model:
        # Background treatment
        phi = pm.MvNormal('phi',
                          mu=phi_,
                          chol=phi_cholesky,
                          testval=phi_,
                          shape=len(phi_))

        # Construct the model
        fit = mod.model([*init_m[:11], phi])
        like = pm.Gamma('like', alpha=1, beta=1.0 / fit, observed=p)

        trace = pm.sample(target_accept=.99)

# In[16]:

if cpu != 'bear':
    pm.summary(trace)

# In[17]:
Exemplo n.º 12
0
    # Citation: http://am207.info/wiki/corr.html for code controlling correlation structure
    # The parameter nu is the prior on correlation; 0 is uniform, infinity is no corelation
    nu = pm.Uniform('nu', 1.0, 5.0)
    # The number of dimensions here is 2: correlation structure is bewteen alpha and beta by district
    num_factors: int = 2
    # Sample the correlation coefficients using the LKJ distribution
    corr_coeffs = pm.LKJCorr('corr_coeffs', nu, num_factors)

    # Sample the variances of the single factors
    sigma_priors = tt.stack([pm.Lognormal('sigma_prior_alpha', mu=0.0, tau=1.0),
                             pm.Lognormal('sigma_prior_beta', mu=0.0, tau=1.0)])

    # Make the covariance matrix as a Theano tensor
    cov = pm.Deterministic('cov', pm_make_cov(sigma_priors, corr_coeffs, num_factors))
    # The multivariate Gaussian of (alpha, beta) by district
    theta_district = pm.MvNormal('theta_district', mu=[0.0, 0.0], cov=cov, shape=(num_districts, num_factors))   

    # The vector of standard deviations for each variable; size num_factors x num_factors
    # Citation: efficient generation of sigmas and rhos from cov
    # https://github.com/aloctavodia/Statistical-Rethinking-with-Python-and-PyMC3/blob/master/Chp_13.ipynb
    sigmas = pm.Deterministic('sigmas', tt.sqrt(tt.diag(cov)))
    # correlation matrix (num_factors x num_factors)
    rhos = pm.Deterministic('rhos', tt.diag(sigmas**-1).dot(cov.dot(tt.diag(sigmas**-1))))

    # Extract the standard deviations of alpha and beta, and the correlation coefficient rho
    sigma_alpha = pm.Deterministic('sigma_alpha', sigmas[0])
    sigma_beta = pm.Deterministic('sigma_beta', sigmas[1])
    rho = pm.Deterministic('rho', rhos[0, 1])

    # Extract alpha_district and beta_district from theta_district
    alpha_district = pm.Deterministic('alpha_district', theta_district[:,0])
Exemplo n.º 13
0
def getmodel(holds={}, mu={}, sig={}, transform=False, nterms=1):

    params = [
        'logS0', 'logw', 'alpha', 'logsig', 'mean', 'u', 'logrp', 'logrm',
        't0p', 't0m'
    ]
    for p in params:
        if p not in holds:
            holds[p] = None
        if p not in mu:
            mu[p] = None
        if p not in sig:
            sig[p] = None

    with pm.Model() as model:
        logS0 = pm.Normal("logS0",
                          mu=mu["logS0"],
                          sd=sig["logS0"],
                          observed=holds['logS0'])
        logw = pm.Normal("logw",
                         mu=mu["logw"],
                         sd=sig["logw"],
                         observed=holds['logw'])

        if np.shape(flux)[0] > 1:
            alpha = pm.MvNormal("alpha",
                                mu=mu["alpha"],
                                chol=np.diag(sig["alpha"]),
                                shape=np.shape(flux)[0] - 1,
                                observed=holds['alpha'])
        logsig = pm.MvNormal("logsig",
                             mu=mu["logsig"],
                             chol=np.diag(sig["logsig"]),
                             shape=np.shape(flux)[0],
                             observed=holds['logsig'])
        mean = pm.MvNormal("mean",
                           mu=mu["mean"],
                           chol=np.diag(sig["mean"]),
                           shape=np.shape(flux)[0],
                           observed=holds['mean'])
        u = sgp.distributions.MvUniform("u",
                                        lower=[0, 0],
                                        upper=[1, 1],
                                        testval=[0.5, 0.5],
                                        observed=holds['u'])

        if transform:
            logrp = pm.Uniform("logrp",
                               lower=-20.0,
                               upper=0.0,
                               testval=mu['logrp'],
                               observed=holds['logrp'])
            logrm = pm.Uniform("logrm",
                               lower=-20.0,
                               upper=0.0,
                               testval=mu['logrm'],
                               observed=holds['logrm'])
            t0p = pm.Uniform("t0p",
                             lower=t[0],
                             upper=t[-1],
                             testval=mu['t0p'],
                             observed=holds['t0p'])
            t0m = pm.Uniform("t0m",
                             lower=t[0],
                             upper=t[-1],
                             testval=mu['t0m'],
                             observed=holds['t0m'])
        else:
            logrp = pm.Uniform("logrp",
                               lower=-20.0,
                               upper=0.0,
                               testval=mu['logrp'],
                               transform=None,
                               observed=holds['logrp'])
            logrm = pm.Uniform("logrm",
                               lower=-20.0,
                               upper=0.0,
                               testval=mu['logrm'],
                               transform=None,
                               observed=holds['logrm'])
            t0p = pm.Uniform("t0p",
                             lower=t[0],
                             upper=t[-1],
                             testval=mu['t0p'],
                             transform=None,
                             observed=holds['t0p'])
            t0m = pm.Uniform("t0m",
                             lower=t[0],
                             upper=t[-1],
                             testval=mu['t0m'],
                             transform=None,
                             observed=holds['t0m'])

        orbit = xo.orbits.KeplerianOrbit(period=5.0 * 60 * 60)
        lcp = (xo.LimbDarkLightCurve(u).get_light_curve(
            orbit=orbit,
            r=np.exp(logrp),
            t=t / (60 * 60) - t0p,
            texp=np.mean(np.diff(t)) / (60 * 60)))
        lcm = (xo.LimbDarkLightCurve(u).get_light_curve(
            orbit=orbit,
            r=np.exp(logrm),
            t=t / (60 * 60) - t0m,
            texp=np.mean(np.diff(t)) / (60 * 60)))
        mean = mean[:, None] + lcp.T[0] + lcm.T[0]

        term = xo.gp.terms.SHOTerm(log_S0=logS0,
                                   log_w0=logw,
                                   log_Q=-np.log(np.sqrt(2)))

        if np.shape(flux)[0] > 1:
            a = tt.exp(tt.concatenate([[0.0], alpha]))
            kernel = sgp.terms.KronTerm(term, alpha=a)
        else:
            kernel = term

        yerr = tt.exp(2 * logsig)
        yerr = yerr[:, None] * tt.ones(len(t))

        if np.shape(flux)[0] > 1:
            gp = xo.gp.GP(kernel, t, yerr, J=2, mean=sgp.means.KronMean(mean))
        else:
            gp = xo.gp.GP(kernel, t, yerr[0], J=2, mean=mean)
        marg = gp.marginal("gp", observed=obs.T)
    return model
Exemplo n.º 14
0
    beta = pm.Normal('beta', mu=0, sigma=100, shape=X_masked.shape[1])
    alpha = pm.HalfCauchy('alpha', beta=5)

    # impute missing X
    chol, stds, corr = pm.LKJCholeskyCov('chol',
                                         n=X_masked.shape[1],
                                         eta=2,
                                         sd_dist=pm.Exponential.dist(1),
                                         compute_corr=True)
    cov = pm.Deterministic('cov', chol.dot(chol.T))
    X_mu = pm.Normal('X_mu',
                     mu=0,
                     sigma=100,
                     shape=X_masked.shape[1],
                     testval=X_masked.mean(axis=0))
    X_modeled = pm.MvNormal('X', mu=X_mu, chol=chol, observed=X_masked)

    # observation
    mu_ = intercept + tt.dot(X_modeled, beta)

    # likelihood
    mu = tt.exp(mu_)
    likelihood = pm.Gamma('y', alpha=alpha, beta=alpha / mu, observed=y)

    # sample
    trace = pm.sample(4000, tune=1000, chains=2)

# summarize results
summary_coef = np.quantile(trace.beta,
                           axis=0,
                           q=[0.5, 0.025, 0.25, 0.75, 0.975])
Exemplo n.º 15
0
    def inference(
            self,
            observedData):  #figure out properties of beta, gamma of our model
        import pymc3 as pm
        import theano.tensor as tt

        def Smean(Stm1, Itm1, beta, N):
            return Stm1 - Stm1 * beta * Itm1 / N

        def Imean(Stm1, Itm1, beta, gamma, N):
            return Itm1 + Stm1 * Itm1 * beta / N - Itm1 * gamma

        def Rmean(Rtm1, Itm1, gamma):
            return Rtm1 + Itm1 * gamma

        def proposeEpidemic(S0, I0, R, beta, gamma, timesteps=10):
            pop = {"S": [S0], "I": [I0], "R": [R0]}
            N = S0 + I0 + R0

            for t in range(timesteps - 1):
                Stm1, Itm1, Rtm1 = pop["S"][-1], pop["I"][-1], pop["R"][-1]

                St = Smean(Stm1, Itm1, beta, N)
                It = Imean(Stm1, Itm1, beta, gamma, N)
                Rt = Rmean(Rtm1, Itm1, gamma)

                pop['S'].append(St)
                pop['I'].append(It)
                pop['R'].append(Rt)
            return pop['S'], pop['I'], pop['R']

        timesteps = len(observedData)

        S0, I0, R0 = observedData.iloc[0]

        p = proposeEpidemic(S0, I0, R0, 1., 0.5, timesteps)
        self.p = p

        with pm.Model(
        ) as model:  #uses pymc3 to make an object of a model class
            beta = pm.Normal(
                'beta', mu=1,
                sigma=0.5)  #calculate normal distribution of beta and gamma
            gamma = pm.Normal('gamma', mu=1, sigma=0.5)

            sigma = pm.Gamma('sigma', 0.5, 0.5)

            meanS, meanI, meanR = proposeEpidemic(S0, I0, R0, beta, gamma,
                                                  timesteps)

            allObs = list(observedData.I.values) + list(observedData.R.values)
            meanIandMeanR = meanI + meanR

            obs = pm.MvNormal('obs',
                              observed=allObs,
                              mu=meanIandMeanR,
                              cov=sigma * np.eye(timesteps * 2))

        with model:
            trace = pm.sample(2 * 10**3)
            self.trace = trace