Exemplo n.º 1
0
    def test_method(self):

        # Create mcmc
        x0 = self.real_parameters * 1.1
        mcmc = pints.PopulationMCMC(x0)

        # PopulationMCMC uses adaptive covariance internally, so requires an
        # initial phase
        self.assertTrue(mcmc.needs_initial_phase())
        self.assertTrue(mcmc.in_initial_phase())

        # Test schedule
        s = np.array([0, 0.1, 0.5])
        mcmc.set_temperature_schedule(s)
        self.assertTrue(np.all(s == mcmc.temperature_schedule()))

        # Perform short run
        chain = []
        for i in range(100):
            x = mcmc.ask()
            fx = self.log_posterior(x)
            sample = mcmc.tell(fx)
            if i == 20:
                self.assertTrue(mcmc.in_initial_phase())
                mcmc.set_initial_phase(False)
                self.assertFalse(mcmc.in_initial_phase())
            if i >= 50:
                chain.append(sample)
            if np.all(sample == x):
                self.assertEqual(mcmc.current_log_pdf(), fx)

        chain = np.array(chain)
        self.assertEqual(chain.shape[0], 50)
        self.assertEqual(chain.shape[1], len(x0))
Exemplo n.º 2
0
    def test_errors(self):

        mcmc = pints.PopulationMCMC(self.real_parameters)
        self.assertRaises(ValueError, mcmc.set_temperature_schedule, 1)
        self.assertRaises(ValueError, mcmc.set_temperature_schedule, [0])
        self.assertRaises(ValueError, mcmc.set_temperature_schedule, [0.5])
        mcmc.set_temperature_schedule([0, 0.5])
        self.assertRaises(
            ValueError, mcmc.set_temperature_schedule, [0.5, 0.5])
        self.assertRaises(ValueError, mcmc.set_temperature_schedule, [0, -0.1])
        self.assertRaises(ValueError, mcmc.set_temperature_schedule, [0, 1.1])

        mcmc = pints.PopulationMCMC(self.real_parameters)
        mcmc._initialise()
        self.assertRaises(RuntimeError, mcmc._initialise)
        self.assertRaises(
            RuntimeError, mcmc.set_temperature_schedule, [0, 0.1])

        mcmc = pints.PopulationMCMC(self.real_parameters)
        self.assertRaises(RuntimeError, mcmc.tell, 1)
Exemplo n.º 3
0
    def test_hyperparameters(self):

        mcmc = pints.PopulationMCMC(self.real_parameters)
        self.assertEqual(mcmc.n_hyper_parameters(), 1)

        # Test setting with an int
        mcmc.set_temperature_schedule(7)
        x = mcmc.temperature_schedule()
        self.assertEqual(len(x), 7)
        mcmc.set_temperature_schedule(8)
        self.assertEqual(len(mcmc.temperature_schedule()), 8)
        mcmc.set_hyper_parameters([7])
        y = mcmc.temperature_schedule()
        self.assertEqual(len(y), 7)
        self.assertTrue(np.all(x == y))

        # Test setting with a list
        mcmc.set_temperature_schedule(x)
        self.assertRaisesRegex(ValueError, 'First hyper-parameter',
                               mcmc.set_hyper_parameters, [x])