def test_sim_only_x_and_t(self):
        m = 20
        x = np.random.uniform(-1, 3, (m, 3))
        t = np.random.uniform(-1, 3, (m, 2))
        y = np.random.normal(size=(m, 1))

        data = SepiaData(x_sim=x, t_sim=t, y_sim=y)
        call_data_methods(data)

        model = SepiaModel(data)
        call_model_methods(model)
        call_plot_functions(model)

        samples = model.get_samples()
        pred = SepiaEmulatorPrediction(x_pred=x,
                                       t_pred=t,
                                       samples=samples,
                                       model=model,
                                       addResidVar=True,
                                       storeMuSigma=True)
        pred.get_w()
        pred.get_y()
        pred.get_y(std=True)
        pred.get_mu_sigma()

        xpred = SepiaXvalEmulatorPrediction(samples=samples,
                                            model=model,
                                            addResidVar=True,
                                            storeMuSigma=True)
        xpred.get_w()
        xpred.get_y()
        xpred.get_y(std=True)
        xpred.get_mu_sigma()
    def test_sim_only_x_only(self):
        m = 20
        x = np.random.uniform(-1, 3, (m, 3))
        y = np.random.normal(size=(m, 50))
        y_ind = np.linspace(0, 1, 50)

        data = SepiaData(x_sim=x, y_sim=y, y_ind_sim=y_ind)
        call_data_methods(data, discrep=False)

        model = SepiaModel(data)
        call_model_methods(model)
        call_plot_functions(model)

        samples = model.get_samples()
        pred = SepiaEmulatorPrediction(x_pred=x,
                                       samples=samples,
                                       model=model,
                                       addResidVar=True,
                                       storeMuSigma=True)
        pred.get_w()
        pred.get_y()
        pred.get_y(std=True)
        pred.get_mu_sigma()

        xpred = SepiaXvalEmulatorPrediction(samples=samples,
                                            model=model,
                                            addResidVar=True,
                                            storeMuSigma=True)
        xpred.get_w()
        xpred.get_y()
        pred.get_y(std=True)
        xpred.get_mu_sigma()
    def test_sim_and_obs_noD_x_and_t(self):
        m = 20
        n = 10
        x = np.random.uniform(-1, 3, (m, 3))
        x2 = np.random.uniform(-1, 3, (n, 3))
        t = np.random.uniform(-1, 3, (m, 2))
        y = np.random.normal(size=(m, 50))
        y_ind = np.linspace(0, 1, 50)
        y2 = np.random.normal(size=(n, 20))
        y_ind2 = np.linspace(0, 1, 20)

        data = SepiaData(x_sim=x,
                         t_sim=t,
                         y_sim=y,
                         y_ind_sim=y_ind,
                         x_obs=x2,
                         y_obs=y2,
                         y_ind_obs=y_ind2)
        call_data_methods(data, discrep=False)

        model = SepiaModel(data)
        call_model_methods(model)
        call_plot_functions(model)

        samples = model.get_samples()
        pred = SepiaEmulatorPrediction(x_pred=x,
                                       t_pred=t,
                                       samples=samples,
                                       model=model,
                                       addResidVar=True,
                                       storeMuSigma=True)
        pred.get_w()
        pred.get_y()
        pred.get_y(std=True)
        pred.get_mu_sigma()

        xpred = SepiaXvalEmulatorPrediction(samples=samples,
                                            model=model,
                                            addResidVar=True,
                                            storeMuSigma=True)
        xpred.get_w()
        xpred.get_y()
        xpred.get_y(std=True)
        xpred.get_mu_sigma()

        pred = SepiaFullPrediction(x_pred=x,
                                   t_pred=t,
                                   samples=samples,
                                   model=model,
                                   addResidVar=True,
                                   storeMuSigma=True)
        pred.get_u_v()
        pred.get_mu_sigma()
        pred.get_ysim()
        pred.get_ysim(as_obs=True)
        pred.get_ysim(as_obs=True, std=True)
        pred.get_yobs()
        pred.get_yobs(as_obs=True)
        pred.get_yobs(as_obs=True, std=True)
    def test_sim_and_obs_noD_t_only_cat(self):
        m = 20
        n = 10
        t = np.concatenate([
            np.random.uniform(-1, 3,
                              (m, 3)), 1 + np.random.choice(3, size=(m, 1))
        ],
                           axis=1)
        y = np.random.normal(size=(m, 50))
        y_ind = np.linspace(0, 1, 50)
        y2 = np.random.normal(size=(n, 20))
        y_ind2 = np.linspace(0, 1, 20)
        cat_inds = [0, 0, 0, 3]

        data = SepiaData(t_sim=t,
                         y_sim=y,
                         y_ind_sim=y_ind,
                         y_obs=y2,
                         y_ind_obs=y_ind2,
                         t_cat_ind=cat_inds)
        call_data_methods(data, discrep=False)

        model = SepiaModel(data)
        call_model_methods(model)
        call_plot_functions(model)

        samples = model.get_samples()
        pred = SepiaEmulatorPrediction(t_pred=t,
                                       samples=samples,
                                       model=model,
                                       addResidVar=True,
                                       storeMuSigma=True)
        pred.get_w()
        pred.get_y()
        pred.get_y(std=True)
        pred.get_mu_sigma()

        xpred = SepiaXvalEmulatorPrediction(samples=samples,
                                            model=model,
                                            addResidVar=True,
                                            storeMuSigma=True)
        xpred.get_w()
        xpred.get_y()
        pred.get_y(std=True)
        xpred.get_mu_sigma()

        pred = SepiaFullPrediction(t_pred=t,
                                   samples=samples,
                                   model=model,
                                   addResidVar=True,
                                   storeMuSigma=True)
        pred.get_u_v()
        pred.get_mu_sigma()
        pred.get_ysim()
        pred.get_ysim(as_obs=True)
        pred.get_ysim(as_obs=True, std=True)
        pred.get_yobs()
        pred.get_yobs(as_obs=True)
        pred.get_yobs(as_obs=True, std=True)
    def test_sim_only_x_and_t_cat(self):
        m = 20
        x = np.concatenate([
            np.random.uniform(-1, 3,
                              (m, 3)), 1 + np.random.choice(3, size=(m, 1))
        ],
                           axis=1)
        t = np.concatenate([
            np.random.uniform(-1, 3,
                              (m, 2)), 1 + np.random.choice(4, size=(m, 1))
        ],
                           axis=1)
        x_cat_ind = [0, 0, 0, 3]
        t_cat_ind = [0, 0, 4]

        y = np.random.normal(size=(m, 50))
        y_ind = np.linspace(0, 1, 50)

        data = SepiaData(x_sim=x,
                         t_sim=t,
                         x_cat_ind=x_cat_ind,
                         t_cat_ind=t_cat_ind,
                         y_sim=y,
                         y_ind_sim=y_ind)
        call_data_methods(data, discrep=False)

        model = SepiaModel(data)
        call_model_methods(model)
        call_plot_functions(model)

        samples = model.get_samples()
        pred = SepiaEmulatorPrediction(x_pred=x,
                                       t_pred=t,
                                       samples=samples,
                                       model=model,
                                       addResidVar=True,
                                       storeMuSigma=True)
        pred.get_w()
        pred.get_y()
        pred.get_y(std=True)
        pred.get_mu_sigma()

        xpred = SepiaXvalEmulatorPrediction(samples=samples,
                                            model=model,
                                            addResidVar=True,
                                            storeMuSigma=True)
        xpred.get_w()
        xpred.get_y()
        xpred.get_y(std=True)
        xpred.get_mu_sigma()
    def test_sim_and_obs_t_only(self):
        m = 20
        n = 10
        t = np.random.uniform(-1, 3, (m, 3))
        y = np.random.normal(size=(m, 1))
        y2 = np.random.normal(size=(n, 1))

        data = SepiaData(t_sim=t, y_sim=y, y_obs=y2)
        call_data_methods(data)

        model = SepiaModel(data)
        call_model_methods(model)
        call_plot_functions(model)

        samples = model.get_samples()
        pred = SepiaEmulatorPrediction(t_pred=t,
                                       samples=samples,
                                       model=model,
                                       addResidVar=True,
                                       storeMuSigma=True)
        pred.get_w()
        pred.get_y()
        pred.get_y(std=True)
        pred.get_mu_sigma()

        xpred = SepiaXvalEmulatorPrediction(samples=samples,
                                            model=model,
                                            addResidVar=True,
                                            storeMuSigma=True)
        xpred.get_w()
        xpred.get_y()
        xpred.get_y(std=True)
        xpred.get_mu_sigma()

        pred = SepiaFullPrediction(t_pred=t,
                                   samples=samples,
                                   model=model,
                                   addResidVar=True,
                                   storeMuSigma=True)
        pred.get_u_v()
        pred.get_mu_sigma()
        pred.get_ysim()
        pred.get_ysim(as_obs=True)
        pred.get_ysim(as_obs=True, std=True)
        pred.get_yobs()
        pred.get_yobs(as_obs=True)
        pred.get_yobs(as_obs=True, std=True)
