示例#1
0
    def test_ask_tell(self):
        # Tests ask and tell

        # test that ellipses are estimated
        sampler = pints.NestedEllipsoidSampler(self.log_prior)
        A1 = np.copy(sampler._A)
        c1 = sampler._centroid
        sampler.set_n_rejection_samples(100)
        sampler.set_ellipsoid_update_gap(10)
        for i in range(5000):
            pt = sampler.ask(1)
            fx = self.log_likelihood(pt)
            sampler.tell(fx)
        A2 = sampler._A
        c2 = sampler._centroid
        self.assertTrue(not np.array_equal(A1, A2))
        self.assertTrue(not np.array_equal(c1, c2))

        # test multiple points being asked and tell'd
        sampler = pints.NestedEllipsoidSampler(self.log_prior)
        pts = sampler.ask(50)
        self.assertEqual(len(pts), 50)
        fx = [self.log_likelihood(pt) for pt in pts]
        proposed = sampler.tell(fx)
        self.assertTrue(len(proposed) > 1)

        # test multiple ask points after rejection samples
        sampler = pints.NestedEllipsoidSampler(self.log_prior)
        sampler.set_n_rejection_samples(10)
        for i in range(100):
            self.assertEqual(len(sampler.ask(20)), 20)
示例#2
0
    def test_logging(self):
        """ Tests logging to screen and file. """

        # No logging
        with StreamCapture() as c:
            sampler = pints.NestedEllipsoidSampler(self.log_likelihood,
                                                   self.log_prior)
            sampler.set_posterior_samples(2)
            sampler.set_rejection_samples(5)
            sampler.set_iterations(10)
            sampler.set_active_points_rate(10)
            sampler.set_log_to_screen(False)
            sampler.set_log_to_file(False)
            samples, margin = sampler.run()
        self.assertEqual(c.text(), '')

        # Log to screen
        with StreamCapture() as c:
            sampler = pints.NestedEllipsoidSampler(self.log_likelihood,
                                                   self.log_prior)
            sampler.set_posterior_samples(2)
            sampler.set_rejection_samples(5)
            sampler.set_iterations(20)
            sampler.set_active_points_rate(10)
            sampler.set_log_to_screen(True)
            sampler.set_log_to_file(False)
            samples, margin = sampler.run()
        lines = c.text().splitlines()
        self.assertEqual(lines[0], 'Running nested rejection sampling')
        self.assertEqual(lines[1], 'Number of active points: 10')
        self.assertEqual(lines[2], 'Total number of iterations: 20')
        self.assertEqual(lines[3], 'Enlargement factor: 1.5')
        self.assertEqual(lines[4], 'Total number of posterior samples: 2')
        self.assertEqual(lines[5], 'Iter. Eval. Time m:s')
        pattern = re.compile('[0-9]+[ ]+[0-9]+[ ]+[0-9]{1}:[0-9]{2}.[0-9]{1}')
        for line in lines[6:]:
            self.assertTrue(pattern.match(line))
        self.assertEqual(len(lines), 12)

        # Log to file
        with StreamCapture() as c:
            with TemporaryDirectory() as d:
                filename = d.path('test.txt')
                sampler = pints.NestedEllipsoidSampler(self.log_likelihood,
                                                       self.log_prior)
                sampler.set_posterior_samples(2)
                sampler.set_rejection_samples(5)
                sampler.set_iterations(10)
                sampler.set_active_points_rate(10)
                sampler.set_log_to_screen(False)
                sampler.set_log_to_file(filename)
                samples, margin = sampler.run()
                with open(filename, 'r') as f:
                    lines = f.read().splitlines()
            self.assertEqual(c.text(), '')
        self.assertEqual(len(lines), 6)
        self.assertEqual(lines[0], 'Iter. Eval. Time m:s')
        pattern = re.compile('[0-9]+[ ]+[0-9]+[ ]+[0-9]{1}:[0-9]{2}.[0-9]{1}')
        for line in lines[5:]:
            self.assertTrue(pattern.match(line))
