class TestSumCov(unittest.TestCase): def setUp(self): np.random.seed(1) self._X1 = np.random.randn(10, 5) self._X2 = np.random.randn(10, 8) self._cov1 = SQExpCov(self._X1) self._cov2 = SQExpCov(self._X2) self._cov = SumCov(self._cov1, self._cov2) def test_sum_combination(self): K1 = self._cov1.K() + self._cov2.K() K2 = self._cov.K() np.testing.assert_almost_equal(K1, K2) def test_Kgrad(self): cov = self._cov def func(x, i): cov.setParams(x) return cov.K() def grad(x, i): cov.setParams(x) return cov.K_grad_i(i) x0 = cov.getParams() err = mcheck_grad(func, grad, x0) np.testing.assert_almost_equal(err, 0.) def test_use_to_predict_exception(self): with self.assertRaises(NotImplementedError): self._cov.use_to_predict = 1.
class TestProd(unittest.TestCase): def setUp(self): # np.random.seed(1) self._X1 = np.random.randn(10, 5) self._X2 = np.random.randn(10, 8) self._X3 = np.random.randn(10, 7) self._cov1 = SQExpCov(self._X1) self._cov2 = SQExpCov(self._X2) self._cov3 = SQExpCov(self._X3) self._cov = ProdCov(self._cov1, self._cov2, self._cov3) def test_sum_combination(self): K1 = self._cov1.K() * self._cov2.K() * self._cov3.K() K2 = self._cov.K() np.testing.assert_almost_equal(K1, K2) def test_Kgrad(self): cov = self._cov def func(x, i): cov.setParams(x) return cov.K() def grad(x, i): cov.setParams(x) return cov.K_grad_i(i) x0 = cov.getParams() err = mcheck_grad(func, grad, x0) np.testing.assert_almost_equal(err, 0.) def test_Khess(self): cov = self._cov for j in range(cov.getNumberParams()): def func(x, i): cov.setParams(x) return cov.K_grad_i(j) def grad(x, i): cov.setParams(x) return cov.K_hess_i_j(j, i) x0 = cov.getParams() err = mcheck_grad(func, grad, x0) np.testing.assert_almost_equal(err, 0.) def test_use_to_predict_exception(self): with self.assertRaises(NotImplementedError): self._cov.use_to_predict = 1.
def simulate_local(self): tmp = SQExpCov(self.X) tmp.length = self.l2 k = tmp.K() k *= covar_rescaling_factor_efficient(k) self.covar += k
class TestSQExp(unittest.TestCase): def setUp(self): np.random.seed(1) self._X = np.random.randn(10, 5) self._cov = SQExpCov(self._X) def test_setX_retE(self): X1 = self._X np.random.seed(2) X2 = np.random.randn(10, 5) E1 = sp.spatial.distance.pdist(X1,'euclidean')**2 E1 = sp.spatial.distance.squareform(E1) E2 = sp.spatial.distance.pdist(X2,'euclidean')**2 E2 = sp.spatial.distance.squareform(E2) np.testing.assert_almost_equal(E1, self._cov.E()) self._cov.X = X2 np.testing.assert_almost_equal(E2, self._cov.E()) def test_param_activation(self): self._cov.act_scale = False self._cov.act_length = False self.assertEqual(len(self._cov.getParams()), 0) self._cov.act_scale = False self._cov.act_length = True self.assertEqual(len(self._cov.getParams()), 1) self._cov.act_scale = True self._cov.act_length = False self.assertEqual(len(self._cov.getParams()), 1) self._cov.act_scale = True self._cov.act_length = True self.assertEqual(len(self._cov.getParams()), 2) self._cov.act_scale = False self._cov.act_length = False self._cov.setParams(np.array([])) with self.assertRaises(ValueError): self._cov.setParams(np.array([0])) with self.assertRaises(ValueError): self._cov.K_grad_i(0) with self.assertRaises(ValueError): self._cov.K_grad_i(1) def test_Kgrad(self): def func(x, i): self._cov.scale = x[i] return self._cov.K() def grad(x, i): self._cov.scale = x[i] return self._cov.K_grad_i(0) x0 = np.array([self._cov.scale]) err = mcheck_grad(func, grad, x0) np.testing.assert_almost_equal(err, 0.) def func(x, i): self._cov.length = x[i] return self._cov.K() def grad(x, i): self._cov.scale = x[i] return self._cov.K_grad_i(1) x0 = np.array([self._cov.scale]) err = mcheck_grad(func, grad, x0) def test_Kgrad_activation(self): self._cov.act_length = False def func(x, i): self._cov.scale = x[i] return self._cov.K() def grad(x, i): self._cov.scale = x[i] return self._cov.K_grad_i(0) x0 = np.array([self._cov.scale]) err = mcheck_grad(func, grad, x0) np.testing.assert_almost_equal(err, 0.) self._cov.act_scale = False self._cov.act_length = True def func(x, i): self._cov.length = x[i] return self._cov.K() def grad(x, i): self._cov.length = x[i] return self._cov.K_grad_i(0) x0 = np.array([self._cov.length]) err = mcheck_grad(func, grad, x0) np.testing.assert_almost_equal(err, 0.) def test_input(self): with self.assertRaises(ValueError): SQExpCov(np.array([[np.inf]])) with self.assertRaises(ValueError): SQExpCov(np.array([[np.nan]])) with self.assertRaises(NotArrayConvertibleError): SQExpCov("Ola meu querido.")