def test_method(self): # Create mcmc xs = [ self.real_parameters * 1.1, self.real_parameters * 1.05, self.real_parameters * 0.9, self.real_parameters * 0.95, ] mcmc = pints.DifferentialEvolutionMCMC(4, xs) # Perform short run chains = [] for i in range(100): xs = mcmc.ask() fxs = np.array([self.log_posterior(x) for x in xs]) ys, fys, ac = mcmc.tell(fxs) if i >= 50: chains.append(ys) if np.any(ac): self.assertTrue(np.all(xs[ac] == ys[ac])) self.assertTrue(np.all(fys[ac] == fxs[ac])) chains = np.array(chains) self.assertEqual(chains.shape[0], 50) self.assertEqual(chains.shape[1], len(xs)) self.assertEqual(chains.shape[2], len(xs[0]))
def test_set_hyper_parameters(self): """ Tests the hyper-parameter interface for this sampler. """ n = 3 x0 = [self.real_parameters] * n mcmc = pints.DifferentialEvolutionMCMC(n, x0) self.assertEqual(mcmc.n_hyper_parameters(), 5) mcmc.set_hyper_parameters([0.5, 0.6, 20, 0, 0]) self.assertEqual(mcmc._gamma, 0.5) self.assertEqual(mcmc._b, 0.6) self.assertEqual(mcmc._gamma_switch_rate, 20) self.assertTrue(not mcmc._gaussian_error) self.assertTrue(not mcmc._relative_scaling) mcmc.set_gamma(0.5) self.assertEqual(mcmc._gamma, 0.5) self.assertRaisesRegex(ValueError, 'non-negative', mcmc.set_gamma, -1) mcmc.set_scale_coefficient(1) self.assertTrue(not mcmc._relative_scaling) self.assertRaisesRegex(ValueError, 'non-negative', mcmc.set_scale_coefficient, -1) mcmc.set_gamma_switch_rate(11) self.assertEqual(mcmc._gamma_switch_rate, 11) self.assertRaisesRegex(ValueError, 'integer', mcmc.set_gamma_switch_rate, 11.5) self.assertRaisesRegex(ValueError, 'exceed 1', mcmc.set_gamma_switch_rate, 0) mcmc.set_gaussian_error(False) self.assertTrue(not mcmc._gaussian_error) mcmc.set_relative_scaling(0) self.assertTrue( np.array_equal(mcmc._b_star, np.repeat(mcmc._b, mcmc._n_parameters))) mcmc.set_relative_scaling(1) self.assertTrue(np.array_equal(mcmc._b_star, mcmc._mu * mcmc._b)) # test implicit conversion to int mcmc.set_hyper_parameters([0.5, 0.6, 20.2, 0, 0]) self.assertEqual(mcmc._gamma_switch_rate, 20) self.assertRaisesRegex(ValueError, 'convertable to an integer', mcmc.set_hyper_parameters, (0.5, 0.6, 'sdf', 0, 0))
def test_flow(self): # Test we have at least 3 chains n = 2 x0 = [self.real_parameters] * n self.assertRaises(ValueError, pints.DifferentialEvolutionMCMC, n, x0) # Test initial proposal is first point n = 3 x0 = [self.real_parameters] * n mcmc = pints.DifferentialEvolutionMCMC(n, x0) self.assertTrue(mcmc.ask() is mcmc._x0) # Double initialisation mcmc = pints.DifferentialEvolutionMCMC(n, x0) mcmc.ask() self.assertRaises(RuntimeError, mcmc._initialise) # Tell without ask mcmc = pints.DifferentialEvolutionMCMC(n, x0) self.assertRaises(RuntimeError, mcmc.tell, 0) # Repeated asks should return same point mcmc = pints.DifferentialEvolutionMCMC(n, x0) # Get into accepting state for i in range(100): mcmc.tell([self.log_posterior(x) for x in mcmc.ask()]) x = mcmc.ask() for i in range(10): self.assertTrue(x is mcmc.ask()) # Repeated tells should fail mcmc.tell([1, 1, 1]) self.assertRaises(RuntimeError, mcmc.tell, [1, 1, 1]) # Bad starting point mcmc = pints.DifferentialEvolutionMCMC(n, x0) mcmc.ask() self.assertRaises(ValueError, mcmc.tell, float('-inf')) # Use uniform error mcmc = pints.DifferentialEvolutionMCMC(n, x0) mcmc.set_gaussian_error(False) for i in range(10): mcmc.tell([self.log_posterior(x) for x in mcmc.ask()]) # Use absolute scaling mcmc = pints.DifferentialEvolutionMCMC(n, x0) mcmc.set_relative_scaling(False) for i in range(10): mcmc.tell([self.log_posterior(x) for x in mcmc.ask()])