Exemple #1
0
    def test_flow(self):

        # Test initial proposal is first point
        x0 = self.real_parameters
        mcmc = pints.MetropolisRandomWalkMCMC(x0)
        self.assertTrue(mcmc.ask() is mcmc._x0)

        # Double initialisation
        mcmc = pints.MetropolisRandomWalkMCMC(x0)
        mcmc.ask()
        self.assertRaises(RuntimeError, mcmc._initialise)

        # Tell without ask
        mcmc = pints.MetropolisRandomWalkMCMC(x0)
        self.assertRaises(RuntimeError, mcmc.tell, 0)

        # Repeated asks should return same point
        mcmc = pints.MetropolisRandomWalkMCMC(x0)
        # Get nearer accepting state
        for i in range(100):
            mcmc.tell(self.log_posterior(mcmc.ask()))
        x = mcmc.ask()
        for i in range(10):
            self.assertTrue(x is mcmc.ask())

        # Repeated tells should fail
        mcmc.tell(1)
        self.assertRaises(RuntimeError, mcmc.tell, 1)

        # Bad starting point
        mcmc = pints.MetropolisRandomWalkMCMC(x0)
        mcmc.ask()
        self.assertRaises(ValueError, mcmc.tell, float('-inf'))
Exemple #2
0
    def test_replace(self):

        x0 = self.real_parameters * 1.1
        mcmc = pints.MetropolisRandomWalkMCMC(x0)

        # One round of ask-tell must have been run
        self.assertRaisesRegex(RuntimeError, 'already running', mcmc.replace,
                               x0, 1)

        mcmc.ask()

        # One round of ask-tell must have been run
        self.assertRaises(RuntimeError, mcmc.replace, x0, 1)

        mcmc.tell(0.5)
        mcmc.replace([1, 2, 3], 10)
        mcmc.replace([1, 2, 3], 10)

        # New position must have correct size
        self.assertRaisesRegex(ValueError,
                               '`current` has the wrong dimensions',
                               mcmc.replace, [1, 2], 1)

        # Proposal can be changed too
        mcmc.ask()
        mcmc.replace([1, 2, 3], 10, [3, 4, 5])

        # New proposal must have correct size
        self.assertRaisesRegex(ValueError,
                               '`proposed` has the wrong dimensions',
                               mcmc.replace, [1, 2, 3], 3, [3, 4])
Exemple #3
0
    def test_method(self):

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

        # Perform short run
        rate = []
        chain = []
        for i in range(100):
            x = mcmc.ask()
            fx = self.log_posterior(x)
            y, fy, ac = mcmc.tell(fx)
            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)
Exemple #4
0
    def test_set_hyper_parameters(self):
        # Tests the hyper-parameter interface for this optimiser.

        x0 = self.real_parameters
        mcmc = pints.MetropolisRandomWalkMCMC(x0)

        self.assertEqual(mcmc.n_hyper_parameters(), 0)

        mcmc.set_hyper_parameters([])
Exemple #5
0
    def test_replace(self):

        x0 = self.real_parameters * 1.1
        mcmc = pints.MetropolisRandomWalkMCMC(x0)
        self.assertRaises(RuntimeError, mcmc.replace, x0, 1)
        mcmc.ask()
        self.assertRaises(RuntimeError, mcmc.replace, x0, 1)
        mcmc.tell(0.5)
        mcmc.replace([1, 2, 3], 10)
        mcmc.replace([1, 2, 3], 10)
        self.assertRaises(ValueError, mcmc.replace, [1, 2], 1)
Exemple #6
0
    def test_method(self):

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

        # Perform short run
        rate = []
        chain = []
        for i in range(100):
            x = mcmc.ask()
            fx = self.log_posterior(x)
            sample = mcmc.tell(fx)
            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)