def test_weight_and_integration_1d(self): """ Test that the Gaussian distribution intagrates to the weight. """ weight = 1.0 gaussian = Gaussian(cov=2.0, mean=4.0, log_weight=np.log(weight), var_names=["a"]) definite_integral, _ = integrate.quad(gaussian.potential, -100.0, 100.0) self.assertAlmostEqual(definite_integral, weight) # test another weight and with canonical form weight = 2.0 gaussian = Gaussian(cov=2.0, mean=4.0, log_weight=np.log(weight), var_names=["a"]) gaussian._update_canform() gaussian.covform = False definite_integral, _ = integrate.quad(gaussian.potential, -100.0, 100.0) self.assertAlmostEqual(definite_integral, weight)
def test_impossible_update_canform(self): """ Test that the _update_canform raises a LinAlgError when the covariance matrix is not invertible. """ gaussian = Gaussian(cov=np.zeros([2, 2]), mean=[0.0, 0.0], log_weight=0.0, var_names=["a", "b"]) with self.assertRaises(np.linalg.LinAlgError): gaussian._update_canform()
def test_marginalise_2d_canform(self): """ Test that the Gaussian marginalisation function returns the correct result for a two dimensional Gaussians. """ gaussian = Gaussian(cov=[[7.0, 2.0], [2.0, 1.0]], mean=[4.0, 1.0], log_weight=0.0, var_names=["a", "b"]) expected_result = Gaussian(cov=7.0, mean=4.0, log_weight=0.0, var_names=["a"]) expected_result._update_canform() gaussian._update_canform() actual_result = gaussian.marginalize(vrs=["a"], keep=True) self.assertTrue(expected_result.equals(actual_result))