예제 #1
0
    def test_method(self):

        # Create mcmc
        x0s = [
            self.real_parameters * 1.1,
            self.real_parameters * 1.05,
            self.real_parameters * 0.9,
            self.real_parameters * 0.95,
        ]
        mcmc = pints.EmceeHammerMCMC(4, x0s)

        # Perform short run
        chains = []
        for i in range(100):
            xs = mcmc.ask()
            fxs = [self.log_posterior(x) for x in xs]
            samples = mcmc.tell(fxs)
            if i >= 50:
                chains.append(samples)
            if np.all(samples == xs):
                self.assertTrue(np.all(mcmc.current_log_pdfs() == fxs))

        chains = np.array(chains)
        self.assertEqual(chains.shape[0], 50)
        self.assertEqual(chains.shape[1], len(x0s))
        self.assertEqual(chains.shape[2], len(x0s[0]))
예제 #2
0
    def test_method(self):

        # Create mcmc
        x0s = [
            self.real_parameters * 1.1,
            self.real_parameters * 1.05,
            self.real_parameters * 0.9,
            self.real_parameters * 0.95,
        ]
        mcmc = pints.EmceeHammerMCMC(4, x0s)

        # 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)
            # Note: not checking xs equal ys here

        chains = np.array(chains)
        self.assertEqual(chains.shape[0], 50)
        self.assertEqual(chains.shape[1], len(x0s))
        self.assertEqual(chains.shape[2], len(x0s[0]))
예제 #3
0
    def test_flow(self):

        # Test we have at least 3 chains
        n = 2
        x0 = [self.real_parameters] * n
        self.assertRaises(ValueError, pints.EmceeHammerMCMC, n, x0)

        # Test initial proposal is first point
        n = 3
        x0 = [self.real_parameters] * n
        mcmc = pints.EmceeHammerMCMC(n, x0)
        self.assertTrue(mcmc.ask() is mcmc._x0)

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

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

        # Repeated asks should return same point
        mcmc = pints.EmceeHammerMCMC(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])
        self.assertRaises(RuntimeError, mcmc.tell, [1])

        # Bad starting point
        mcmc = pints.EmceeHammerMCMC(n, x0)
        mcmc.ask()
        self.assertRaises(ValueError, mcmc.tell, float('-inf'))
예제 #4
0
    def test_set_hyper_parameters(self):
        # Tests the parameter and hyper-parameter interfaces for this sampler.

        n = 3
        x0 = [self.real_parameters] * n
        mcmc = pints.EmceeHammerMCMC(n, x0)

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

        scale = mcmc.scale() + 0.1
        mcmc.set_hyper_parameters([scale])
        self.assertEqual(mcmc.scale(), scale)

        self.assertRaisesRegex(ValueError, 'exceed', mcmc.set_hyper_parameters,
                               [1])