Exemplo n.º 1
0
    def test_constraints_testmodel(self):
        self.testmodel['.*rbf'].constrain_negative()
        self.assertListEqual(
            self.testmodel.constraints[
                transformations.NegativeLogexp()].tolist(), [0, 1])

        self.testmodel['.*lengthscale'].constrain_bounded(0, 1)
        self.assertListEqual(
            self.testmodel.constraints[
                transformations.NegativeLogexp()].tolist(), [1])
        self.assertListEqual(
            self.testmodel.constraints[transformations.Logistic(0,
                                                                1)].tolist(),
            [0])

        self.testmodel[''].unconstrain_negative()
        self.assertListEqual(
            self.testmodel.constraints[
                transformations.NegativeLogexp()].tolist(), [])
        self.assertListEqual(
            self.testmodel.constraints[transformations.Logistic(0,
                                                                1)].tolist(),
            [0])

        self.testmodel['.*lengthscale'].unconstrain_bounded(0, 1)
        self.assertListEqual(
            self.testmodel.constraints[transformations.Logistic(0,
                                                                1)].tolist(),
            [])
Exemplo n.º 2
0
    def test_constraints_set_direct(self):
        self.testmodel['.*rbf'].constrain_negative()
        self.testmodel['.*lengthscale'].constrain_bounded(0, 1)
        self.testmodel['.*variance'].fix()

        self.assertListEqual(
            self.testmodel.constraints[transformations.__fixed__].tolist(),
            [1, 2])
        self.assertListEqual(
            self.testmodel.constraints[transformations.Logistic(0,
                                                                1)].tolist(),
            [0])
        self.assertListEqual(
            self.testmodel.constraints[
                transformations.NegativeLogexp()].tolist(), [1])

        cache_constraints = self.testmodel.constraints.copy()

        self.testmodel.unconstrain()
        self.testmodel.likelihood.fix()

        self.assertListEqual(self.testmodel._fixes_.tolist(), [
            transformations.UNFIXED, transformations.UNFIXED,
            transformations.FIXED
        ])

        self.assertListEqual(
            self.testmodel.constraints[transformations.__fixed__].tolist(),
            [2])
        self.assertListEqual(
            self.testmodel.constraints[transformations.Logistic(0,
                                                                1)].tolist(),
            [])
        self.assertListEqual(
            self.testmodel.constraints[
                transformations.NegativeLogexp()].tolist(), [])

        self.testmodel.constraints = cache_constraints
        self.assertListEqual(
            self.testmodel.constraints[transformations.__fixed__].tolist(),
            [1, 2])
        self.assertListEqual(
            self.testmodel.constraints[transformations.Logistic(0,
                                                                1)].tolist(),
            [0])
        self.assertListEqual(
            self.testmodel.constraints[
                transformations.NegativeLogexp()].tolist(), [1])

        self.assertListEqual(self.testmodel._fixes_.tolist(), [
            transformations.UNFIXED, transformations.FIXED,
            transformations.FIXED
        ])

        self.assertIs(self.testmodel.constraints,
                      self.testmodel.likelihood.constraints._param_index_ops)
        self.assertIs(self.testmodel.constraints,
                      self.testmodel.kern.constraints._param_index_ops)
Exemplo n.º 3
0
 def test_constraints_in_init(self):
     class Test(Parameterized):
         def __init__(self, name=None, parameters=[], *a, **kw):
             super(Test, self).__init__(name=name)
             self.x = Param('x', np.random.uniform(0,1,(3,4)))
             self.x[0].constrain_bounded(0,1)
             self.link_parameter(self.x)
             self.x[1].fix()
     t = Test()
     c = {transformations.Logistic(0,1): np.array([0, 1, 2, 3]), 'fixed': np.array([4, 5, 6, 7])}
     np.testing.assert_equal(t.x.constraints[transformations.Logistic(0,1)], c[transformations.Logistic(0,1)])
     np.testing.assert_equal(t.x.constraints['fixed'], c['fixed'])
Exemplo n.º 4
0
    def test_fix_constrain(self):
        # save the constraints as they where:
        before_constraints = dict(self.testmodel.constraints.items())
        # fix
        self.testmodel.fix()

        test_constraints = dict(self.testmodel.constraints.items())
        # make sure fixes are in place:
        np.testing.assert_array_equal(
            test_constraints[transformations.__fixed__], [0, 1, 2])
        # make sure, the constraints still exist
        for k in before_constraints:
            np.testing.assert_array_equal(before_constraints[k],
                                          test_constraints[k])

        # override fix and previous constraint:
        self.testmodel.likelihood.constrain_bounded(0, 1)
        # lik not fixed anymore
        np.testing.assert_array_equal(
            self.testmodel.constraints[transformations.__fixed__], [0, 1])
        # previous constraints still in place:
        np.testing.assert_array_equal(
            self.testmodel.constraints[transformations.Logexp()], [0, 1])
        # lik bounded
        np.testing.assert_array_equal(
            self.testmodel.constraints[transformations.Logistic(0, 1)], [2])
