def test_emulator_setup_loglik(self): # set up regular model d = SepiaData(x_sim=self.x_sim, y_sim=self.y_sim, y_ind_sim=np.array([0, 1])) d.create_K_basis(K=np.eye(2)) d.transform_xt(x_notrans=True) d.standardize_y(scale='columnwise') print(d) mod = SepiaModel(d) print('Emulator model LL=%f \n' % compute_log_lik(mod)) # set up kron model kd = SepiaData(xt_sim_sep=self.x_sim_kron, y_sim=self.y_sim, y_ind_sim=self.y_ind_sim) kd.create_K_basis(K=np.eye(2)) kd.transform_xt(x_notrans=True) kd.standardize_y(scale='columnwise') print(kd) kmod = SepiaModel(kd) print('Emulator Sep model LL=%f \n' % compute_log_lik(kmod)) self.assertAlmostEqual(compute_log_lik(mod), compute_log_lik(kmod), places=5) pass
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_multivariate_sim_only_setup(self): """ Tests setup for multivariate sim only model """ d = SepiaData(t_sim=self.data_dict['t_sim'], y_sim=self.data_dict['y_sim'], y_ind_sim=self.data_dict['y_ind_sim']) print('Testing multivariate sim-only SepiaModelSetup...', flush=True) print(d, flush=True) # Try it without doing standardization/transform to be sure it doesn't break model_notrans = SepiaModel(copy.deepcopy(d)) # Do explicit transformation d.transform_xt() d.standardize_y() model = SepiaModel(d) # Check that either way gives same transformation self.assertTrue(np.allclose(model_notrans.data.sim_data.orig_y_mean, model.data.sim_data.orig_y_mean)) self.assertTrue(np.allclose(model_notrans.data.sim_data.orig_y_sd, model.data.sim_data.orig_y_sd)) self.assertTrue(np.allclose(model_notrans.data.sim_data.y_std, model.data.sim_data.y_std)) self.assertTrue(np.allclose(model_notrans.data.sim_data.t_trans, model.data.sim_data.t_trans)) # Check model components are set up as expected self.assertTrue(not model.num.scalar_out) self.assertTrue(model.num.sim_only) self.assertTrue(model.num.m == 100) self.assertTrue(model.num.n == 0) self.assertTrue(model.num.p == 1) self.assertTrue(model.num.q == 3) self.assertTrue(model.num.pu == 5) self.assertTrue(model.num.pv == 0) #self.assertTrue(np.allclose(model.num.w, model.data.sim_data.y_std)) # TODO compute projection # Check parameter setup -- betaU betaU = model.params.betaU self.assertTrue(betaU.val_shape == (model.num.q + model.num.p, model.num.pu)) self.assertTrue(betaU.prior.dist == 'Beta') self.assertTrue(betaU.mcmc.stepType == 'BetaRho') # Check parameter setup -- lamUz lamUz = model.params.lamUz self.assertTrue(lamUz.val_shape == (1, model.num.pu)) self.assertTrue(lamUz.prior.dist == 'Gamma') self.assertTrue(lamUz.mcmc.stepType == 'PropMH') # Check parameter setup -- lamWOs lamWOs = model.params.lamWOs self.assertTrue(lamWOs.val_shape == (1, 1)) self.assertTrue(lamWOs.prior.dist == 'Gamma') self.assertTrue(lamWOs.mcmc.stepType == 'PropMH') # Check parameter setup -- lamWs lamWs = model.params.lamWs self.assertTrue(lamWs.val_shape == (1, model.num.pu)) self.assertTrue(lamWs.prior.dist == 'Gamma') self.assertTrue(lamWs.mcmc.stepType == 'PropMH') mcmc_list_names = [p.name for p in model.params.mcmcList] self.assertTrue(set(mcmc_list_names) == set(['betaU', 'lamUz', 'lamWOs', 'lamWs']))
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 setUp(self, m=100, n=1, nt_sim=50, nt_obs=20, n_theta=3, n_basis=5, sig_n=0.1, seed=42): n_hier = 3 self.hier_idx = np.array([[0, 0, 0]]) #self.hier_idx = np.array([[1, 1, 1], [2, -1, 2]]) # TODO this fails for multivariate; cant use for univariate now multi_data_list = [] univ_data_list = [] for si in range(n_hier): multi_data_dict = generate_data.generate_multi_sim_and_obs(m=m, n=n, nt_sim=nt_sim, nt_obs=nt_obs, n_theta=n_theta, n_basis=n_basis, sig_n=sig_n, seed=seed) univ_data_dict = generate_data.generate_univ_sim_and_obs(m=m, n=n, sig_n=sig_n, seed=seed) d = SepiaData(t_sim=univ_data_dict['t_sim'], y_sim=univ_data_dict['y_sim'], y_obs=univ_data_dict['y_obs']) d.transform_xt() d.standardize_y() univ_data_list.append(d) d = SepiaData(t_sim=multi_data_dict['t_sim'], y_sim=multi_data_dict['y_sim'], y_ind_sim=multi_data_dict['y_ind_sim'], y_obs=multi_data_dict['y_obs'], y_ind_obs=multi_data_dict['y_ind_obs']) d.transform_xt() d.standardize_y() d.create_K_basis(5) d.create_D_basis('constant') multi_data_list.append(d) self.univ_model_list = [SepiaModel(d) for d in univ_data_list] self.multi_model_list = [SepiaModel(d) for d in multi_data_list]
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_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 setUp(self, m=20, n=1, nt_sim=30, nt_obs=20, n_theta=3, n_basis=5, sig_n=0.1, seed=42): multi_data_dict = generate_data.generate_multi_sim_and_obs( m=m, n=n, nt_sim=nt_sim, nt_obs=nt_obs, n_theta=n_theta, n_basis=n_basis, sig_n=sig_n, seed=seed) univ_data_dict = generate_data.generate_univ_sim_and_obs(m=m, n=n, sig_n=sig_n, seed=seed) d = SepiaData(t_sim=univ_data_dict['t_sim'], y_sim=univ_data_dict['y_sim']) d.transform_xt() d.standardize_y() self.univ_sim_only_model = SepiaModel(d) d = SepiaData(t_sim=univ_data_dict['t_sim'], y_sim=univ_data_dict['y_sim'], y_obs=univ_data_dict['y_obs']) d.transform_xt() d.standardize_y() self.univ_sim_and_obs_model = SepiaModel(d) d = SepiaData(t_sim=multi_data_dict['t_sim'], y_sim=multi_data_dict['y_sim'], y_ind_sim=multi_data_dict['y_ind_sim']) d.transform_xt() d.standardize_y() d.create_K_basis(5) self.multi_sim_only_model = SepiaModel(d) t = np.concatenate([ multi_data_dict['t_sim'], np.random.choice(range(1, 5), (m, 1), replace=True) ], axis=1) d = SepiaData(t_sim=t, y_sim=multi_data_dict['y_sim'], y_ind_sim=multi_data_dict['y_ind_sim'], t_cat_ind=[0, 0, 0, 4]) d.transform_xt() d.standardize_y() d.create_K_basis(5) self.multi_sim_only_catind_model = SepiaModel(d)
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 setup_univ_sim_only(m=300, seed=42., n_lik=0, n_mcmc=0, n_pred=0, n_lev=0, n_burn=0, sens=0): try: eng = matlab.engine.start_matlab() eng.cd(root_path) eng.addpath('matlab/', nargout=0) res = eng.setup_univ_sim_only(m, seed, n_lik, n_mcmc, n_pred, n_lev, n_burn, sens, nargout=1) eng.quit() except Exception as e: print(e) print( 'Matlab error; make sure matlab.engine installed, check Matlab code for errors.' ) y = np.array(res['y'], dtype=float) xt = np.array(res['xt'], dtype=float) data = SepiaData(x_sim=xt[:, 0][:, None], t_sim=xt[:, 1][:, None], y_sim=y) print(data) data.standardize_y() data.transform_xt() model = SepiaModel(data) return model, res
def setup_multi_sim_and_obs_sharedtheta(m=100, n=10, nt_sim=20, nt_obs=15, noise_sd=0.1, nx=5, n_pc=10, seed=42., n_lik=0, n_mcmc=0, n_pred=0, n_shared=2, clist=[], fix_K=False): try: eng = matlab.engine.start_matlab() eng.cd(root_path) eng.addpath('matlab/', nargout=0) res = eng.setup_multi_sim_and_obs_sharedtheta(m, n, nt_sim, nt_obs, noise_sd, nx, n_pc, seed, n_lik, n_mcmc, n_pred, n_shared, matlab.double(clist), nargout=1) eng.quit() except Exception as e: print(e) print( 'Matlab error; make sure matlab.engine installed, check Matlab code for errors.' ) y = np.array(res['y'], dtype=float) # (m, nt_sim, n_shared) y_ind = np.array(res['y_ind'], dtype=float).squeeze() # (nt_sim, n_shared) xt = np.array(res['xt'], dtype=float) # (m, nx, n_shared) y_obs = np.array(res['y_obs'], dtype=float) # (n, nt_sim, n_shared) y_ind_obs = np.array(res['y_ind_obs'], dtype=float).squeeze() # (nt_obs, n_shared) x_obs = np.array(res['x_obs'], dtype=float) # (n, 1, n_shared) model_list = [] for i in range(n_shared): data = SepiaData(x_sim=xt[:, 0, i][:, None], t_sim=xt[:, 1:, i], y_sim=y[:, :, i], y_ind_sim=y_ind[:, i], x_obs=x_obs[:, :, i], y_obs=y_obs[:, :, i], y_ind_obs=y_ind_obs[:, i]) data.standardize_y() data.transform_xt() data.create_K_basis(n_pc) model = SepiaModel(data) model_list.append(model) return model_list, res
def setup_univ_sim_and_obs(m=100, n=50, seed=42., n_lik=0, n_mcmc=0, n_pred=0): try: eng = matlab.engine.start_matlab() eng.cd(root_path) eng.addpath('matlab/', nargout=0) res = eng.setup_univ_sim_and_obs(m, n, seed, n_lik, n_mcmc, n_pred, nargout=1) eng.quit() except Exception as e: print(e) print( 'Matlab error; make sure matlab.engine installed, check Matlab code for errors.' ) y = np.array(res['y'], dtype=float) xt = np.array(res['xt'], dtype=float) y_obs = np.array(res['y_obs'], dtype=float) x_obs = np.array(res['x_obs'], dtype=float).reshape((n, 1)) data = SepiaData(x_sim=xt[:, 0][:, None], t_sim=xt[:, 1][:, None], y_sim=y, x_obs=x_obs, y_obs=y_obs) data.standardize_y() data.transform_xt() print(data) model = SepiaModel(data) return model, res
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)
def setup_multi_sim_and_obs(m=100, n=10, nt_sim=20, nt_obs=15, noise_sd=0.1, nx=5, n_pc=10, seed=42., n_lik=0, n_mcmc=0, n_pred=0, fix_K=False): try: eng = matlab.engine.start_matlab() eng.cd(root_path) eng.addpath('matlab/', nargout=0) res = eng.setup_multi_sim_and_obs(m, n, nt_sim, nt_obs, noise_sd, nx, n_pc, seed, n_lik, n_mcmc, n_pred, nargout=1) eng.quit() except Exception as e: print(e) print( 'Matlab error; make sure matlab.engine installed, check Matlab code for errors.' ) y = np.array(res['y'], dtype=float) y_ind = np.array(res['y_ind'], dtype=float).squeeze() xt = np.array(res['xt'], dtype=float) y_obs = np.array(res['y_obs'], dtype=float) y_ind_obs = np.array(res['y_ind_obs'], dtype=float).squeeze() x_obs = np.array(res['x_obs'], dtype=float) data = SepiaData(x_sim=xt[:, 0][:, None], t_sim=xt[:, 1:], y_sim=y, y_ind_sim=y_ind, x_obs=x_obs, y_obs=y_obs, y_ind_obs=y_ind_obs) data.standardize_y() data.transform_xt() if fix_K: # means use the K from matlab - avoid issues with positive/negative component ambiguity data.create_K_basis(n_pc, K=np.array(res['K']).T) else: data.create_K_basis(n_pc) data.create_D_basis('constant') print(data) model = SepiaModel(data) return model, res
def setUp(self, m=100, n=1, nt_sim=50, nt_obs=20, n_theta=3, n_basis=5, sig_n=0.1, seed=42): multi_data_dict = generate_data.generate_multi_sim_and_obs(m=m, n=n, nt_sim=nt_sim, nt_obs=nt_obs, n_theta=n_theta, n_basis=n_basis, sig_n=sig_n, seed=seed) univ_data_dict = generate_data.generate_univ_sim_and_obs(m=m, n=n, sig_n=sig_n, seed=seed) d = SepiaData(x_sim=univ_data_dict['t_sim'], y_sim=univ_data_dict['y_sim']) d.transform_xt() d.standardize_y() self.univ_sim_only_model = SepiaModel(d) d = SepiaData(t_sim=univ_data_dict['t_sim'], y_sim=univ_data_dict['y_sim'], y_obs=univ_data_dict['y_obs']) d.transform_xt() d.standardize_y() self.univ_sim_and_obs_model = SepiaModel(d) d = SepiaData(x_sim=multi_data_dict['t_sim'], y_sim=multi_data_dict['y_sim'], y_ind_sim=multi_data_dict['y_ind_sim']) d.transform_xt() d.standardize_y() d.create_K_basis(5) self.multi_sim_only_model = SepiaModel(d) d = SepiaData(t_sim=multi_data_dict['t_sim'], y_sim=multi_data_dict['y_sim'], y_ind_sim=multi_data_dict['y_ind_sim'], y_obs=multi_data_dict['y_obs'], y_ind_obs=multi_data_dict['y_ind_obs']) d.transform_xt() d.standardize_y() d.create_K_basis(5) self.multi_sim_and_obs_noD_model = SepiaModel(d) d = SepiaData(t_sim=multi_data_dict['t_sim'], y_sim=multi_data_dict['y_sim'], y_ind_sim=multi_data_dict['y_ind_sim'], y_obs=multi_data_dict['y_obs'], y_ind_obs=multi_data_dict['y_ind_obs']) d.transform_xt() d.standardize_y() d.create_K_basis(5) d.create_D_basis('linear') self.multi_sim_and_obs_model = SepiaModel(d)
def test_multivariate_sim_and_obs_ragged_setup(self): m = 700 # number of simulated observations p = 3 # dimension of x (simulation inputs) ell_sim = 1000 # dimension of y output sim pu = 3 # number of PCs q = 2 # dimension of t (extra sim inputs) n = 5 # number of observed observations ell_obs = np.random.randint(100, 600, n) y_ind_sim = np.linspace(0, 100, ell_sim) K_true_sim = np.vstack([ 0.5 * (np.sin(y_ind_sim) + 1), np.square(-y_ind_sim + 50) / 2500, y_ind_sim / 100 ]) y_sim = np.transpose( np.log(1 + y_ind_sim)[:, None] + np.dot( K_true_sim.T, 2 * np.array([1, 0.5, 0.2])[:, None] * np.random.normal(0, 1, (pu, m)))) x_sim = 0.5 * np.random.uniform(-1, 3, (m, p)) t = np.random.uniform(-10, 10, (m, q)) y_ind_obs = [ np.linspace(0, 100, ell_obs[i]) + np.random.uniform(-3, 3, ell_obs[i]) for i in range(len(ell_obs)) ] for yi in y_ind_obs: yi[yi < 0] = 0 K_true_obs = [ np.vstack( [0.5 * (np.sin(yi) + 1), np.square(-yi + 50) / 2500, yi / 100]) for yi in y_ind_obs ] y_obs = [ 10 + np.squeeze( np.log(1 + y_ind_obs[i])[:, None] + np.dot( K_true_obs[i].T, 2 * np.array([1, 0.5, 0.2])[:, None] * np.random.normal(0, 1, (pu, 1)))) for i in range(len(y_ind_obs)) ] x_obs = 0.5 * np.random.uniform(-1, 3, (n, p)) d = SepiaData(x_sim=x_sim, y_sim=y_sim, t_sim=t, y_ind_sim=y_ind_sim, x_obs=x_obs, y_obs=y_obs, y_ind_obs=y_ind_obs) model = SepiaModel(d)
def setup_multi_sim_and_obs_noD(m=100, n=10, nt_sim=20, nt_obs=15, noise_sd=0.1, nx=5, n_pc=10, seed=42., n_lik=0, n_mcmc=0): try: eng = matlab.engine.start_matlab() eng.cd(root_path) eng.addpath('matlab/', nargout=0) res = eng.setup_multi_sim_and_obs_noD(m, n, nt_sim, nt_obs, noise_sd, nx, n_pc, seed, n_lik, n_mcmc, nargout=1) eng.quit() except Exception as e: print(e) print( 'Matlab error; make sure matlab.engine installed, check Matlab code for errors.' ) y = np.array(res['y'], dtype=float) y_ind = np.array(res['y_ind'], dtype=float).squeeze() xt = np.array(res['xt'], dtype=float) y_obs = np.array(res['y_obs'], dtype=float) y_ind_obs = np.array(res['y_ind_obs'], dtype=float).squeeze() x_obs = np.array(res['x_obs'], dtype=float) data = SepiaData(x_sim=xt[:, 0][:, None], t_sim=xt[:, 1:], y_sim=y, y_ind_sim=y_ind, 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) print(data) model = SepiaModel(data) return model, res
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()
def setup_multi_sim_only(m=300, nt=20, nx=5, n_pc=10, seed=42., n_lik=0, n_mcmc=0, n_pred=0, fix_K=False, sens=0): try: eng = matlab.engine.start_matlab() eng.cd(root_path) eng.addpath('matlab/', nargout=0) res = eng.setup_multi_sim_only(m, nt, nx, n_pc, seed, n_lik, n_mcmc, n_pred, sens, nargout=1) eng.quit() except Exception as e: print(e) print( 'Matlab error; make sure matlab.engine installed, check Matlab code for errors.' ) y = np.array(res['y'], dtype=float) y_ind = np.array(res['y_ind'], dtype=float).squeeze() xt = np.array(res['xt'], dtype=float) data = SepiaData(x_sim=xt[:, 0][:, None], t_sim=xt[:, 1:], y_sim=y, y_ind_sim=y_ind) data.standardize_y() data.transform_xt() data.create_K_basis(n_pc) if fix_K: data.sim_data.K = np.array(res['K']).T print(data) model = SepiaModel(data) return model, res
def setup_neddermeyer(seed=42., n_mcmc=100, sens=1, n_burn=0, n_lev=0): try: eng = matlab.engine.start_matlab() eng.cd(root_path) eng.addpath('matlab/', nargout=0) #dataStruct = eng.neddeg(0, nargout=1) res = eng.setup_neddermeyer(seed, n_mcmc, sens, n_burn, n_lev) eng.quit() except Exception as e: print(e) print( 'Matlab error; make sure matlab.engine installed, check Matlab code for errors.' ) # get python model import pickle data_dict = pickle.load( open('../../examples/Neddermeyer/pkls/nedderDataDict.pkl', 'rb')) data = SepiaData(x_sim=data_dict['x_sim'],t_sim=data_dict['t_sim'],y_sim=data_dict['y_sim'],\ y_ind_sim=data_dict['y_ind_sim'],x_obs=data_dict['x_obs'],y_obs=data_dict['y_obs'],\ y_ind_obs=data_dict['y_ind_obs']) data.transform_xt() # set orig sim vals and Ksim data.sim_data.orig_y_mean = data_dict['sim_orig_y_mean'] data.sim_data.orig_y_sd = data_dict['sim_orig_y_sd'] data.sim_data.y_std = data_dict['sim_y_std'] data.sim_data.K = data_dict['Ksim'] # set orig obs vals and Kobs data.obs_data.orig_y_mean = data_dict['obs_orig_y_mean'] data.obs_data.orig_y_sd = data_dict['obs_orig_y_sd'] data.obs_data.y_std = data_dict['obs_orig_y_std'] data.obs_data.K = data_dict['Kobs'] # call create_D_basis data.create_D_basis(D_obs=data_dict['Dobs'], D_sim=data_dict['Dsim']) model = SepiaModel(data) return model, res
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 setup_model(data, Sigy=None, lamVzGroup=None): """ Sets up SepiaModel object based on SepiaData object. :param data: SepiaData object :param Sigy: observation covariance matrix (default is identity) :param lamVzGroup: groups for lamVz (otherwise single lamVz for all D basis functions) :return: instantiated SepiaModel object """ # Check if things transformed, do a default thing, but give warning if not done by user if data.sim_data.y_std is None: print('Warning: you did not standardize y!') print('Continuing with default standardization.') print( 'To customize, call data.standardize_y on your SepiaData object first, or do manual standardization.' ) data.standardize_y(center=True, scale='scalar') if data.sim_data.x_trans is None and data.sim_data.t_trans is None: print('Warning: you did not rescale x/t to unit cube!') print('Continuing with default transformation to unit cube.') print( 'To customize, call data.transform_xt on your SepiaData object first, or do manual transformation.' ) data.transform_xt() # If not scalar out, check that K/D are set appropriately if not data.scalar_out: if data.sim_data.K is None: print('Warning: you did not set up the K basis!') print('Continuing with default PCA basis.') print( 'To customize, call data.create_K_basis on your SepiaData object.' ) data.create_K_basis() else: if not data.sim_only: if data.ragged_obs: obs_K_shapes = np.array( [k.shape[0] for k in data.obs_data.K]) if not np.all(data.sim_data.K.shape[0] == obs_K_shapes): raise ValueError('Sim and obs K shapes not conformal.') else: if data.sim_data.K.shape[0] != data.obs_data.K.shape[0]: raise ValueError('Sim and obs K shapes not conformal.') if not data.sim_only and data.obs_data.D is None: print('Warning: you did not set up a D basis!') print( 'To use one, call data.create_D_basis on your SepiaData object.' ) # Set up model object to modify model = SepiaModel() model.data = data num = model.num num.scalar_out = data.scalar_out num.sim_only = data.sim_only # Local references to data for initialization of model sim_data = data.sim_data obs_data = data.obs_data # Infer p, q, n, m, pu, pv m, p = sim_data.x.shape if sim_data.t is not None: q = sim_data.t.shape[1] else: q = 0 ell_sim = sim_data.y.shape[1] if data.scalar_out: pu = 1 else: pu = sim_data.K.shape[0] num.m, num.p, num.q, num.pu = m, p, q, pu if not data.sim_only: if data.ragged_obs: ell_obs = [obs_data.y[i].shape for i in range(len(obs_data.y))] else: ell_obs = obs_data.y.shape[1] n = obs_data.x.shape[0] if data.scalar_out or obs_data.D is None: pv = 0 else: if data.ragged_obs: pv = obs_data.D[0].shape[0] else: pv = obs_data.D.shape[0] num.n, num.pv = n, pv else: num.n = 0 num.pv = 0 # Set up Sigy/Lamy if not data.sim_only: if Sigy is None: if data.ragged_obs: Sigy = [ np.atleast_2d(np.diag(np.ones(ell_obs[i]))) for i in range(len(ell_obs)) ] else: Sigy = np.diag(np.ones(ell_obs)) if data.scalar_out: Lamy = 1 / Sigy else: if data.ragged_obs: Lamy = [np.linalg.inv(Sigy[i]) for i in range(len(Sigy))] else: Lamy = np.linalg.inv(Sigy) # Set up GP inputs/DistCov objects if not data.sim_only: data.x = obs_data.x_trans else: data.x = np.array([], dtype=np.float).reshape((0, 1)) num.x0Dist = SepiaDistCov(data.x) if sim_data.t_trans is not None: data.zt = np.concatenate([sim_data.x_trans, sim_data.t_trans], axis=1) else: data.zt = sim_data.x_trans num.ztDist = SepiaDistCov(data.zt) if not data.sim_only: # Check for lamVzGroups, validate TODO validate if lamVzGroup is not None: lamVzGnum = np.unique(lamVzGroup).shape[0] else: lamVzGroup = [0] * pv lamVzGnum = 1 num.lamVzGroup = lamVzGroup num.lamVzGnum = lamVzGnum #assert (lamVzGnum == np.unique(lamVzGroup).shape[0]) # Transform obs data using D, Kobs -> v, u if not data.sim_only: if data.scalar_out: u = obs_data.y_std v = np.array([], dtype=float) else: DKridge = 1e-6 * np.diag(np.ones(pu + pv)) # (pu+pv, pu+pv) if data.ragged_obs: v = [] u = [] for i in range(len(data.obs_data.y)): if obs_data.D is None: DK = obs_data.K[i] else: DK = np.concatenate([obs_data.D[i], obs_data.K[i] ]) # (pu+pv, ell_obs) DKprod = np.linalg.multi_dot([DK, Lamy[i], DK.T]) # (pu+pv, pu+pv) y_tmp = obs_data.y_std[i][:, None] vu = np.dot(np.linalg.inv(DKprod + DKridge), np.linalg.multi_dot([DK, Lamy[i], y_tmp])) v.append(vu[:pv, :].T) u.append(vu[pv:, :].T) v = np.concatenate(v) u = np.concatenate(u) else: if obs_data.D is None: DK = obs_data.K else: DK = np.concatenate([obs_data.D, obs_data.K]) # (pu+pv, ell_obs) DKprod = np.linalg.multi_dot([DK, Lamy, DK.T]) # (pu+pv, pu+pv) vu = np.dot(np.linalg.inv(DKprod + DKridge), np.linalg.multi_dot([DK, Lamy, obs_data.y_std.T])) v = vu[:pv, :].T u = vu[pv:, :].T num.u = u.reshape((n * pu, 1), order='F').copy() num.v = v.reshape((n * pv, 1), order='F').copy() else: num.u = np.array([], dtype=np.float).reshape((0, 1)) num.v = np.array([], dtype=np.float).reshape((0, 1)) # Transform sim data using Ksim -> w if data.scalar_out: w = sim_data.y_std else: w = np.dot(np.linalg.pinv(sim_data.K).T, sim_data.y_std.T).T num.w = w.reshape((m * pu, 1), order='F').copy() if num.scalar_out: num.uw = np.concatenate((num.u, num.w), axis=0) else: num.vuw = np.concatenate((num.v, num.u, num.w), axis=0) num.vu = np.concatenate((num.v, num.u), axis=0) # Compute LamSim if data.scalar_out: LamSim = np.ones(1) else: LamSim = np.diag(np.dot(sim_data.K, sim_data.K.T)) num.LamSim = LamSim # Compute LamObs if not data.sim_only: if data.scalar_out: LamObs = Lamy * np.diag(np.ones(n)) rankLO = np.linalg.matrix_rank(LamObs) else: if obs_data.D is None: if data.ragged_obs: DK = [obs_data.K[i] for i in range(n)] else: DK = obs_data.K else: if data.ragged_obs: DK = [ np.concatenate([obs_data.D[i], obs_data.K[i]]) for i in range(n) ] else: DK = np.concatenate([obs_data.D, obs_data.K ]) # Note: our DK = DK' in matlab DKridge = 1e-6 * np.diag(np.ones(pu + pv)) if data.ragged_obs: DKprod = [ np.linalg.multi_dot([DK[i], Lamy[i], DK[i].T]) for i in range(n) ] else: DKprod = [ np.linalg.multi_dot([DK, Lamy, DK.T]) for i in range(n) ] LamObs = scipy.linalg.block_diag(*DKprod) rankLO = np.linalg.matrix_rank( LamObs, hermitian=True) # Same as value in matlab LamObs = LamObs + scipy.linalg.block_diag( *[DKridge for i in range(n)]) # Reorder indices: is currently v1 u1 v2 u2 ... want v1 v2 v3 ... u1 u2 ... inew = [] for kk in range(pv): inew += list(range(kk, n * (pu + pv), pu + pv)) for kk in range(pu): inew += list(range(pv + kk, n * (pu + pv), pu + pv)) inew = np.array(inew) LamObs = LamObs[inew[:, None], inew[None, :]] SigObs = np.linalg.inv(LamObs + 1e-8 * np.diag(np.ones(LamObs.shape[0]))) num.SigObs = SigObs else: rankLO = 0 # Compute prior correction for lamOs, lamWOs if not data.sim_only: if data.ragged_obs: tot_elements = np.sum(ell_obs) else: tot_elements = ell_obs * n lamOs_a_corr = 0.5 * (tot_elements - rankLO) lamOs_b_corr = 0 if not data.scalar_out: if obs_data.D is None: if data.ragged_obs: DK = [obs_data.K[i] for i in range(n)] else: DK = obs_data.K else: if data.ragged_obs: DK = [ np.concatenate([obs_data.D[i], obs_data.K[i]]) for i in range(n) ] else: DK = np.concatenate([obs_data.D, obs_data.K ]) # Note: our DK = DK' in matlab if data.ragged_obs: DKvu = [ np.dot( DK[i].T, np.concatenate([v[None, i, :], u[None, i, :]], axis=1).T) for i in range(n) ] for i in range(n): resid = obs_data.y_std[i] - DKvu[i].squeeze() lamOs_b_corr += 0.5 * np.sum( np.linalg.multi_dot([resid, Lamy[i], resid])) else: DKvu = np.dot(DK.T, np.concatenate([v, u], axis=1).T) for i in range(n): resid = obs_data.y_std[i, :] - DKvu[:, i] lamOs_b_corr += 0.5 * np.sum( np.linalg.multi_dot([resid, Lamy, resid])) lamWOs_a_corr = 0.5 * (ell_sim - pu) * m if data.scalar_out: lamWOs_b_corr = 0 else: y_sim_std_hat = np.dot(w, sim_data.K) lamWOs_b_corr = 0.5 * np.sum( np.square(sim_data.y_std - y_sim_std_hat)) # same as matlab # Set up params/priors if model.num.sim_only: model.set_params_sim_only(lamWOs_a_corr, lamWOs_b_corr) elif pv == 0: model.set_params_noD(lamOs_a_corr, lamOs_b_corr, lamWOs_a_corr, lamWOs_b_corr) else: model.set_params_full(lamOs_a_corr, lamOs_b_corr, lamWOs_a_corr, lamWOs_b_corr) return model
plt.xlabel('y index') plt.ylabel('y') plt.show() #%% data.transform_xt() data.standardize_y(scale='columnwise') data.create_K_basis(5) data.create_D_basis(type='linear') print(data) #%% model = SepiaModel(data) #%% cachefile_name='multivariate_example_with_prediction.pkl' import os.path import pickle use_save_file=False if use_save_file and os.path.isfile(cachefile_name): model=pickle.load(open(cachefile_name, "rb" )) else: model.tune_step_sizes(50, 20) model.do_mcmc(1000) if use_save_file:
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)
y_sim = matfile['y'].T y_ind_sim = matfile['y_ind'].squeeze() xt_sim = matfile['x'] y_obs = matfile['y_obs'] 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):
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
h_dense = data_dict['h_dense'] D_dense = np.zeros(shape=(h_dense.shape[0], pv)) for j in range(pv): D_obs[:, j] = norm.pdf(h_field, D_grid[j], D_width) 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
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)
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)
y_sim = matfile['y'].T y_ind_sim = matfile['y_ind'].squeeze() xt_sim = matfile['x'] y_obs = matfile['y_obs'] 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=2) data.create_D_basis(D_obs=matfile['Dobs'].T) print(data) model = SepiaModel(data) nsamp = int(matfile['nsamp']) nburn = int(matfile['nburn']) @timeit def run_mcmc(): model.do_mcmc(nburn + nsamp) print('Python mcmc time:') run_mcmc() # import cProfile # cProfile.run('run_mcmc()', 'mcmc.profile') # # import os