Exemple #1
0
    def calib(símismo, rep, quema, extraer):
        """
        Esta función sirve para llamar a las funcionalidades de calibración de PyMC.

        :param rep: El número de repeticiones para la calibración.
        :type rep: int

        :param quema: El número de repeticiones iniciales a cortar de los resultados. Esto evita que los resultados
        estén muy influenciados por los valores iniciales (y posiblemente erróneos) que toman los parámetros al
        principio de la calibración.
        :type quema: int

        :param extraer: Cada cuántas repeticiones hay que guardar para los resultados. Por ejemplo, con `extraer`=10,
        cada 10 repeticiones se guardará, así que, con `rep`=10000, `quema`=100 y `extraer`=10, quedaremos con trazas
        de (10000 - 100) / 10 = 990 datos para aproximar la destribución de cada parámetro.

        """

        símismo.n_iter += rep

        if not usar_pymc3:
            # Utilizar el algoritmo Metrópolis Adaptivo para la calibración. Sería probablemente mejor utilizar NUTS, pero
            # para eso tendría que implementar pymc3 aquí y de verdad no quiero.
            if símismo.método.lower() == 'metrópolis adaptivo':
                símismo.MCMC.use_step_method(pm2.AdaptiveMetropolis,
                                             símismo.MCMC.stochastics,
                                             delay=200,
                                             interval=200,
                                             greedy=False,
                                             shrink_if_necessary=True,
                                             verbose=4)
            elif símismo.método.lower() == 'metrópolis':
                pass
            else:
                raise ValueError

            # Llamar la función "sample" (muestrear) del objeto MCMC de PyMC
            símismo.MCMC.sample(iter=rep,
                                burn=quema,
                                thin=extraer,
                                verbose=1,
                                tune_interval=10)

        else:
            if símismo.método.lower() == 'mcs':
                n_trazas = 1
                dir_temp = mkdtemp(prefix='TKN_MCS')
                traza = mcs.sample_smc(n_steps=rep,
                                       n_chains=n_trazas,
                                       progressbar=False,
                                       homepath=dir_temp,
                                       stage=0,
                                       random_seed=42)
Exemple #2
0
    def check_trace(self, step_method):
        """Tests whether the trace for step methods is exactly the same as on master.

        Code changes that effect how random numbers are drawn may change this, and require
        `master_samples` to be updated, but such changes should be noted and justified in the
        commit.

        This method may also be used to benchmark step methods across commits, by running, for
        example

        ```
        BENCHMARK=100000 ./scripts/test.sh -s pymc3/tests/test_step.py:TestStepMethods
        ```

        on multiple commits.
        """
        n_steps = 100
        with Model() as model:
            x = Normal('x', mu=0, sd=1)
            if step_method.__name__ == 'SMC':
                trace = smc.sample_smc(n_steps=n_steps,
                                       n_chains=2,
                                       start=[{
                                           'x': 1.
                                       }, {
                                           'x': -1.
                                       }],
                                       random_seed=1,
                                       n_jobs=1,
                                       progressbar=False,
                                       homepath=self.temp_dir)

            elif step_method.__name__ == 'NUTS':
                step = step_method(scaling=model.test_point)
                trace = sample(0,
                               tune=n_steps,
                               discard_tuned_samples=False,
                               step=step,
                               random_seed=1,
                               chains=1)
            else:
                trace = sample(0,
                               tune=n_steps,
                               discard_tuned_samples=False,
                               step=step_method(),
                               random_seed=1,
                               chains=1)
        assert_array_almost_equal(trace.get_values('x'),
                                  self.master_samples[step_method],
                                  decimal=select_by_precision(float64=6,
                                                              float32=4))
Exemple #3
0
    def test_sample_n_core(self, n_jobs, stage):

        mtrace = smc.sample_smc(
            n_steps=self.n_steps,
            step=self.step,
            stage=stage,
            n_jobs=n_jobs,
            progressbar=True,
            homepath=self.test_folder,
            model=self.ATMIP_test,
            rm_flag=True)

        x = mtrace.get_values('X')
        mu1d = np.abs(x).mean(axis=0)
        np.testing.assert_allclose(self.muref, mu1d, rtol=0., atol=0.03)
Exemple #4
0
    def test_sample_n_core(self, n_jobs, stage):

        mtrace = smc.sample_smc(samples=self.samples,
                                n_chains=self.n_chains,
                                stage=stage,
                                n_jobs=n_jobs,
                                progressbar=True,
                                homepath=self.test_folder,
                                model=self.ATMIP_test,
                                rm_flag=True)

        x = mtrace.get_values('X')
        mu1d = np.abs(x).mean(axis=0)
        np.testing.assert_allclose(self.muref, mu1d, rtol=0., atol=0.03)
        # Scenario IV Ching, J. & Chen, Y. 2007
        assert np.round(np.log(self.ATMIP_test.marginal_likelihood)) == -12.0