示例#3
0
    def test_getters_and_setters(self):
        # Tests various get() and set() methods.
        sampler = pints.NestedEllipsoidSampler(self.log_prior)

        # Active points
        x = sampler.n_active_points() + 1
        self.assertNotEqual(sampler.n_active_points(), x)
        sampler.set_n_active_points(x)
        self.assertEqual(sampler.n_active_points(), x)
        self.assertRaisesRegex(
            ValueError, 'greater than 5', sampler.set_n_active_points, 5)

        # Rejection samples
        x = sampler.n_rejection_samples() + 1
        self.assertNotEqual(sampler.n_rejection_samples(), x)
        sampler.set_n_rejection_samples(x)
        self.assertEqual(sampler.n_rejection_samples(), x)
        self.assertRaisesRegex(
            ValueError, 'negative', sampler.set_n_rejection_samples, -1)

        # Enlargement factor
        x = sampler.enlargement_factor() * 2
        self.assertNotEqual(sampler.enlargement_factor(), x)
        sampler.set_enlargement_factor(x)
        self.assertEqual(sampler.enlargement_factor(), x)
        self.assertRaisesRegex(
            ValueError, 'exceed 1', sampler.set_enlargement_factor, 0.5)
        self.assertRaisesRegex(
            ValueError, 'exceed 1', sampler.set_enlargement_factor, 1)

        # Ellipsoid update gap
        x = sampler.ellipsoid_update_gap() * 2
        self.assertNotEqual(sampler.ellipsoid_update_gap(), x)
        sampler.set_ellipsoid_update_gap(x)
        self.assertEqual(sampler.ellipsoid_update_gap(), x)
        self.assertRaisesRegex(
            ValueError, 'exceed 1', sampler.set_ellipsoid_update_gap, 0.5)
        self.assertRaisesRegex(
            ValueError, 'exceed 1', sampler.set_ellipsoid_update_gap, 1)

        # dynamic enlargement factor
        self.assertTrue(not sampler.dynamic_enlargement_factor())
        sampler.set_dynamic_enlargement_factor(1)
        self.assertTrue(sampler.dynamic_enlargement_factor())

        # alpha
        self.assertRaises(ValueError, sampler.set_alpha, -0.2)
        self.assertRaises(ValueError, sampler.set_alpha, 1.2)
        self.assertEqual(sampler.alpha(), 0.2)
        sampler.set_alpha(0.4)
        self.assertEqual(sampler.alpha(), 0.4)

        # initial phase
        self.assertTrue(sampler.needs_initial_phase())
        self.assertTrue(sampler.in_initial_phase())
        sampler.set_initial_phase(False)
        self.assertTrue(not sampler.in_initial_phase())
        self.assertEqual(sampler.name(), 'Nested ellipsoidal sampler')
示例#4
0
    def test_getters_and_setters(self):
        """
        Tests various get() and set() methods.
        """
        sampler = pints.NestedEllipsoidSampler(self.log_likelihood,
                                               self.log_prior)

        # Iterations
        x = sampler.iterations() + 1
        self.assertNotEqual(sampler.iterations(), x)
        sampler.set_iterations(x)
        self.assertEqual(sampler.iterations(), x)
        self.assertRaisesRegex(ValueError, 'negative', sampler.set_iterations,
                               -1)

        # Active points rate
        x = sampler.active_points_rate() + 1
        self.assertNotEqual(sampler.active_points_rate(), x)
        sampler.set_active_points_rate(x)
        self.assertEqual(sampler.active_points_rate(), x)
        self.assertRaisesRegex(ValueError, 'greater than 5',
                               sampler.set_active_points_rate, 5)

        # Posterior samples
        x = sampler.posterior_samples() + 1
        self.assertNotEqual(sampler.posterior_samples(), x)
        sampler.set_posterior_samples(x)
        self.assertEqual(sampler.posterior_samples(), x)
        self.assertRaisesRegex(ValueError, 'greater than zero',
                               sampler.set_posterior_samples, 0)

        # Rejection samples
        x = sampler.rejection_samples() + 1
        self.assertNotEqual(sampler.rejection_samples(), x)
        sampler.set_rejection_samples(x)
        self.assertEqual(sampler.rejection_samples(), x)
        self.assertRaisesRegex(ValueError, 'negative',
                               sampler.set_rejection_samples, -1)

        # Enlargement factor
        x = sampler.enlargement_factor() * 2
        self.assertNotEqual(sampler.enlargement_factor(), x)
        sampler.set_enlargement_factor(x)
        self.assertEqual(sampler.enlargement_factor(), x)
        self.assertRaisesRegex(ValueError, 'exceed 1',
                               sampler.set_enlargement_factor, 0.5)
        self.assertRaisesRegex(ValueError, 'exceed 1',
                               sampler.set_enlargement_factor, 1)

        # Ellipsoid update gap
        x = sampler.ellipsoid_update_gap() * 2
        self.assertNotEqual(sampler.ellipsoid_update_gap(), x)
        sampler.set_ellipsoid_update_gap(x)
        self.assertEqual(sampler.ellipsoid_update_gap(), x)
        self.assertRaisesRegex(ValueError, 'exceed 1',
                               sampler.set_ellipsoid_update_gap, 0.5)
        self.assertRaisesRegex(ValueError, 'exceed 1',
                               sampler.set_ellipsoid_update_gap, 1)
