def test_options(self): # Test setting acceptance rate x0 = self.real_parameters mcmc = pints.DramACMC(x0) self.assertRaises(RuntimeError, mcmc.tell, 0.0) x0 = self.real_parameters mcmc = pints.DramACMC(x0) mcmc.ask() self.assertRaises(ValueError, mcmc.tell, -float('inf')) self.assertNotEqual(mcmc.target_acceptance_rate(), 0.5) mcmc.set_target_acceptance_rate(0.5) self.assertEqual(mcmc.target_acceptance_rate(), 0.5) mcmc.set_target_acceptance_rate(1) self.assertRaises(ValueError, mcmc.set_target_acceptance_rate, 0) self.assertRaises(ValueError, mcmc.set_target_acceptance_rate, -1e-6) self.assertRaises(ValueError, mcmc.set_target_acceptance_rate, 1.00001) # test hyperparameter setters and getters self.assertEqual(mcmc.n_hyper_parameters(), 3) self.assertRaises(ValueError, mcmc.set_hyper_parameters, [-0.1, 1, 3]) self.assertRaises(ValueError, mcmc.set_hyper_parameters, [0.5, 0, 3]) self.assertRaises(ValueError, mcmc.set_hyper_parameters, [0.5, 1, -1]) mcmc.set_hyper_parameters([0.1, 4, 3.5]) self.assertEqual(mcmc.eta(), 0.1) self.assertEqual(mcmc.n_kernels(), 4) self.assertEqual(mcmc.upper_scale(), 3.5) mcmc.ask() mcmc.set_sigma_scale() scale = mcmc.sigma_scale() a_min = np.log10(1) a_max = np.log10(3.5) scale1 = 10**np.linspace(a_min, a_max, 4) scale1 = scale1[::-1] self.assertTrue(np.array_equal(scale, scale1)) self.assertEqual(mcmc.name(), ('Delayed Rejection Adaptive Metropolis (Dram) MCMC'))
def test_options(self): # Test setting acceptance rate x0 = self.real_parameters mcmc = pints.DramACMC(x0) self.assertRaises(RuntimeError, mcmc.tell, 0.0) x0 = self.real_parameters mcmc = pints.DramACMC(x0) mcmc.ask() self.assertRaises(ValueError, mcmc.tell, -float('inf')) self.assertNotEqual(mcmc.target_acceptance_rate(), 0.5) mcmc.set_target_acceptance_rate(0.5) self.assertEqual(mcmc.target_acceptance_rate(), 0.5) mcmc.set_target_acceptance_rate(1) self.assertRaises(ValueError, mcmc.set_target_acceptance_rate, 0) self.assertRaises(ValueError, mcmc.set_target_acceptance_rate, -1e-6) self.assertRaises(ValueError, mcmc.set_target_acceptance_rate, 1.00001) # test hyperparameter setters and getters self.assertEqual(mcmc.n_hyper_parameters(), 3) self.assertRaises(ValueError, mcmc.set_hyper_parameters, [-0.1, 1, 3]) self.assertRaises(ValueError, mcmc.set_hyper_parameters, [0.5, 0, 3]) self.assertRaises(ValueError, mcmc.set_hyper_parameters, [0.5, 1, -1]) mcmc.set_hyper_parameters([0.1, 4, 3]) self.assertEqual(mcmc.eta(), 0.1) mcmc.ask() scale1 = [2, 3] mcmc.set_sigma_scale(scale1) scale = mcmc.sigma_scale() self.assertTrue(np.array_equal(scale, scale1)) self.assertRaisesRegex(ValueError, 'must be of length 2', mcmc.set_sigma_scale, [1]) self.assertEqual(mcmc.name(), 'Delayed Rejection Adaptive Metropolis (Dram) MCMC')
def test_method(self): # Create mcmc x0 = self.real_parameters * 1.1 mcmc = pints.DramACMC(x0) # Configure mcmc.set_target_acceptance_rate(0.3) mcmc.set_initial_phase(True) # Perform short run rate = [] chain = [] for i in range(100): x = mcmc.ask() fx = self.log_posterior(x) sample = mcmc.tell(fx) while sample is None: x = mcmc.ask() fx = self.log_posterior(x) sample = mcmc.tell(fx) if i == 20: mcmc.set_initial_phase(False) if i >= 50: chain.append(sample) rate.append(mcmc.acceptance_rate()) if np.all(sample == x): self.assertEqual(mcmc.current_log_pdf(), fx) chain = np.array(chain) rate = np.array(rate) self.assertEqual(chain.shape[0], 50) self.assertEqual(chain.shape[1], len(x0)) self.assertEqual(rate.shape[0], 100) # Test with more kernels x0 = self.real_parameters * 1.1 mcmc = pints.DramACMC(x0) # Configure mcmc.set_n_kernels(4) # Perform short run rate = [] chain = [] for i in range(100): x = mcmc.ask() fx = self.log_posterior(x) sample = mcmc.tell(fx) while sample is None: x = mcmc.ask() fx = self.log_posterior(x) sample = mcmc.tell(fx) if i == 20: mcmc.set_initial_phase(False) if i >= 50: chain.append(sample) rate.append(mcmc.acceptance_rate()) if np.all(sample == x): self.assertEqual(mcmc.current_log_pdf(), fx) chain = np.array(chain) rate = np.array(rate) self.assertEqual(chain.shape[0], 50) self.assertEqual(chain.shape[1], len(x0)) self.assertEqual(rate.shape[0], 100)
def test_method(self): # Create mcmc x0 = self.real_parameters * 1.1 mcmc = pints.DramACMC(x0) # Configure mcmc.set_target_acceptance_rate(0.3) mcmc.set_initial_phase(True) # Perform short run rate = [] chain = [] for i in range(100): x = mcmc.ask() fx = self.log_posterior(x) reply = mcmc.tell(fx) while reply is None: x = mcmc.ask() fx = self.log_posterior(x) reply = mcmc.tell(fx) y, fy, ac = reply if i == 20: mcmc.set_initial_phase(False) if i >= 50: chain.append(y) rate.append(mcmc.acceptance_rate()) self.assertTrue(isinstance(ac, bool)) if ac: self.assertTrue(np.all(x == y)) self.assertEqual(fx, fy) chain = np.array(chain) rate = np.array(rate) self.assertEqual(chain.shape[0], 50) self.assertEqual(chain.shape[1], len(x0)) self.assertEqual(rate.shape[0], 100) # Test with more kernels x0 = self.real_parameters * 1.1 mcmc = pints.DramACMC(x0) # Configure mcmc.set_n_kernels(4) # Perform short run rate = [] chain = [] for i in range(100): x = mcmc.ask() fx = self.log_posterior(x) reply = mcmc.tell(fx) while reply is None: x = mcmc.ask() fx = self.log_posterior(x) reply = mcmc.tell(fx) y, fy, ac = reply if i == 20: mcmc.set_initial_phase(False) if i >= 50: chain.append(y) rate.append(mcmc.acceptance_rate()) self.assertTrue(isinstance(ac, bool)) if ac: self.assertTrue(np.all(x == y)) self.assertEqual(fx, fy) chain = np.array(chain) rate = np.array(rate) self.assertEqual(chain.shape[0], 50) self.assertEqual(chain.shape[1], len(x0)) self.assertEqual(rate.shape[0], 100)