Ejemplo n.º 1
0
    def test_named_models_are_unsupported(self):
        def normal_sim(a, b):
            return np.random.normal(a, b, 1000)

        with pm.Model(name="NamedModel"):
            a = pm.Normal("a", mu=0, sigma=1)
            b = pm.HalfNormal("b", sigma=1)
            c = pm.Potential("c", pm.math.switch(a > 0, 0, -np.inf))
            s = pm.Simulator(
                "s", normal_sim, params=(a, b), sum_stat="sort", epsilon=1, observed=self.data
            )
            with pytest.raises(NotImplementedError, match="named models"):
                pm.sample_smc(draws=10, kernel="ABC")
Ejemplo n.º 2
0
def Two_Parameter_Model(Path, N_people, SEED, Max_pump=128):
    '''
    Path: Path where your record file exist.
    N_people: The Number of participants you have.
    SEED: Set radnom seed of MCMC sampling.
    Max_pump: Maxiumum trial of your BART setting. My setting is 128.
    '''
    traces_gamma = []
    traces_beta = []
    for part in range(1, N_people + 1):
        with pm.Model() as total_model:
            start_time = time.time()
            p = 0.15
            obs = BernData(part, Max_pump, Path)
            #Pth participant
            gamma_plus = pm.Uniform("gamma_plus", 0, 10)
            beta = pm.Uniform("beta", 0, 10)
            omega_k = -gamma_plus / np.log(1 - p)
            for i in range(len(obs)):
                for l in range(Max_pump):
                    theta_lk = 1 / (1 + np.exp(beta * (l - omega_k)))
                    prob = pm.Bernoulli("prob_{}_{}".format(i, l),
                                        p=theta_lk,
                                        observed=obs[i][l])
            _trace = pm.sample_smc(1000, cores=6, random_seed=SEED)
            print("Sampling end:", part,
                  "--- %s seconds ---" % (time.time() - start_time))
        traces_gamma.append(_trace["gamma_plus"])
        traces_beta.append(_trace["beta"])
    return traces_gamma, traces_beta
Ejemplo n.º 3
0
    def test_sample(self):
        with self.SMC_test:
            mtrace = pm.sample_smc(draws=self.samples)

        x = mtrace["X"]
        mu1d = np.abs(x).mean(axis=0)
        np.testing.assert_allclose(self.muref, mu1d, rtol=0.0, atol=0.03)
Ejemplo n.º 4
0
 def sample_model(self, method = 'log-model', field = 'deaths', **kwargs):
         models = self.phenom_model(method, field)
         self.traces = {} 
         self.traces[method] = {}
         for i, country in enumerate(self.countries):
             self.traces[method][country] = pm.sample_smc(model = models[method][country], **kwargs)
             #marginal_likelihood = model.marginal_log_likelihood
         return models, self.traces #, marginal_likelihood
def run_bayesian_inference_gaussian_error_model(loglike,
                                                variables,
                                                ndraws,
                                                nburn,
                                                njobs,
                                                algorithm='nuts',
                                                get_map=False,
                                                print_summary=False):
    # create our Op
    if algorithm != 'nuts':
        logl = LogLike(loglike)
    else:
        logl = LogLikeWithGrad(loglike)

    # use PyMC3 to sampler from log-likelihood
    with pm.Model():
        # must be defined inside with pm.Model() block
        pymc_variables, pymc_var_names = get_pymc_variables(
            variables.all_variables())

        # convert m and c to a tensor vector
        theta = tt.as_tensor_variable(pymc_variables)

        # use a DensityDist (use a lamdba function to "call" the Op)
        pm.DensityDist('likelihood', lambda v: logl(v), observed={'v': theta})

        if get_map:
            map_sample_dict = pm.find_MAP()

        if algorithm == 'smc':
            assert njobs == 1  # njobs is always 1 when using smc
            trace = pm.sample_smc(ndraws)
        else:
            if algorithm == 'metropolis':
                step = pm.Metropolis(pymc_variables)
            elif algorithm == 'nuts':
                step = pm.NUTS(pymc_variables)

            trace = pm.sample(ndraws,
                              tune=nburn,
                              discard_tuned_samples=True,
                              start=None,
                              cores=njobs,
                              step=step)

    if print_summary:
        print(pm.summary(trace))

    samples, effective_sample_size = extract_mcmc_chain_from_pymc3_trace(
        trace, pymc_var_names, ndraws, nburn, njobs)

    if get_map:
        map_sample = extract_map_sample_from_pymc3_dict(
            map_sample_dict, pymc_var_names)
    else:
        map_samples = None

    return samples, effective_sample_size, map_sample
