Ejemplo n.º 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))
Ejemplo n.º 2
0
    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)
Ejemplo n.º 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_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
model.do_mcmc(10, prog=False)  # The same burn-in process
Ejemplo n.º 5
0
#%%

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:
        pickle.dump(model, open( cachefile_name, "w+b" ))

#%%

# Extract MCMC samples into dictionary with parameter names
samples_dict = {p.name: p.mcmc_to_array() for p in model.params.mcmcList}

for i, k in enumerate(samples_dict.keys()):
    param_shape = samples_dict[k].shape[1]
    if param_shape >= 5:
        ncol = 5
        nrow = int(np.ceil(param_shape / ncol))
    else: