def test_more_than_one_glm_is_ok(self): with Model(): GLM.from_formula('y ~ x', self.data_logistic, family=families.Binomial(link=families.logit), name='glm1') GLM.from_formula('y ~ x', self.data_logistic, family=families.Binomial(link=families.logit), name='glm2')
def test_glm_offset(self): offset = 1.0 with Model() as model: GLM.from_formula("y ~ x", self.data_linear, offset=offset) step = Slice(model.vars) trace = sample(500, step=step, tune=0, progressbar=False, random_seed=self.random_seed) assert round(abs(np.mean(trace["Intercept"]) - self.intercept + offset), 1) == 0
def test_glm_link_func2(self): with Model() as model: GLM.from_formula('y ~ x', self.data_logistic2, family=families.Binomial(priors={'n': self.data_logistic2['n']})) trace = sample(1000, progressbar=False, random_seed=self.random_seed) assert round(abs(np.mean(trace['Intercept'])-self.intercept), 1) == 0 assert round(abs(np.mean(trace['x'])-self.slope), 1) == 0
def test_glm_offset(self): offset = 1. with Model() as model: GLM.from_formula('y ~ x', self.data_linear, offset=offset) step = Slice(model.vars) trace = sample(500, step=step, tune=0, progressbar=False, random_seed=self.random_seed) assert round(abs(np.mean(trace['Intercept'])-self.intercept+offset), 1) == 0
def test_glm(self): with Model() as model: GLM.from_formula("y ~ x", self.data_linear) step = Slice(model.vars) trace = sample(500, step=step, tune=0, progressbar=False, random_seed=self.random_seed) assert round(abs(np.mean(trace["Intercept"]) - self.intercept), 1) == 0 assert round(abs(np.mean(trace["x"]) - self.slope), 1) == 0 assert round(abs(np.mean(trace["sd"]) - self.sd), 1) == 0
def test_glm_link_func2(self): with Model() as model: GLM.from_formula('y ~ x', self.data_logistic2, family=families.Binomial(priors={'n': self.data_logistic2['n']})) trace = sample(1000, progressbar=False, init='adapt_diag', random_seed=self.random_seed) assert round(abs(np.mean(trace['Intercept'])-self.intercept), 1) == 0 assert round(abs(np.mean(trace['x'])-self.slope), 1) == 0
def test_glm_link_func(self): with Model() as model: GLM.from_formula('y ~ x', self.data_logistic, family=families.Binomial(link=families.logit)) step = Slice(model.vars) trace = sample(1000, step, progressbar=False, random_seed=self.random_seed) assert round(abs(np.mean(trace['Intercept'])-self.intercept), 1) == 0 assert round(abs(np.mean(trace['x'])-self.slope), 1) == 0
def test_glm(self): with Model() as model: GLM.from_formula('y ~ x', self.data_linear) step = Slice(model.vars) trace = sample(500, step, progressbar=False, random_seed=self.random_seed) assert round(abs(np.mean(trace['Intercept'])-self.intercept), 1) == 0 assert round(abs(np.mean(trace['x'])-self.slope), 1) == 0 assert round(abs(np.mean(trace['sd'])-self.sd), 1) == 0
def test_glm_link_func2(self): with Model() as model: GLM.from_formula( "y ~ x", self.data_logistic2, family=families.Binomial(priors={"n": self.data_logistic2["n"]}), ) trace = sample(1000, progressbar=False, init="adapt_diag", random_seed=self.random_seed) assert round(abs(np.mean(trace["Intercept"]) - self.intercept), 1) == 0 assert round(abs(np.mean(trace["x"]) - self.slope), 1) == 0
def test_boolean_y(self): model = GLM.from_formula( "y ~ x", pd.DataFrame({"x": self.data_logistic["x"], "y": self.data_logistic["y"]}) ) model_bool = GLM.from_formula( "y ~ x", pd.DataFrame( {"x": self.data_logistic["x"], "y": [bool(i) for i in self.data_logistic["y"]]} ), ) assert_equal(model.y.observations, model_bool.y.observations)
def test_boolean_y(self): model = GLM.from_formula('y ~ x', pd.DataFrame( {'x': self.data_logistic['x'], 'y': self.data_logistic['y']} ) ) model_bool = GLM.from_formula('y ~ x', pd.DataFrame( {'x': self.data_logistic['x'], 'y': [bool(i) for i in self.data_logistic['y']]} ) ) assert_equal(model.y.observations, model_bool.y.observations)
def test_glm(self): with Model() as model: GLM.from_formula('y ~ x', self.data_linear) step = Slice(model.vars) trace = sample(500, step, progressbar=False, random_seed=self.random_seed) self.assertAlmostEqual(np.mean(trace['Intercept']), self.intercept, 1) self.assertAlmostEqual(np.mean(trace['x']), self.slope, 1) self.assertAlmostEqual(np.mean(trace['sd']), self.sd, 1)
def test_glm_formula_from_calling_scope(self): """Formula can extract variables from the calling scope.""" z = pd.Series([10, 20, 30]) df = pd.DataFrame({"y": [0, 1, 0], "x": [1.0, 2.0, 3.0]}) GLM.from_formula("y ~ x + z", df, family=pymc3.glm.families.Binomial())