Beispiel #7
0
    def test_multivariate_sim_and_obs_mcmc_thetacon(self):
        """
        Tests mcmc for multivariate sim and obs model with theta constraint function
        """

        print(
            'Testing multivariate sim and obs SepiaMCMC w theta constraint...',
            flush=True)

        data = self.multi_sim_and_obs_data

        def fcon(x):
            return (x[:, 0] + x[:, 1]) < 0.8

        theta_init = np.array([[0.2, 0.3, 0.5]])

        model = SepiaModel(data, theta_init=theta_init, theta_fcon=fcon)

        model.tune_step_sizes(50, 10)
        model.do_mcmc(100)
        samples = model.get_samples()
        self.assertTrue(
            np.all(samples['theta'][:, 0] + samples['theta'][:, 1] < 0.8))
    def test_sim_only_x_only_cat(self):
        m = 20
        x = np.concatenate([
            np.random.uniform(-1, 3,
                              (m, 3)), 1 + np.random.choice(3, size=(m, 1))
        ],
                           axis=1)
        y = np.random.normal(size=(m, 1))
        cat_inds = [0, 0, 0, 3]

        data = SepiaData(x_sim=x, y_sim=y, x_cat_ind=cat_inds)
        call_data_methods(data)

        model = SepiaModel(data)
        call_model_methods(model)
        call_plot_functions(model)

        samples = model.get_samples()
        pred = SepiaEmulatorPrediction(x_pred=x,
                                       samples=samples,
                                       model=model,
                                       addResidVar=True,
                                       storeMuSigma=True)
        pred.get_w()
        pred.get_y()
        pred.get_y(std=True)
        pred.get_mu_sigma()

        xpred = SepiaXvalEmulatorPrediction(samples=samples,
                                            model=model,
                                            addResidVar=True,
                                            storeMuSigma=True)
        xpred.get_w()
        xpred.get_y()
        xpred.get_y(std=True)
        xpred.get_mu_sigma()
