def test_create_precomputed_info(self): num_samples = 20 x = np.array([[1, 2], [4, 5], [6, 7], [8, 9], [10, 11]]) noise_prior = Gaussian(mu=np.log(0.01), sigma=1) builder = HellingerDistanceBuilder(noise_prior, num_samples, max_num_hyperparameters=40, max_num_kernels=10, active_models=MagicMock(), initial_model_indices=MagicMock(), data_X=x) cov = Covariance(RBF(1)) p1 = LogGaussian(20, 1) p2 = LogGaussian(0, 1.1) cov.raw_kernel.variance.set_prior(p1, warning=False) cov.raw_kernel.lengthscale.set_prior(p2, warning=False) result = builder.create_precomputed_info(cov, x) self.assertIsInstance(result, tuple) self.assertEqual(len(result), 2) self.assertIsInstance(result[0], np.ndarray) self.assertIsInstance(result[1], np.ndarray) self.assertEqual(result[0].shape, (num_samples, )) self.assertEqual(result[1].shape, (x.shape[0], x.shape[0], num_samples))
def test_hellinger_distance(self): cov_i = Covariance(RBF(1)) p1 = LogGaussian(20, 1) p2 = LogGaussian(0, 1.1) cov_i.raw_kernel.variance.set_prior(p1, warning=False) cov_i.raw_kernel.lengthscale.set_prior(p2, warning=False) cov_j = Covariance(RationalQuadratic(1)) p3 = LogGaussian(11, 2) p4 = LogGaussian(2, 1.12) cov_j.raw_kernel.variance.set_prior(p3, warning=False) cov_j.raw_kernel.lengthscale.set_prior(p4, warning=False) cov_j.raw_kernel.power.set_prior(p2, warning=False) x = np.array([[1, 2], [4, 5], [6, 7], [8, 9], [10, 11]]) noise_prior = Gaussian(mu=np.log(0.01), sigma=1) builder = HellingerDistanceBuilder(noise_prior, num_samples=20, max_num_hyperparameters=40, max_num_kernels=10, active_models=MagicMock(), initial_model_indices=MagicMock(), data_X=x) log_det_i, mini_gram_matrices_i = builder.create_precomputed_info( cov_i, x) log_det_j, mini_gram_matrices_j = builder.create_precomputed_info( cov_j, x) result = HellingerDistanceBuilder.hellinger_distance( log_det_i, mini_gram_matrices_i, log_det_j, mini_gram_matrices_j) self.assertIsInstance(result, float)
def test_prior_log_gaussian_fast(self): prior_1 = LogGaussian(mu=0.5, sigma=2) prior_2 = LogGaussian(mu=0.7, sigma=2.2) prior_list = np.array([prior_1, prior_2]) samples = probability_samples() result = prior_sample_log_gaussian(prior_list, samples) self.assertIsInstance(result, np.ndarray) self.assertEqual(result.shape, (samples.shape[0], prior_list.size))
def setUp(self) -> None: self.x = np.array([[1, 2], [4, 5], [6, 7], [8, 9], [10, 11]]) self.noise_prior = Gaussian(mu=np.log(0.01), sigma=1) self.cov_i = RBF(1) p1 = LogGaussian(20, 1) p2 = LogGaussian(0, 1.1) self.cov_i.variance.set_prior(p1, warning=False) self.cov_i.lengthscale.set_prior(p2, warning=False) models = [GPModel(self.cov_i)] self.active_models = ActiveSet(max_n_models=3) self.active_models.models = models self.ind_init = [0]
def setUp(self) -> None: self.x = np.array([[1, 2], [4, 5], [6, 7], [8, 9], [10, 11]]) self.noise_prior = Gaussian(mu=np.log(0.01), sigma=1) cov_1 = Covariance(RBF(1)) p1 = LogGaussian(20, 1) p2 = LogGaussian(0, 1.1) cov_1.raw_kernel.variance.set_prior(p1, warning=False) cov_1.raw_kernel.lengthscale.set_prior(p2, warning=False) cov_2 = Covariance(RBF(1)) p3 = LogGaussian(11, 1) p4 = LogGaussian(1, 1.21) cov_2.raw_kernel.variance.set_prior(p3, warning=False) cov_2.raw_kernel.lengthscale.set_prior(p4, warning=False) cov_3 = Covariance(RationalQuadratic(1)) p5 = LogGaussian(4, 1) p6 = LogGaussian(1.2, 1.21) p7 = LogGaussian(13, 1.21) cov_3.raw_kernel.variance.set_prior(p5, warning=False) cov_3.raw_kernel.lengthscale.set_prior(p6, warning=False) cov_3.raw_kernel.power.set_prior(p7, warning=False) models = [GPModel(cov_1), GPModel(cov_2), GPModel(cov_3)] self.active_models = ActiveSet(max_n_models=3) self.active_models.models = models self.ind_init = [0, 2]
def test_get_priors(self): # Test assertRaises ValueError if not all parameters have priors set c = RBF(1) self.assertRaises(ValueError, get_priors, c) c = RBF(1) * RationalQuadratic(1) p1 = LogGaussian(2, 2.21) p2 = LogGaussian(1, 2.1) p3 = LogGaussian(1, 2) c.parameters[0].variance.set_prior(p1, warning=False) c.parameters[1].lengthscale.set_prior(p2, warning=False) c.parameters[0].lengthscale.set_prior(p3, warning=False) c.parameters[1].power.set_prior(p2, warning=False) c.parameters[1].variance.set_prior(p1, warning=False) result = get_priors(c) self.assertIsInstance(result, np.ndarray) self.assertEqual(result.shape, (5, )) np.testing.assert_array_equal(result, [p1, p3, p1, p2, p2])