Exemple #1
0
 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')
Exemple #2
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
Exemple #3
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
Exemple #4
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
Exemple #5
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
Exemple #6
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)
Exemple #7
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
Exemple #8
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)

            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)
Exemple #9
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)
Exemple #10
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
Exemple #11
0
 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",
         )
Exemple #12
0
    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
Exemple #13
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)

            self.assertAlmostEqual(np.mean(trace['Intercept']), self.intercept,
                                   1)
            self.assertAlmostEqual(np.mean(trace['x']), self.slope, 1)
Exemple #14
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)
Exemple #15
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=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
Exemple #16
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
Exemple #17
0
 def test_from_xy(self):
     with Model():
         GLM(
             self.data_logistic["x"],
             self.data_logistic["y"],
             family=families.Binomial(link=families.logit),
             name="glm1",
         )
Exemple #18
0
 def test_from_xy(self):
     with Model():
         GLM(self.data_logistic['x'],
             self.data_logistic['y'],
             family=families.Binomial(link=families.logit),
             name='glm1')
Exemple #19
0
 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())
Exemple #20
0
 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())