data = SepiaData(t_sim=design,
                 y_sim=y_sim,
                 y_ind_sim=y_ind,
                 y_obs=y_obs,
                 y_ind_obs=y_ind)
data.standardize_y()
data.transform_xt()
data.create_K_basis(n_features - 1)
print(data)

# Setup model
# We have a known observation error
Sigy = np.diag(
    np.squeeze(
        (0.01 * np.ones(n_features) * y_obs) / data.sim_data.orig_y_sd**2))
model = SepiaModel(data, Sigy)

# Modify priors to match Matlab
model.params.lamWs.prior.bounds[1] = np.inf
model.params.lamWs.prior.params = [np.ones((1, 11)), np.zeros((1, 11))]

# Do mcmc
model.tune_step_sizes(100, 25)
model.do_mcmc(10000)
samples_dict = model.get_samples()

with open('data/sepia_mcmc_samples1-5000.pkl', 'wb') as f:
    pickle.dump(samples_dict, f)

with open('data/sepia_model.pkl', 'wb') as f:
    pickle.dump(model, f)
print('Multi-process worker mcmc and samples return took %f s' %
      (tref2 - tref1))
print('              with overhead of %f s' % ((tref1 - tref0) +
                                               (tref3 - tref2)))
print('              total = %f s' % (tref3 - tref0))