Ejemplo n.º 6
0
    def test_one_gaussian(self):
        with self.SMABC_test:
            trace = pm.sample_smc(draws=1000, kernel="ABC")

        np.testing.assert_almost_equal(self.data.mean(),
                                       trace["a"].mean(),
                                       decimal=2)
        np.testing.assert_almost_equal(self.data.std(),
                                       trace["b"].mean(),
                                       decimal=1)
Ejemplo n.º 7
0
 def test_start(self):
     with pm.Model() as model:
         a = pm.Poisson("a", 5)
         b = pm.HalfNormal("b", 10)
         y = pm.Normal("y", a, b, observed=[1, 2, 3, 4])
         start = {
             "a": np.random.poisson(5, size=500),
             "b_log__": np.abs(np.random.normal(0, 10, size=500)),
         }
         trace = pm.sample_smc(500, start=start)
Ejemplo n.º 8
0
    def test_ml(self):
        data = np.repeat([1, 0], [50, 50])
        marginals = []
        a_prior_0, b_prior_0 = 1.0, 1.0
        a_prior_1, b_prior_1 = 20.0, 20.0

        for alpha, beta in ((a_prior_0, b_prior_0), (a_prior_1, b_prior_1)):
            with pm.Model() as model:
                a = pm.Beta("a", alpha, beta)
                y = pm.Bernoulli("y", a, observed=data)
                trace = pm.sample_smc(2000)
                marginals.append(model.marginal_log_likelihood)
        # compare to the analytical result
        assert abs(np.exp(marginals[1] - marginals[0]) - 4.0) <= 1
Ejemplo n.º 9
0
    def test_one_gaussian(self):
        with self.SMABC_test:
            trace = pm.sample_smc(draws=2000,
                                  kernel="ABC",
                                  epsilon=0.1,
                                  n_steps=25)

        np.testing.assert_almost_equal(
            [self.data.mean() * 10, self.data.std()],
            [trace["a"].mean() * 10, trace["b"].mean()],
            decimal=1)
        np.testing.assert_almost_equal(self.data.std(),
                                       trace["b"].mean(),
                                       decimal=1)
Ejemplo n.º 10
0
    def test_sim_data_ppc(self):
        with self.SMABC_test:
            trace, sim_data = pm.sample_smc(draws=1000, kernel="ABC", chains=2, save_sim_data=True)
            pr_p = pm.sample_prior_predictive(1000)
            po_p = pm.sample_posterior_predictive(trace, 1000)

        assert sim_data["s"].shape == (2, 1000, 1000)
        np.testing.assert_almost_equal(self.data.mean(), sim_data["s"].mean(), decimal=2)
        np.testing.assert_almost_equal(self.data.std(), sim_data["s"].std(), decimal=1)
        assert pr_p["s"].shape == (1000, 1000)
        np.testing.assert_almost_equal(0, pr_p["s"].mean(), decimal=1)
        np.testing.assert_almost_equal(1.4, pr_p["s"].std(), decimal=1)
        assert po_p["s"].shape == (1000, 1000)
        np.testing.assert_almost_equal(0, po_p["s"].mean(), decimal=2)
        np.testing.assert_almost_equal(1, po_p["s"].std(), decimal=1)
Ejemplo n.º 11
0
 def test_discrete_continuous(self):
     with pm.Model() as model:
         a = pm.Poisson("a", 5)
         b = pm.HalfNormal("b", 10)
         y = pm.Normal("y", a, b, observed=[1, 2, 3, 4])
         trace = pm.sample_smc()
Ejemplo n.º 12
0
    f = pm.Bound(pm.Beta, lower=0., upper=1.0)('f', alpha=1.1, beta=1.1)

    sigma = pm.HalfNormal('sigma', sd=1)

    forward = th_forward_model(tauD, tauF, U, f)

    cov = np.eye(2) * sigma**2

    Y_obs = pm.MvNormal('Y_obs', mu=forward, cov=cov, observed=Y_sim)

    startsmc = {
        v.name: np.random.uniform(1e-3, 2, size=draws)
        for v in TM_model.free_RVs
    }

    trace_TM = pm.sample_smc(draws, start=startsmc)

# %%
pm.plot_posterior(trace_TM, kind='hist', bins=30, color='seagreen')

# %%

# %% FROM SCRATCH
###############################################################################
###############################################################################
# %%
## %% TM-GLM inference
#def log_prior_theta(gamma1,gamma2,beta1,beta2):
#    D, F = np.random.gamma(gamma1, gamma1)
#    U, f = np.ranodm.beta(1.01, 1.01, 2)
#    theta = D,F,U,f
Ejemplo n.º 13
0
 def test_potential(self):
     with self.SMABC_potential:
         trace = pm.sample_smc(draws=1000, kernel="ABC")
         assert np.all(trace["a"] >= 0)