Exemple #5
0
    def test_sample_n_core(self, n_jobs, stage):

        mtrace = smc.sample_smc(samples=self.samples,
                                chains=self.n_chains,
                                stage=stage,
                                cores=n_jobs,
                                progressbar=True,
                                homepath=self.test_folder,
                                model=self.ATMIP_test,
                                rm_flag=True)

        x = mtrace.get_values('X')
        mu1d = np.abs(x).mean(axis=0)
        np.testing.assert_allclose(self.muref, mu1d, rtol=0., atol=0.03)
        # Scenario IV Ching, J. & Chen, Y. 2007
        assert np.round(np.log(self.ATMIP_test.marginal_likelihood)) == -12.0
Exemple #6
0
    def check_trace(self, step_method):
        """Tests whether the trace for step methods is exactly the same as on master.

        Code changes that effect how random numbers are drawn may change this, and require
        `master_samples` to be updated, but such changes should be noted and justified in the
        commit.

        This method may also be used to benchmark step methods across commits, by running, for
        example

        ```
        BENCHMARK=100000 ./scripts/test.sh -s pymc3/tests/test_step.py:TestStepMethods
        ```

        on multiple commits.
        """
        n_steps = 100
        with Model() as model:
            x = Normal('x', mu=0, sd=1)
            if step_method.__name__ == 'SMC':
                trace = smc.sample_smc(n_steps=n_steps,
                                       n_chains=2,
                                       start=[{'x':1.}, {'x':-1.}],
                                       random_seed=1,
                                       n_jobs=1, progressbar=False,
                                       homepath=self.temp_dir)

            elif step_method.__name__ == 'NUTS':
                step = step_method(scaling=model.test_point)
                trace = sample(0, tune=n_steps,
                               discard_tuned_samples=False,
                               step=step, random_seed=1, chains=1)
            else:
                trace = sample(0, tune=n_steps,
                               discard_tuned_samples=False,
                               step=step_method(), random_seed=1, chains=1)
        assert_array_almost_equal(
            trace.get_values('x'),
            self.master_samples[step_method],
            decimal=select_by_precision(float64=6, float32=4))
Exemple #7
0
    niter = 10000

    start = pm.find_MAP(model=basic_model)

    print("Maximum a-posteriori estimate:")
    print(start)

    n_chains = 500
    n_steps = 50
    tune_interval = 10
    n_jobs = 1
    trace = smc.sample_smc(
        n_steps=n_steps,
        n_chains=n_chains,
        tune_interval=tune_interval,
        n_jobs=n_jobs,
        #start=start,
        progressbar=True,
        stage=0,
        homepath=test_folder,
        model=basic_model)

    #t = trace[niter//2:] # discard 50% of values
    #print t
    #x0_ = trace.get_values('x0',burn=niter//2, combine=True)

    #step = pm.NUTS(scaling=start)
    #trace = pm.sample(niter, start = start, step = step)

    #step = pm.NUTS()
    #trace = pm.sample(niter, step = step, tune=2000)
    # transform need to be None for SMC to work (transformed doesnt have random method)
    h2 = pm.Uniform('h2', transform=None)
    sigma2 = pm.HalfCauchy('sigma2', 5, transform=None)
    #beta_0 = pm.Uniform('beta_0', lower=-1000, upper=1000)   # a replacement for improper prior
    w = pm.Normal('w', mu=0, sd=100, shape=M)
    z = pm.Normal('z', mu=0, sd=(h2 * sigma2)**0.5, shape=N)
    g = T.dot(L, z)
    y = pm.Normal('y',
                  mu=g + T.dot(X, w),
                  sd=((1 - h2) * sigma2)**0.5,
                  observed=Pheno)

mtrace = smc.sample_smc(n_steps=n_steps,
                        n_chains=n_chains,
                        tune_interval=tune_interval,
                        n_jobs=n_jobs,
                        progressbar=False,
                        stage=0,
                        homepath=test_folder,
                        model=mixedEffect2)
#%%
pm.traceplot(mtrace, lines={
    'w': w0,
    'z': z0
})
#%% plot advi and NUTS (copy from pymc3 example)
burnin = 1000
from scipy import stats
import seaborn as sns

gbij = approx.bij
means = gbij.rmap(approx.mean.eval())