#
# The samples from the parallel chains are in the Sepia model object now
# Can proceed with the Sepia model object as normal
#

# Compare the trace plots for qualitative equivalence of samples
# between the "regular" model object and the model object constituted with samples

samples_dict_ref = model_ref.get_samples()
samples_dict = model.get_samples()

theta_names = ['C']

mcmc_trace_ref = SepiaPlot.mcmc_trace(samples_dict_ref, theta_names)
mcmc_trace = SepiaPlot.mcmc_trace(samples_dict, theta_names)

mcmc_trace_ref.show()
mcmc_trace.show()

p_stats_ref = SepiaPlot.param_stats(samples_dict_ref,
                                    theta_names=theta_names,
                                    q1=.05,
                                    q2=.95,
                                    digits=4)
    def test_sim_and_obs_x_and_t_cat(self):
        m = 20
        n = 10
        x = np.concatenate([
            np.random.uniform(-1, 3,
                              (m, 3)), 1 + np.random.choice(3, size=(m, 1))
        ],
                           axis=1)
        x2 = np.concatenate([
            np.random.uniform(-1, 3,
                              (n, 3)), 1 + np.random.choice(3, size=(n, 1))
        ],
                            axis=1)
        t = np.concatenate([
            np.random.uniform(-1, 3,
                              (m, 2)), 1 + np.random.choice(4, size=(m, 1))
        ],
                           axis=1)
        y = np.random.normal(size=(m, 50))
        y_ind = np.linspace(0, 1, 50)
        y2 = np.random.normal(size=(n, 20))
        y_ind2 = np.linspace(0, 1, 20)
        x_cat_ind = [0, 0, 0, 3]
        t_cat_ind = [0, 0, 4]

        data = SepiaData(x_sim=x,
                         t_sim=t,
                         x_cat_ind=x_cat_ind,
                         t_cat_ind=t_cat_ind,
                         y_sim=y,
                         y_ind_sim=y_ind,
                         x_obs=x2,
                         y_obs=y2,
                         y_ind_obs=y_ind2)
        call_data_methods(data)

        model = SepiaModel(data)
        call_model_methods(model)
        call_plot_functions(model)

        samples = model.get_samples()
        pred = SepiaEmulatorPrediction(x_pred=x,
                                       t_pred=t,
                                       samples=samples,
                                       model=model,
                                       addResidVar=True,
                                       storeMuSigma=True)
        pred.get_w()
        pred.get_y()
        pred.get_y(std=True)
        pred.get_mu_sigma()

        xpred = SepiaXvalEmulatorPrediction(samples=samples,
                                            model=model,
                                            addResidVar=True,
                                            storeMuSigma=True)
        xpred.get_w()
        xpred.get_y()
        xpred.get_y(std=True)
        xpred.get_mu_sigma()

        pred = SepiaFullPrediction(x_pred=x,
                                   t_pred=t,
                                   samples=samples,
                                   model=model,
                                   addResidVar=True,
                                   storeMuSigma=True)
        pred.get_u_v()
        pred.get_mu_sigma()
        pred.get_ysim()
        pred.get_ysim(as_obs=True)
        pred.get_ysim(as_obs=True, std=True)
        pred.get_yobs()
        pred.get_yobs(as_obs=True)
        pred.get_yobs(as_obs=True, std=True)
        pred.get_discrepancy()
        pred.get_discrepancy(as_obs=True)
        pred.get_discrepancy(as_obs=True, std=True)
