Пример #1
0
    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
Пример #2
0
    def logLik(self, cvar='all', cindex=None):
        """
        Compute model log lik with current values of variables.

        :param cvar: string -- name of variables changed since last call (controls recomputation of num components), or 'all'
        :param cindex: int -- index of flattened cvar that has changed since last call (or None)
        :return: scalar -- log lik value
        """
        L = compute_log_lik(self, cvar, cindex)
        return L
Пример #3
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