def setUp(self): mean = np.array([-1, 1, 0]) covariance = np.array([[6, 0.7, 0.2], [0.7, 3, 0.9], [0.2, 0.9, 1]]) self.test_model = JGD(["x", "y", "z"], mean, covariance) self.nuts_sampler = NUTSda( model=self.test_model, grad_log_pdf=GradLogPDFGaussian )
class TestNUTSInference(unittest.TestCase): def setUp(self): mean = np.array([-1, 1, 0]) covariance = np.array([[6, 0.7, 0.2], [0.7, 3, 0.9], [0.2, 0.9, 1]]) self.test_model = JGD(['x', 'y', 'z'], mean, covariance) self.nuts_sampler = NUTSda(model=self.test_model, grad_log_pdf=GradLogPDFGaussian) def test_errors(self): with self.assertRaises(TypeError): NUTS(model=self.test_model, grad_log_pdf=JGD) with self.assertRaises(TypeError): NUTS(model=self.test_model, grad_log_pdf=None, simulate_dynamics=GradLogPDFGaussian) with self.assertRaises(ValueError): NUTSda(model=self.test_model, delta=-0.2, grad_log_pdf=None) with self.assertRaises(ValueError): NUTSda(model=self.test_model, delta=1.1, grad_log_pdf=GradLogPDFGaussian) with self.assertRaises(TypeError): NUTS(self.test_model, GradLogPDFGaussian).sample(initial_pos={1, 1, 1}, num_samples=1) with self.assertRaises(ValueError): NUTS(self.test_model, GradLogPDFGaussian).sample(initial_pos=[1, 1], num_samples=1) with self.assertRaises(TypeError): NUTSda(self.test_model, GradLogPDFGaussian).sample(initial_pos=1, num_samples=1, num_adapt=1) with self.assertRaises(ValueError): NUTSda(self.test_model, GradLogPDFGaussian).sample(initial_pos=[1, 1, 1, 1], num_samples=1, num_adapt=1) with self.assertRaises(TypeError): NUTS(self.test_model, GradLogPDFGaussian).generate_sample(initial_pos=0.1, num_samples=1).send(None) with self.assertRaises(ValueError): NUTS(self.test_model, GradLogPDFGaussian).generate_sample(initial_pos=(0, 1, 1, 1), num_samples=1).send(None) with self.assertRaises(TypeError): NUTSda(self.test_model, GradLogPDFGaussian).generate_sample(initial_pos=[[1, 2, 3]], num_samples=1, num_adapt=1).send(None) with self.assertRaises(ValueError): NUTSda(self.test_model, GradLogPDFGaussian).generate_sample(initial_pos=[1], num_samples=1, num_adapt=1).send(None) def test_sampling(self): np.random.seed(1010101) samples = self.nuts_sampler.sample(initial_pos=[-0.4, 1, 3.6], num_adapt=0, num_samples=10000, return_type='recarray') sample_array = np.array( [samples[var_name] for var_name in self.test_model.variables]) sample_covariance = np.cov(sample_array) self.assertTrue( np.linalg.norm(sample_covariance - self.test_model.covariance) < 3) np.random.seed(1210161) samples = self.nuts_sampler.generate_sample(initial_pos=[-0.4, 1, 3.6], num_adapt=0, num_samples=10000) samples_array = np.array([sample for sample in samples]) sample_covariance = np.cov(samples_array.T) self.assertTrue( np.linalg.norm(sample_covariance - self.test_model.covariance) < 3) np.random.seed(12313131) samples = self.nuts_sampler.sample(initial_pos=[0.2, 0.4, 2.2], num_adapt=10000, num_samples=10000) sample_covariance = np.cov(samples.values.T) self.assertTrue( np.linalg.norm(sample_covariance - self.test_model.covariance) < 0.4) np.random.seed(921312312) samples = self.nuts_sampler.generate_sample( initial_pos=[0.2, 0.4, 2.2], num_adapt=10000, num_samples=10000) samples_array = np.array([sample for sample in samples]) sample_covariance = np.cov(samples_array.T) self.assertTrue( np.linalg.norm(sample_covariance - self.test_model.covariance) < 0.4) def tearDown(self): del self.test_model del self.nuts_sampler
def setUp(self): mean = np.array([-1, 1, 0]) covariance = np.array([[6, 0.7, 0.2], [0.7, 3, 0.9], [0.2, 0.9, 1]]) self.test_model = JGD(['x', 'y', 'z'], mean, covariance) self.nuts_sampler = NUTSda(model=self.test_model, grad_log_pdf=GradLogPDFGaussian)
class TestNUTSInference(unittest.TestCase): def setUp(self): mean = np.array([-1, 1, 0]) covariance = np.array([[6, 0.7, 0.2], [0.7, 3, 0.9], [0.2, 0.9, 1]]) self.test_model = JGD(['x', 'y', 'z'], mean, covariance) self.nuts_sampler = NUTSda(model=self.test_model, grad_log_pdf=GradLogPDFGaussian) def test_errors(self): with self.assertRaises(TypeError): NUTS(model=self.test_model, grad_log_pdf=JGD) with self.assertRaises(TypeError): NUTS(model=self.test_model, grad_log_pdf=None, simulate_dynamics=GradLogPDFGaussian) with self.assertRaises(ValueError): NUTSda(model=self.test_model, delta=-0.2, grad_log_pdf=None) with self.assertRaises(ValueError): NUTSda(model=self.test_model, delta=1.1, grad_log_pdf=GradLogPDFGaussian) with self.assertRaises(TypeError): NUTS(self.test_model, GradLogPDFGaussian).sample(initial_pos={1, 1, 1}, num_samples=1) with self.assertRaises(ValueError): NUTS(self.test_model, GradLogPDFGaussian).sample(initial_pos=[1, 1], num_samples=1) with self.assertRaises(TypeError): NUTSda(self.test_model, GradLogPDFGaussian).sample(initial_pos=1, num_samples=1, num_adapt=1) with self.assertRaises(ValueError): NUTSda(self.test_model, GradLogPDFGaussian).sample(initial_pos=[1, 1, 1, 1], num_samples=1, num_adapt=1) with self.assertRaises(TypeError): NUTS(self.test_model, GradLogPDFGaussian).generate_sample(initial_pos=0.1, num_samples=1).send(None) with self.assertRaises(ValueError): NUTS(self.test_model, GradLogPDFGaussian).generate_sample(initial_pos=(0, 1, 1, 1), num_samples=1).send(None) with self.assertRaises(TypeError): NUTSda(self.test_model, GradLogPDFGaussian).generate_sample(initial_pos=[[1, 2, 3]], num_samples=1, num_adapt=1).send(None) with self.assertRaises(ValueError): NUTSda(self.test_model, GradLogPDFGaussian).generate_sample(initial_pos=[1], num_samples=1, num_adapt=1).send(None) def test_sampling(self): np.random.seed(1010101) samples = self.nuts_sampler.sample(initial_pos=[-0.4, 1, 3.6], num_adapt=0, num_samples=10000, return_type='recarray') sample_array = np.array([samples[var_name] for var_name in self.test_model.variables]) sample_covariance = np.cov(sample_array) self.assertTrue(np.linalg.norm(sample_covariance - self.test_model.covariance) < 3) np.random.seed(1210161) samples = self.nuts_sampler.generate_sample(initial_pos=[-0.4, 1, 3.6], num_adapt=0, num_samples=10000) samples_array = np.array([sample for sample in samples]) sample_covariance = np.cov(samples_array.T) self.assertTrue(np.linalg.norm(sample_covariance - self.test_model.covariance) < 3) np.random.seed(12313131) samples = self.nuts_sampler.sample(initial_pos=[0.2, 0.4, 2.2], num_adapt=10000, num_samples=10000) sample_covariance = np.cov(samples.values.T) self.assertTrue(np.linalg.norm(sample_covariance - self.test_model.covariance) < 0.4) np.random.seed(921312312) samples = self.nuts_sampler.generate_sample(initial_pos=[0.2, 0.4, 2.2], num_adapt=10000, num_samples=10000) samples_array = np.array([sample for sample in samples]) sample_covariance = np.cov(samples_array.T) self.assertTrue(np.linalg.norm(sample_covariance - self.test_model.covariance) < 0.4) def tearDown(self): del self.test_model del self.nuts_sampler