示例#1
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_univariate_sim_and_obs_saveload(self):
        """
        Tests save/load for univariate sim and obs model
        """

        print('Testing univariate sim and obs save/load...', flush=True)

        data = self.univ_sim_and_obs_data

        model = SepiaModel(data)
        model.tune_step_sizes(50, 10)
        model.do_mcmc(100)
        model.save_model_info(overwrite=True)

        new_model = SepiaModel(data)
        new_model.restore_model_info()

        # check num stuff
        self.assertEqual(model.num.logLik, new_model.num.logLik)
        self.assertTrue(np.allclose(model.num.uw, new_model.num.uw))
        self.assertTrue(np.allclose(model.num.SigWl, new_model.num.SigWl))

        # check param stuff
        for p1, p2 in zip(model.params, new_model.params):
            self.assertTrue(p1.name == p2.name)
            self.assertTrue(np.allclose(p1.fixed, p2.fixed))
            self.assertTrue(np.allclose(p1.val, p2.val))
            self.assertTrue(np.allclose(np.array(p1.mcmc.draws), np.array(p2.mcmc.draws)))
            if p1.name != 'logPost':
                self.assertTrue(np.allclose(p1.mcmc.stepParam, p2.mcmc.stepParam))
                self.assertTrue(p1.mcmc.stepType == p2.mcmc.stepType)
                self.assertTrue(np.allclose(np.array(p1.prior.bounds), np.array(p2.prior.bounds)))
                self.assertTrue(np.allclose(np.array(p1.prior.params), np.array(p2.prior.params)))
                self.assertTrue(p1.prior.dist == p2.prior.dist)

        new_model.do_mcmc(10)
示例#3
0
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)
    D_sim[:, j] = norm.pdf(h_sim, D_grid[j], D_width)
    D_dense[:, j] = norm.pdf(h_dense, D_grid[j], D_width)
data.create_D_basis(D_obs=D_obs.T, D_sim=D_sim)
#
# Data setup completed
#

nmcmc = 5000
#
# Standard mcmc reference model setup and sampling
#
model_ref = SepiaModel(data)
model_ref.tune_step_sizes(50, 20, verbose=False)
# burn in the model. This is qualitative, and needs to be assessed on trace plots
# This model is actually OK, but do 10 samples for 'burn-in'
model_ref.do_mcmc(10, prog=False)
# and discard those samples
model_ref.clear_samples()
tref = time()  # timing start
model_ref.do_mcmc(nmcmc)
print('Single-process mcmc took %f s' % (time() - tref), flush=True)

#
# Perform the same operation with parallel mcmc chains
#
model = SepiaModel(data)  # new model instance
model.tune_step_sizes(50, 20, verbose=False)  # optimize step sizes
model.do_mcmc(10, prog=False)  # The same burn-in process
model.clear_samples()  # reset the model's sample set, leaving the model state

#
示例#5
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
示例#6
0
y_ind_obs = matfile['y_ind_obs'].squeeze()
x_obs = matfile['x_obs']
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()