Ejemplo n.º 14
0
 def test_custom_dist_sum(self):
     with self.SMABC_test2:
         trace = pm.sample_smc(draws=1000, kernel="ABC")
Ejemplo n.º 15
0
            d_I,
            epsilon_I_deterministic,
            rho_deterministic,
            omega,
            sigma_deterministic,
        ),
    )

    likelihood_model = pm.Normal("likelihood_model",
                                 mu=fitting_model,
                                 sigma=standard_deviation,
                                 observed=observations_to_fit)

    seirdpq_trace_calibration = pm.sample_smc(draws=draws,
                                              n_steps=25,
                                              parallel=True,
                                              cores=int(os.cpu_count()),
                                              progressbar=True,
                                              random_seed=seed)

duration = time.time() - start_time

print(f"-- Monte Carlo simulations done in {duration / 60:.3f} minutes")

# %%
print("-- Arviz post-processing:")
import warnings

warnings.filterwarnings("ignore")

start_time = time.time()
plot_step = 1
Ejemplo n.º 16
0
def run_bayesian_inference_gaussian_error_model(loglike,
                                                variables,
                                                ndraws,
                                                nburn,
                                                njobs,
                                                algorithm='nuts',
                                                get_map=False,
                                                print_summary=False,
                                                loglike_grad=None,
                                                seed=None):
    r"""
    Draw samples from the posterior distribution using Markov Chain Monte 
    Carlo for data that satisfies

    .. math:: y=f(z)+\epsilon

    where :math:`y` is a vector of observations, :math:`z` are the 
    parameters of a function which are to be inferred, and :math:`\epsilon`
    is Gaussian noise.

    Parameters
    ----------
    loglike : pyapprox_dev.bayesian_inference.markov_chain_monte_carlo.GaussianLogLike
        A log-likelihood function associated with a Gaussian error model

    variables : pya.IndependentMultivariateRandomVariable
        Object containing information of the joint density of the inputs z.
        This is used to generate random samples from this join density

    ndraws : integer
        The number of posterior samples

    nburn : integer
        The number of samples to discard during initialization

    njobs : integer
        The number of prallel chains

    algorithm : string
        The MCMC algorithm should be one of

        - 'nuts'
        - 'metropolis'
        - 'smc'

    get_map : boolean
        If true return the MAP
    
    print_summary : boolean
        If true print summary statistics about the posterior samples

    loglike_grad : callable
        Function with signature
      
       ``loglikegrad(z) -> np.ndarray (nvars)``

        where ``z`` is a 2D np.ndarray with shape (nvars,nsamples

    random_seed : int or list of ints
        A list is accepted if ``cores`` is greater than one. PyMC3 does not 
        produce consistent results by setting numpy.random.seed instead
        seed must be passed in
    """

    # create our Op
    if algorithm != 'nuts':
        logl = LogLike(loglike)
    else:
        logl = LogLikeWithGrad(loglike, loglike_grad)

    # use PyMC3 to sampler from log-likelihood
    with pm.Model():
        # must be defined inside with pm.Model() block
        pymc_variables, pymc_var_names = get_pymc_variables(
            variables.all_variables())

        # convert m and c to a tensor vector
        theta = tt.as_tensor_variable(pymc_variables)

        # use a DensityDist (use a lamdba function to "call" the Op)
        pm.DensityDist('likelihood', lambda v: logl(v), observed={'v': theta})

        if get_map:
            map_sample_dict = pm.find_MAP()

        if algorithm == 'smc':
            assert njobs == 1  # njobs is always 1 when using smc
            trace = pm.sample_smc(ndraws)
        else:
            if algorithm == 'metropolis':
                step = pm.Metropolis(pymc_variables)
            elif algorithm == 'nuts':
                step = pm.NUTS(pymc_variables)

            trace = pm.sample(ndraws,
                              tune=nburn,
                              discard_tuned_samples=True,
                              start=None,
                              cores=njobs,
                              step=step,
                              compute_convergence_checks=False,
                              random_seed=seed)
            # compute_convergence_checks=False avoids bugs in theano

        if print_summary:
            try:
                print(pm.summary(trace))
            except:
                print('could not print summary. likely issue with theano')

        samples, effective_sample_size = extract_mcmc_chain_from_pymc3_trace(
            trace, pymc_var_names, ndraws, nburn, njobs)

        if get_map:
            map_sample = extract_map_sample_from_pymc3_dict(
                map_sample_dict, pymc_var_names)
        else:
            map_samples = None

    return samples, effective_sample_size, map_sample
Ejemplo n.º 17
0
 def test_name_is_string_type(self):
     with self.SMABC_potential:
         assert not self.SMABC_potential.name
         trace = pm.sample_smc(draws=10, kernel="ABC")
         assert isinstance(trace._straces[0].name, str)