Exemplo n.º 5
0
    def setUp(self):
        class P(Parameterized):
            def __init__(self, name, **kwargs):
                super(P, self).__init__(name=name)
                for k, val in kwargs.items():
                    self.__setattr__(k, val)
                    self.link_parameter(self.__getattribute__(k))
            def parameters_changed(self):
                self.gradient[:] = 1.
                
        self.rbf = Parameterized('rbf')
        self.rbf.lengthscale = Param('lengthscale', np.random.uniform(.1, 1), transformations.Logexp())
        self.rbf.variance = Param('variance', np.random.uniform(0.1, 0.5), transformations.Logexp()) 
        self.rbf.link_parameters(self.rbf.variance, self.rbf.lengthscale)
        
        self.white = P('white', variance=Param('variance', np.random.uniform(0.1, 0.5), transformations.Logexp()))
        self.param = Param('param', np.random.uniform(0,1,(10,5)), transformations.Logistic(0, 1))

        self.test1 = Parameterized('test_parameterized')

        self.test1.param = self.param
        self.test1.kern = Parameterized('add')
        self.test1.kern.link_parameters(self.rbf, self.white)
        
        self.test1.link_parameter(self.test1.kern)
        self.test1.link_parameter(self.param, 0)

        # print self.test1:
        #=============================================================================
        # test_model.          |    Value    |  Constraint   |  Prior  |  Tied to
        # param                |  (25L, 2L)  |   {0.0,1.0}   |         |
        # add.rbf.variance     |        1.0  |  0.0,1.0 +ve  |         |
        # add.rbf.lengthscale  |        1.0  |  0.0,1.0 +ve  |         |
        # add.white.variance   |        1.0  |  0.0,1.0 +ve  |         |
        #=============================================================================

        class M(Model):
            def __init__(self, name, **kwargs):
                super(M, self).__init__(name=name)
                for k, val in kwargs.items():
                    self.__setattr__(k, val)
                    self.link_parameter(self.__getattribute__(k))
            def objective_function(self):
                return self._obj
            def parameters_changed(self):
                self._obj = self.param_array.sum()
        
        self.testmodel = M('testmodel', 
                           kern=P('rbf', 
                                 variance=Param('variance', np.random.uniform(0.1, 0.5), transformations.Logexp()), 
                                 lengthscale=Param('lengthscale', np.random.uniform(.1, 1, 1), transformations.Logexp())),
                           likelihood=P('Gaussian_noise',
                                        variance=Param('variance', np.random.uniform(0.1, 0.5), transformations.Logexp()))
                           )
Exemplo n.º 6
0
 def test_add_parameter_in_hierarchy(self):
     self.test1.kern.rbf.link_parameter(
         Param("NEW", np.random.rand(2), transformations.NegativeLogexp()),
         1)
     self.assertListEqual(
         self.test1.constraints[transformations.NegativeLogexp()].tolist(),
         list(range(self.param.size + 1, self.param.size + 1 + 2)))
     self.assertListEqual(
         self.test1.constraints[transformations.Logistic(0, 1)].tolist(),
         list(range(self.param.size)))
     self.assertListEqual(
         self.test1.constraints[transformations.Logexp(0, 1)].tolist(),
         np.r_[50, 53:55].tolist())
Exemplo n.º 7
0
    def setUp(self):
        self.rbf = Parameterized('rbf')
        self.rbf.lengthscale = Param('lengthscale', np.random.uniform(.1, 1),
                                     transformations.Logexp())
        self.rbf.variance = Param('variance', np.random.uniform(0.1, 0.5),
                                  transformations.Logexp())
        self.rbf.link_parameters(self.rbf.variance, self.rbf.lengthscale)

        self.white = P('white',
                       variance=Param('variance', np.random.uniform(0.1, 0.5),
                                      transformations.Logexp()))
        self.param = Param('param', np.random.uniform(0, 1, (10, 5)),
                           transformations.Logistic(0, 1))

        self.test1 = Parameterized('test_parameterized')

        self.test1.param = self.param
        self.test1.kern = Parameterized('add')
        self.test1.kern.link_parameters(self.rbf, self.white)

        self.test1.link_parameter(self.test1.kern)
        self.test1.link_parameter(self.param, 0)