def test_mcmc_random_seed(stan_model, two_group_sample_data): y1, y2 = two_group_sample_data mcmc1 = get_mcmc(stan_model, y1, y2, rand_seed=1).extract() mcmc2 = get_mcmc(stan_model, y1, y2, rand_seed=1).extract() np.testing.assert_equal(mcmc1, mcmc2)
def test_mcmc_aggregation(stan_model, two_group_sample_data): y1, y2 = two_group_sample_data w1 = w2 = 6 * [2] mcmc_short = get_mcmc(stan_model, y1, y2, w1=w1, w2=w2, rand_seed=1) mcmc_long = get_mcmc(stan_model, 2 * y1, 2 * y2, rand_seed=1) for parameter in mcmc_short.extract().keys(): assert np.isclose(mcmc_short.extract()[parameter], mcmc_long.extract()[parameter]).all()
def fit(self, y1, y2, w1=None, w2=None): self.mcmc_ = get_mcmc(self.model, y1, y2, w1, w2, **self.kwargs) self.data_ = az.from_pystan( posterior=self.mcmc_, posterior_predictive=['y1_pred', 'y2_pred'], observed_data=['y1', 'y2'], log_likelihood='log_lik', coords={ 'group_mu': ['Group 1', 'Group 2'], 'group_sigma': ['Group 1', 'Group 2'] }, dims={ 'mu': ['group_mu'], 'sigma': ['group_sigma'] })
def test_mcmc(stan_model, two_group_sample_data): y1, y2 = two_group_sample_data mcmc = get_mcmc(stan_model, y1, y2, rand_seed=1) row_ind_1 = list(mcmc.summary()['summary_rownames']).index('mu[1]') row_ind_2 = list(mcmc.summary()['summary_rownames']).index('mu[2]') col_ind_m = list(mcmc.summary()['summary_colnames']).index('mean') col_ind_rh = list(mcmc.summary()['summary_colnames']).index('Rhat') assert np.isclose(mcmc.summary()['summary'][row_ind_1, col_ind_m], np.mean(y1), atol=0.1) assert np.isclose(mcmc.summary()['summary'][row_ind_2, col_ind_m], np.mean(y2), atol=0.1) assert np.isclose(mcmc.summary()['summary'][:, col_ind_rh], 1.0, atol=0.1).all()
import numpy as np from bab.mcmc import get_mcmc, get_stan_model from bab.make_data import make_data from bab.power import get_power from bab.model import BayesAB y1, y2 = make_data(0, 1, 1, 2, 10, percent_outliers=0, sd_outlier_factor=2.0, rand_seed=1) stan_model = get_stan_model() mcmc = get_mcmc(stan_model, y1, y2) get_power(stan_model, y1, y2, (-1., 1.), (0., 1.), 1., 1., n_sim=10) model = BayesAB() model.fit(2 * np.random.randn(10) + 3, np.random.randn(10))
def get_power(stan_model, y1, y2, rope_m, rope_sd, max_hdi_width_m, max_hdi_width_sd, cred_mass=0.95, n_sim=200, precision=2, rand_seed=None): """ :param stan_model: StanModel instance :param y1: iterable, prospective samples of group one :param y2: iterable, prospective samples of group two :param rope_m: iterable, of length two, such as (-1, 1), specifying the limit of the ROPE on the difference of means. :param rope_sd: iterable, of length two, such as (-1, 1), specifying the limit of the ROPE on the difference of standard deviations. :param max_hdi_width_m: float, maximum desired width of the 95% HDI on the difference of means. :param max_hdi_width_sd: float, maximum desired width of the 95% HDI on the difference of standard deviations. :param cred_mass: (optional) float, fraction of credible mass. :param n_sim: (optional) int, number of simulated experiments used to estimate the power. :param precision: (optional) int, number of decimals to round the power statistics to. :param rand_seed: int (optional), random seed :return: power, dict """ if rand_seed is not None: np.random.seed(int(rand_seed)) sample_size = len(y1) mcmc_chain = get_mcmc(stan_model, y1, y2, rand_seed=rand_seed).extract() step_idx = _get_step_indices(mcmc_chain, n_sim) goal_tally = { 'HDIm > ROPE': 0, 'HDIm < ROPE': 0, 'HDIm in ROPE': 0, 'HDIm width < max': 0, 'HDIsd > ROPE': 0, 'HDIsd < ROPE': 0, 'HDIsd in ROPE': 0, 'HDIsd width < max': 0 } power = { 'HDIm > ROPE': [0, 0, 0], 'HDIm < ROPE': [0, 0, 0], 'HDIm in ROPE': [0, 0, 0], 'HDIm width < max': [0, 0, 0], 'HDIsd > ROPE': [0, 0, 0], 'HDIsd < ROPE': [0, 0, 0], 'HDIsd in ROPE': [0, 0, 0], 'HDIsd width < max': [0, 0, 0] } n_sim = 0 for step in step_idx: n_sim += 1 y1_sim, y2_sim = _generate_simulated_data(mcmc_chain, sample_size, step) sim_chain = get_mcmc(stan_model, y1_sim, y2_sim, rand_seed=rand_seed).extract() goal_tally = _update_goal_tally(goal_tally, max_hdi_width_m, max_hdi_width_sd, rope_m, rope_sd, sim_chain) _assess_and_tally_goals(cred_mass, goal_tally, n_sim, power) _log_progress(n_sim, power, step_idx) for k, v in power.items(): power[k] = [round(e, precision) for e in v] return power