示例#5
0
 def test_hyper_params(self):
     # Tests the hyper parameter interface is working.
     sampler = pints.NestedEllipsoidSampler(self.log_prior)
     self.assertEqual(sampler.n_hyper_parameters(), 6)
     sampler.set_hyper_parameters([220, 130, 2.0, 133, 1, 0.8])
     self.assertEqual(sampler.n_active_points(), 220)
     self.assertEqual(sampler.n_rejection_samples(), 130)
     self.assertEqual(sampler.enlargement_factor(), 2.0)
     self.assertEqual(sampler.ellipsoid_update_gap(), 133)
     self.assertTrue(sampler.dynamic_enlargement_factor())
     self.assertTrue(sampler.alpha(), 0.8)
示例#6
0
 def test_hyper_params(self):
     """
     Tests the hyper parameter interface is working.
     """
     sampler = pints.NestedEllipsoidSampler(self.log_likelihood,
                                            self.log_prior)
     self.assertEqual(sampler.n_hyper_parameters(), 3)
     sampler.set_hyper_parameters([6, 2, 3])
     self.assertEqual(sampler.active_points_rate(), 6)
     self.assertEqual(sampler.ellipsoid_update_gap(), 2)
     self.assertEqual(sampler.enlargement_factor(), 3)
示例#7
0
    def test_nones(self):
        # test handing of nones
        # test that None is returned
        sampler = pints.NestedEllipsoidSampler(self.log_prior)
        pts = sampler.ask(1)
        fx = np.nan
        sample, other = sampler.tell(fx)
        self.assertEqual(sample, None)

        # test that None is returned
        sampler = pints.NestedEllipsoidSampler(self.log_prior)
        pts = sampler.ask(1)
        fx = [np.nan, np.nan]
        sample, other = sampler.tell(fx)
        self.assertEqual(sample, None)

        # test if fx has one None and one non-none
        pts = sampler.ask(2)
        fx = [np.nan, -20]
        sample, other = sampler.tell(fx)
        self.assertEqual(sample[0], pts[1][0])
示例#8
0
    def test_quick(self):
        """ Test a single run. """

        sampler = pints.NestedEllipsoidSampler(self.log_likelihood,
                                               self.log_prior)
        sampler.set_posterior_samples(10)
        sampler.set_rejection_samples(20)
        sampler.set_iterations(50)
        sampler.set_active_points_rate(50)
        sampler.set_log_to_screen(False)
        samples, margin = sampler.run()
        # Check output: Note n returned samples = n posterior samples
        self.assertEqual(samples.shape, (10, 2))
示例#9
0
    def test_settings_check(self):
        """
        Tests the settings check at the start of a run.
        """
        sampler = pints.NestedEllipsoidSampler(self.log_likelihood,
                                               self.log_prior)
        sampler.set_posterior_samples(2)
        sampler.set_rejection_samples(5)
        sampler.set_iterations(10)
        sampler.set_active_points_rate(10)
        sampler.set_log_to_screen(False)
        sampler.run()

        sampler.set_posterior_samples(10)
        self.assertRaisesRegex(ValueError, 'exceed 0.25', sampler.run)
        sampler.set_posterior_samples(2)
        sampler.set_iterations(4)
        self.assertRaisesRegex(ValueError, 'exceed number of iterations',
                               sampler.run)