Beispiel #12
0
    def test_full_setup_LL_pred(self):
        # Set up and check calibration model
        x_obs = np.ones((3, 2)) * np.array([0.5, 0.75, 0.25]).reshape((-1, 1))
        y_obs = np.block([[-0.1, 0.1], [-0.2, 0.3], [0.1, 0]])

        # augment to also test more than scalar dimensions in x and t
        x_sim_cal = np.hstack((0.5 * np.ones(
            (self.x_sim.shape[0], 1)), self.x_sim[:, :1]))
        t_sim_cal = self.x_sim[:, 1:]
        xt_sim_sep = [np.array(0.5).reshape(1, 1)] + self.x_sim_kron

        y_sim_std = (self.y_sim - np.mean(self.y_sim, axis=0).reshape(
            1, -1)) / np.std(self.y_sim, axis=0, ddof=1).reshape(1, -1)

        dc = SepiaData(x_sim=x_sim_cal,
                       t_sim=t_sim_cal,
                       y_sim=y_sim_std,
                       x_obs=x_obs,
                       y_obs=y_obs,
                       y_ind_sim=self.y_ind_sim,
                       y_ind_obs=self.y_ind_sim)
        dc.create_K_basis(K=np.eye(2))
        dc.create_D_basis(D_sim=np.eye(2), D_obs=np.eye(2))
        dc.transform_xt(x_notrans=True, t_notrans=True)
        dc.standardize_y(y_mean=0, y_sd=1)
        print(dc)
        cmod = SepiaModel(dc)

        print('Calibration model LL=%f' % compute_log_lik(cmod))

        kdc = SepiaData(xt_sim_sep=xt_sim_sep,
                        y_sim=y_sim_std,
                        x_obs=x_obs,
                        y_obs=y_obs,
                        y_ind_sim=self.y_ind_sim,
                        y_ind_obs=self.y_ind_sim)
        kdc.create_K_basis(K=np.eye(2))
        kdc.create_D_basis(D_sim=np.eye(2), D_obs=np.eye(2))
        kdc.transform_xt(x_notrans=True, t_notrans=True)
        kdc.standardize_y(y_mean=0, y_sd=1)
        print(kdc)
        kcmod = SepiaModel(kdc)

        print('Calibration Sep model LL=%f' % compute_log_lik(kcmod))

        self.assertAlmostEqual(compute_log_lik(cmod),
                               compute_log_lik(kcmod),
                               places=5)

        print(
            'Running MCMC on Calibration model in Sep pred mode, regular design'
        )
        np.random.seed(42)
        t1 = time()
        cmod.do_mcmc(10)
        print('sampling time %f' % (time() - t1))
        csamp = cmod.get_samples(sampleset=[cmod.get_last_sample_ind()])
        cpred = SepiaFullPrediction(mode='Sep',
                                    model=cmod,
                                    samples=csamp,
                                    storeMuSigma=True,
                                    x_pred=np.array([0.5, 0.5]).reshape(
                                        (1, -1)))
        print(cpred.get_ysim())
        csm, css = cpred.get_mu_sigma()
        print(csm)
        print(css)

        print(
            'Running MCMC on Calibration model in Sep pred mode, separable design'
        )
        np.random.seed(42)
        t1 = time()
        kcmod.do_mcmc(10)
        print('sampling time %f' % (time() - t1))
        kcsamp = kcmod.get_samples(sampleset=[kcmod.get_last_sample_ind()])
        kcpred = SepiaFullPrediction(mode='Sep',
                                     model=kcmod,
                                     samples=kcsamp,
                                     storeMuSigma=True,
                                     x_pred=np.array([0.5, 0.5]).reshape(
                                         (1, -1)))
        print(kcpred.get_ysim())
        kcsm, kcss = kcpred.get_mu_sigma()
        print(kcsm)
        print(kcss)

        print('testing max difference which is %g' % np.max(abs(csm - kcsm)))
        self.assertAlmostEqual(0, np.max(abs(csm - kcsm)))

        print('testing max difference which is %g' % np.max(abs(css - kcss)))
        self.assertAlmostEqual(0, np.max(abs(css - kcss)))

        ###### Timing for predictions

        test_x_pred = np.random.rand(10, 2)

        csamp = cmod.get_samples()
        t1 = time()
        cpred0 = SepiaFullPrediction(mode='notSep',
                                     model=cmod,
                                     samples=csamp,
                                     storeMuSigma=True,
                                     x_pred=test_x_pred)
        print('predict time non-Sep in non-Sep mode %f' % (time() - t1))
        t1 = time()
        cpred = SepiaFullPrediction(mode='Sep',
                                    model=cmod,
                                    samples=csamp,
                                    storeMuSigma=True,
                                    x_pred=test_x_pred)
        print('predict time non-sep in Sep mode %f' % (time() - t1))

        kcsamp = kcmod.get_samples()
        t1 = time()
        kcpred = SepiaFullPrediction(mode='Sep',
                                     model=kcmod,
                                     samples=kcsamp,
                                     storeMuSigma=True,
                                     x_pred=test_x_pred)
        print('predict time Sep %f' % (time() - t1))

        pass
