def setUp(self): self.params = {"num_trees":5,"num_sweeps":2,"num_cutpoints":10, "max_depth_num":5,"burnin":1} self.model = xbart.XBART(**self.params) self.model_2 = xbart.XBART(**self.params) n = 100 self.x = np.random.rand(n)
def test_z_from_json(self): model = xbart.XBART() model.from_json("model.xbart") n_test = 1000; d = 10 x_test= np.random.rand(n_test,d) y_pred_json = model.predict(x_test,return_mean=False) self.assertFalse(np.array_equal(y_pred_json,y_pred_json*0))
def test_dimension_mismatch_x_y(self): with self.assertRaises(AssertionError): x,x_test,y,y_test,d = self.make_data() # Make y not match x y = np.concatenate((y,y)) model = xbart.XBART(model="Probit",**self.params) model.fit(x,y,d-1)
def test_dimension_mismatch_x_x(self): with self.assertRaises(AssertionError): x,x_test,y,y_test,d = self.make_data() model = xbart.XBART(model="Probit",**self.params) model.fit(x,y,d-1) # Make x_test not match x_test = np.concatenate((x_test,x_test),axis=1) model.predict(x_test)
def test_multinomial(self): x,x_test,y,y_test,d = self.make_data() y = (y >0)*1 ; y_test = (y_test > 0)*1 model = xbart.XBART(model="Multinomial",**self.params,num_classes=2) model.fit(x,y,d-1) y_pred = (model.predict(x_test)[:,1] > 0)*1 y_bin = (y_test > 0 )*1 acc = np.mean(y_pred == y_bin) print("Accuracy:" + str(acc)) self.assertTrue(acc > 0.1)
def test_probit(self): x,x_test,y,y_test,d = self.make_data() y = (y >0)*2-1 ; y_test = (y_test > 0)*2-1 model = xbart.XBART(model="Probit",**self.params) model.fit(x,y,d-1) y_pred = (model.predict(x_test) > 0)*1 y_bin = (y_test > 0 )*1 acc = np.mean(y_pred == y_bin) print("Accuracy:" + str(acc)) self.assertTrue(acc > 0.6)
def test_bool_with_good_float(self): xbart.XBART(parallel = 0.0)
def test_bool_with_good_int(self): xbart.XBART(parallel = 0)
def test_bool_with_bad_string(self): with self.assertRaises(TypeError): xbart.XBART(parallel = "2")
def test_bool_with_bad_float(self): with self.assertRaises(TypeError): xbart.XBART(parallel= 2.2)
def test_bool_with_bad_int(self): with self.assertRaises(TypeError): params = {"parallel":2} xbart.XBART(parallel = 2)
def test_float_bad_string(self): with self.assertRaises(TypeError): xbart.XBART(alpha="5")
def test_float_good_int(self): xbart.XBART(alpha=5)
def test_int_as_good_float(self): xbart.XBART(num_trees = 5.0)
def test_int_as_bad_string(self): with self.assertRaises(TypeError): xbart.XBART(num_trees = 5.1)
def test_missing_values(self): x,x_test,y,y_test,d = self.make_data() x[0,5] = np.NINF model = xbart.XBART(**self.params) model.fit(x,y,d-1)