class TestAbstractModel(DerandomizedTestCase):
    
    def setUp(self):
        super(TestAbstractModel, self).setUp()
        self.mock_model = MockModel()

    def test_pr0_shape(self):
        """
        Model: Checks that pr0-based Model subtypes give the right shape.
        """
        outcomes = np.array([0, 1], dtype=int)
        modelparams = np.random.random((3, 2))
        expparams = np.zeros((4,), dtype=self.mock_model.expparams_dtype)
        
        assert self.mock_model.likelihood(outcomes, modelparams, expparams).shape == (2, 3, 4)
        
    def test_simulate_experiment(self):
        """
        Model: Checks that simulate_experiment behaves correctly.
        """
        modelparams = np.random.random((1, 2))
        expparams = np.zeros((1,), dtype=self.mock_model.expparams_dtype)
        
        assert_almost_equal(
            self.mock_model.simulate_experiment(modelparams, expparams, repeat=2000).mean(),
            0.5,
            1
        )
class TestAbstractModel(DerandomizedTestCase):
    def setUp(self):
        super(TestAbstractModel, self).setUp()
        self.mock_model = MockModel()

    def test_pr0_shape(self):
        """
        Model: Checks that pr0-based Model subtypes give the right shape.
        """
        outcomes = np.array([0, 1], dtype=int)
        modelparams = np.random.random((3, 2))
        expparams = np.zeros((4, ), dtype=self.mock_model.expparams_dtype)

        assert self.mock_model.likelihood(outcomes, modelparams,
                                          expparams).shape == (2, 3, 4)

    def test_simulate_experiment(self):
        """
        Model: Checks that simulate_experiment behaves correctly.
        """
        modelparams = np.random.random((1, 2))
        expparams = np.zeros((1, ), dtype=self.mock_model.expparams_dtype)

        assert_almost_equal(
            self.mock_model.simulate_experiment(modelparams,
                                                expparams,
                                                repeat=2000).mean(), 0.5, 1)
    def test_region_est_ellipsoid(self):
        """
        Tests that region_est_ellipsoid works.
        """

        dist = MultivariateNormalDistribution(self.MEAN, self.COV)
        # the model is irrelevant; we just want the updater to have some particles
        # with the desired normal distribution.
        u = SMCUpdater(MockModel(4), self.N_PARTICLES, dist)

        # ask for a confidence level of 0.5
        A, c = u.region_est_ellipsoid(level=0.5)

        # center of ellipse should be the mean of the multinormal
        assert_almost_equal(np.round(c), self.MEAN, 1)

        # finally, the principal lengths of the ellipsoid
        # should be the same as COV
        _, QA, _ = np.linalg.svd(A)
        _, QC, _ = np.linalg.svd(self.COV)
        QA, QC = np.sqrt(QA), np.sqrt(QC)
        assert_almost_equal(
            QA / np.linalg.norm(QA),
            QC / np.linalg.norm(QC),
            1
        )
