def test_linear_component(self): vars_to_create = { "sigma", "sigma_interval__", "y_obs", "lm_x0", "lm_Intercept" } with Model() as model: lm = LinearComponent(self.data_linear["x"], self.data_linear["y"], name="lm") # yields lm_x0, lm_Intercept sigma = Uniform("sigma", 0, 20) # yields sigma_interval__ Normal("y_obs", mu=lm.y_est, sigma=sigma, observed=self.y_linear) # yields y_obs start = find_MAP(vars=[sigma]) step = Slice(model.vars) trace = sample(500, tune=0, step=step, start=start, progressbar=False, random_seed=self.random_seed) assert round(abs(np.mean(trace["lm_Intercept"]) - self.intercept), 1) == 0 assert round(abs(np.mean(trace["lm_x0"]) - self.slope), 1) == 0 assert round(abs(np.mean(trace["sigma"]) - self.sd), 1) == 0 assert vars_to_create == set(model.named_vars.keys())
def test_linear_component(self): vars_to_create = { 'sigma', 'sigma_interval__', 'y_obs', 'lm_x0', 'lm_Intercept' } with Model() as model: lm = LinearComponent(self.data_linear['x'], self.data_linear['y'], name='lm') # yields lm_x0, lm_Intercept sigma = Uniform('sigma', 0, 20) # yields sigma_interval__ Normal('y_obs', mu=lm.y_est, sigma=sigma, observed=self.y_linear) # yields y_obs start = find_MAP(vars=[sigma]) step = Slice(model.vars) trace = sample(500, tune=0, step=step, start=start, progressbar=False, random_seed=self.random_seed) assert round(abs(np.mean(trace['lm_Intercept']) - self.intercept), 1) == 0 assert round(abs(np.mean(trace['lm_x0']) - self.slope), 1) == 0 assert round(abs(np.mean(trace['sigma']) - self.sd), 1) == 0 assert vars_to_create == set(model.named_vars.keys())
def test_linear_component_from_formula(self): with Model() as model: lm = LinearComponent.from_formula('y ~ x', self.data_linear) sigma = Uniform('sigma', 0, 20) Normal('y_obs', mu=lm.y_est, sd=sigma, observed=self.y_linear) start = find_MAP(vars=[sigma]) step = Slice(model.vars) trace = sample(500, step=step, start=start, 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['sigma']), self.sd, 1)
def test_linear_component_from_formula(self): with Model() as model: lm = LinearComponent.from_formula('y ~ x', self.data_linear) sigma = Uniform('sigma', 0, 20) Normal('y_obs', mu=lm.y_est, sd=sigma, observed=self.y_linear) start = find_MAP(vars=[sigma]) step = Slice(model.vars) trace = sample(500, step=step, start=start, 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['sigma'])-self.sd), 1) == 0
def test_linear_component_from_formula(self): with Model() as model: lm = LinearComponent.from_formula("y ~ x", self.data_linear) sigma = Uniform("sigma", 0, 20) Normal("y_obs", mu=lm.y_est, sigma=sigma, observed=self.y_linear) start = find_MAP(vars=[sigma]) step = Slice(model.vars) trace = sample( 500, tune=0, step=step, start=start, 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["sigma"]) - self.sigma), 1) == 0