def _build_prior(self, name, X, reparameterize=True, jitter=JITTER_DEFAULT, **kwargs): mu = self.mean_func(X) cov = stabilize(self.cov_func(X), jitter) if reparameterize: size = infer_size(X, kwargs.pop("size", None)) v = pm.StudentT(name + "_rotated_", mu=0.0, sigma=1.0, nu=self.nu, size=size, **kwargs) f = pm.Deterministic(name, mu + cholesky(cov).dot(v)) else: f = pm.MvStudentT(name, nu=self.nu, mu=mu, cov=cov, **kwargs) return f
def time_drug_evaluation(self): # fmt: off drug = np.array([ 101, 100, 102, 104, 102, 97, 105, 105, 98, 101, 100, 123, 105, 103, 100, 95, 102, 106, 109, 102, 82, 102, 100, 102, 102, 101, 102, 102, 103, 103, 97, 97, 103, 101, 97, 104, 96, 103, 124, 101, 101, 100, 101, 101, 104, 100, 101 ]) placebo = np.array([ 99, 101, 100, 101, 102, 100, 97, 101, 104, 101, 102, 102, 100, 105, 88, 101, 100, 104, 100, 100, 100, 101, 102, 103, 97, 101, 101, 100, 101, 99, 101, 100, 100, 101, 100, 99, 101, 100, 102, 99, 100, 99 ]) # fmt: on y = pd.DataFrame({ "value": np.r_[drug, placebo], "group": np.r_[["drug"] * len(drug), ["placebo"] * len(placebo)], }) y_mean = y.value.mean() y_std = y.value.std() * 2 sigma_low = 1 sigma_high = 10 with pm.Model(): group1_mean = pm.Normal("group1_mean", y_mean, sd=y_std) group2_mean = pm.Normal("group2_mean", y_mean, sd=y_std) group1_std = pm.Uniform("group1_std", lower=sigma_low, upper=sigma_high) group2_std = pm.Uniform("group2_std", lower=sigma_low, upper=sigma_high) lambda_1 = group1_std**-2 lambda_2 = group2_std**-2 nu = pm.Exponential("ν_minus_one", 1 / 29.0) + 1 pm.StudentT("drug", nu=nu, mu=group1_mean, lam=lambda_1, observed=drug) pm.StudentT("placebo", nu=nu, mu=group2_mean, lam=lambda_2, observed=placebo) diff_of_means = pm.Deterministic("difference of means", group1_mean - group2_mean) pm.Deterministic("difference of stds", group1_std - group2_std) pm.Deterministic( "effect size", diff_of_means / np.sqrt( (group1_std**2 + group2_std**2) / 2)) pm.sample(draws=20000, cores=4, chains=4, progressbar=False, compute_convergence_checks=False)
def make_model(cls): with pm.Model() as model: a = pm.StudentT("a", nu=4, mu=0, sigma=1) return model