示例#4
0
 def instantiate_model(self):
     return DirectViewParallelizedModel(
         MockModel(n_mps=2),
         MockDirectView(),
         # Disable the serial threshold to force the parallel
         # model to use our mocked direct view.
         serial_threshold=0)
    def test_region_est_hull(self):
        """
        Tests that test_region_est_hull works
        """
        dist = MultivariateNormalDistribution(self.MEAN, self.COV)
        # the model is irrelevant; we just want the updater to have some particles
        # with the desired normal distribution.
        u = SMCUpdater(MockModel(self.N_MPS), self.N_PARTICLES, dist)

        faces, vertices = u.region_est_hull(level=0.95)

        # In this multinormal case, the convex hull surface
        # should be centered at MEAN
        assert_almost_equal(np.round(np.mean(vertices, axis=0)),
                            np.round(self.MEAN))

        # And a lower level should result in a smaller hull
        # and therefore smaller sample variance
        faces2, vertices2 = u.region_est_hull(level=0.2)
        assert_array_less(np.var(vertices2, axis=0), np.var(vertices, axis=0))
    def test_est_credible_region(self):
        """
        Tests that est_credible_region doesn't fail miserably
        """
        dist = MultivariateNormalDistribution(self.MEAN, self.COV)
        # the model is irrelevant; we just want the updater to have some particles
        # with the desired normal distribution.
        u = SMCUpdater(MockModel(self.N_MPS), self.N_PARTICLES, dist)

        # first check that 0.95 confidence points consume 0.9 confidence points
        points1 = u.est_credible_region(level=0.95)
        points2 = u.est_credible_region(level=0.9)
        assert_almost_equal(
            np.sort(unique_rows(np.concatenate([points1, points2])), axis=0),
            np.sort(points1, axis=0))

        # do the same thing with different slice
        points1 = u.est_credible_region(level=0.95,
                                        modelparam_slice=self.SLICE)
        points2 = u.est_credible_region(level=0.9, modelparam_slice=self.SLICE)
        assert_almost_equal(
            np.sort(unique_rows(np.concatenate([points1, points2])), axis=0),
            np.sort(points1, axis=0))
    def test_in_credible_region(self):
        """
        Tests that in_credible_region works.
        """

        dist = MultivariateNormalDistribution(self.MEAN, self.COV)
        # the model is irrelevant; we just want the updater to have some particles
        # with the desired normal distribution.
        u = SMCUpdater(MockModel(4), self.N_PARTICLES, dist)

        # some points to test with
        test_points = np.random.multivariate_normal(self.MEAN, self.COV, self.N_PARTICLES)

        # method='pce'
        results = [
            u.in_credible_region(test_points, level=0.9, method='pce'),
            u.in_credible_region(test_points, level=0.84, method='pce'),
            u.in_credible_region(test_points, level=0.5, method='pce'),
        ]
        assert_almost_equal(
            np.array([np.mean(x.astype('float')) for x in results]),
            np.array([0.9, 0.84, 0.5]),
            3
        )

        # method='hpd-hull'
        results1 = [
            u.in_credible_region(test_points, level=0.9, method='hpd-hull'),
            u.in_credible_region(test_points, level=0.84, method='hpd-hull'),
            u.in_credible_region(test_points, level=0.5, method='hpd-hull'),
        ]
        assert_array_less(
            np.array([0.9, 0.84, 0.5]),
            np.array([np.mean(x.astype('float')) for x in results1])
        )

        # method='hpd-mvee'
        results2 = [
            u.in_credible_region(test_points, level=0.9, method='hpd-mvee'),
            u.in_credible_region(test_points, level=0.84, method='hpd-mvee'),
            u.in_credible_region(test_points, level=0.5, method='hpd-mvee'),
        ]
        assert_array_less(
            np.array([0.9, 0.84, 0.5]),
            np.array([np.mean(x.astype('float')) for x in results2])
        )

        # the mvee should be bigger than the convex hull.
        # this passes iff all points in the ellipses are
        # also in the hulls.
        assert_array_less(
            np.hstack([x.astype('float') for x in results1]),
            np.hstack([x.astype('float') for x in results2]) + 0.5
        )

        # check for no failures with slices.
        u.in_credible_region(test_points[:100,self.SLICE], level=0.9, method='pce', modelparam_slice=self.SLICE)
        u.in_credible_region(test_points[:100,self.SLICE], level=0.9, method='hpd-hull', modelparam_slice=self.SLICE)
        u.in_credible_region(test_points[:100,self.SLICE], level=0.9, method='hpd-mvee', modelparam_slice=self.SLICE)

        # check for no failures with single inputs
        assert(u.in_credible_region(test_points[0,:], level=0.9, method='pce').size == 1)
        assert(u.in_credible_region(test_points[0,:], level=0.9, method='hpd-hull').size == 1)
        assert(u.in_credible_region(test_points[0,:], level=0.9, method='hpd-mvee').size == 1)
 def setUp(self):
     super(TestAbstractModel, self).setUp()
     self.mock_model = MockModel()
示例#9
0
 def test_test_model_runs(self):
     model = MockModel()
     prior = UniformDistribution(np.array([[10, 12], [2, 3]]))
     eps = np.arange(10, 20).astype(model.expparams_dtype)
     test_model(model, prior, eps)
示例#10
0
 def instantiate_expparams(self):
     return np.array([(10.0, 2)], dtype=MockModel().expparams_dtype)
 def setUp(self):
     super(TestAbstractModel, self).setUp()
     self.mock_model = MockModel()