Beispiel #13
0
    for j in range(param_shape):
        plt.subplot(nrow, ncol, j + 1)
        plt.hist(samples_dict[k][:, j])
        plt.xlabel(k)
    plt.show()


#%% Emulator-only predictions

model.verbose=True

from sepia.SepiaPredict import SepiaEmulatorPrediction
x_pred=np.linspace(0,1,9).reshape((9,1))
# make t_pred most like the calibrated thetas for comparison to below
t_pred=np.tile(np.array([1,0,1]).reshape(1,3),(9,1)) 
pred_samples=model.get_samples(numsamples=7)
pred=SepiaEmulatorPrediction(x_pred=x_pred, samples=pred_samples, model=model, t_pred=t_pred)

predw=pred.get_w()
plt.figure()
plt.plot(np.mean(predw,0).T)
plt.xlabel('PCA weights w')
plt.ylabel('w value for each predicted x \n mean over samples')
plt.show()

predystd=pred.get_y(std=True)
plt.figure()
plt.plot(model.data.sim_data.y_ind,np.mean(predystd,0).T)
plt.xlabel('native data space y')
plt.ylabel('standardized predicted y for each predicted x \n mean over samples')
plt.show()
Beispiel #14
0
data = SepiaData(x_sim=xt_sim[:, 0][:, None], t_sim=xt_sim[:, 1][:, None], y_sim=y_sim, y_ind_sim=y_ind_sim,
                 x_obs=x_obs, y_obs=y_obs, y_ind_obs=y_ind_obs)
data.standardize_y()
data.transform_xt()
data.create_K_basis(n_pc=n_pc)
data.create_D_basis(D_obs=matfile['Dobs'].T)
print(data)

np.random.seed(int(seed))
model = SepiaModel(data)
if lamWOs_init > 0:
    model.params.lamWOs.val = np.array([[lamWOs_init]])

model.do_mcmc(nburn + nsamp)
np.random.seed(seed)
psamps = model.get_samples(sampleset=range(50), flat=True)

@timeit
def do_wPred():
    for _ in range(10):
        pred = wPred([0.5], psamps, model.num, model.data, returnMuSigma=True, addResidVar=True, returnRlz=True)

@timeit
def do_uvPred():
    for _ in range(50):
        pred = uvPred([0.5], psamps, model.num, model.data, returnMuSigma=True, useAltW=False)

print('\nuvPred x10')
do_uvPred()

print('wPred x10')