Exemplo n.º 1
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.º 2
0
def test_parameter_modify_in_init():
    class TestLikelihood(Parameterized):
        def __init__(self,
                     param1=2.,
                     param2=3.,
                     param3=np.random.uniform(size=(2, 2, 2))):

            super(TestLikelihood, self).__init__("TestLike")
            self.p1 = Param('param1', param1)
            self.p2 = Param('param2', param2)

            self.link_parameter(self.p1)
            self.link_parameter(self.p2)

            self.p1.fix()
            self.p1.unfix()

            self['.*param'].constrain_positive()

            self.p2.constrain_negative()
            self.p1.fix()
            self.p2.constrain_positive()
            self.p2.fix()
            self.p2.constrain_positive()

            self['.*param1'].unconstrain(transformations.Logexp())

    m = TestLikelihood()
    print(m)
    val = m.p1.values.copy()
    assert (m.p1.is_fixed)
    assert (m.constraints[transformations.Logexp()].tolist() == [1])
    m.randomize()
    assert (m.p1 == val)
Exemplo n.º 3
0
    def test_constraints(self):
        self.rbf.constrain(transformations.Square(), False)
        self.assertListEqual(
            self.test1.constraints[transformations.Square()].tolist(),
            list(range(self.param.size, self.param.size + self.rbf.size)))
        self.assertListEqual(
            self.test1.constraints[transformations.Logexp()].tolist(),
            [self.param.size + self.rbf.size])

        self.test1.kern.unlink_parameter(self.rbf)
        self.assertListEqual(
            self.test1.constraints[transformations.Square()].tolist(), [])

        self.test1.unconstrain_positive()
        self.assertListEqual(
            self.test1.constraints[transformations.Logexp()].tolist(), [])
Exemplo n.º 4
0
 def test_default_constraints(self):
     self.assertIs(self.rbf.variance.constraints._param_index_ops, self.rbf.constraints._param_index_ops)
     self.assertIs(self.test1.constraints, self.rbf.constraints._param_index_ops)
     self.assertListEqual(self.rbf.constraints.indices()[0].tolist(), list(range(2)))
     kern = self.test1.kern
     self.test1.unlink_parameter(kern)
     self.assertListEqual(kern.constraints[transformations.Logexp()].tolist(), list(range(3)))
Exemplo n.º 5
0
    def test_parameter_modify_in_init(self):
        class TestLikelihood(Parameterized):
            def __init__(self, param1 = 2., param2 = 3.):
                super(TestLikelihood, self).__init__("TestLike")
                self.p1 = Param('param1', param1)
                self.p2 = Param('param2', param2)

                self.link_parameter(self.p1)
                self.link_parameter(self.p2)

                self.p1.fix()
                self.p1.unfix()
                self.p2.constrain_negative()
                self.p1.fix()
                self.p2.constrain_positive()
                self.p2.fix()
                self.p2.constrain_positive()

        m = TestLikelihood()
        print(m)
        val = m.p1.values.copy()
        self.assert_(m.p1.is_fixed)
        self.assert_(m.constraints[transformations.Logexp()].tolist(), [1])
        m.randomize()
        self.assertEqual(m.p1, val)
Exemplo n.º 6
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.º 7
0
    def test_fix_unfix_constraints(self):
        self.testmodel.constrain_bounded(0, 1)
        self.testmodel['.*variance'].constrain(transformations.Logexp())
        self.testmodel['.*Gauss'].constrain_bounded(0.3, 0.7)
        before_constraints = dict(self.testmodel.constraints.items())

        self.testmodel.fix()

        test_constraints = dict(self.testmodel.constraints.items())
        for k in before_constraints:
            np.testing.assert_array_equal(before_constraints[k],
                                          test_constraints[k])
        np.testing.assert_array_equal(
            test_constraints[transformations.__fixed__], [0, 1, 2])

        # Assert fixing works and does not randomize the - say - lengthscale:
        val = float(self.testmodel.kern.lengthscale)
        self.testmodel.randomize()
        self.assertEqual(val, self.testmodel.kern.lengthscale)

        self.testmodel.unfix()

        test_constraints = dict(self.testmodel.constraints.items())
        for k in before_constraints:
            np.testing.assert_array_equal(before_constraints[k],
                                          test_constraints[k])
Exemplo n.º 8
0
 def test_hierarchy_error(self):
     self.assertRaises(HierarchyError, self.testmodel.link_parameter,
                       self.testmodel.parameters[0])
     p2 = P('Gaussian_noise',
            variance=Param('variance', np.random.uniform(0.1, 0.5),
                           transformations.Logexp()))
     self.testmodel.link_parameter(p2.variance)
     self.assertTrue(self.testmodel.checkgrad())
     self.assertRaises(HierarchyError, self.testmodel.unlink_parameter, p2)
     self.assertRaises(HierarchyError, self.testmodel.unlink_parameter,
                       'not a parameter')
Exemplo n.º 9
0
    def setUp(self):

        self.testmodel = M('testmodel')
        self.testmodel.kern = P('rbf')
        self.testmodel.likelihood = P('Gaussian_noise',
                                      variance=Param(
                                          'variance',
                                          np.random.uniform(0.1, 0.5),
                                          transformations.Logexp()))
        self.testmodel.link_parameter(self.testmodel.kern)
        self.testmodel.link_parameter(self.testmodel.likelihood)
        variance = Param('variance', np.random.uniform(0.1, 0.5),
                         transformations.Logexp())
        lengthscale = Param('lengthscale', np.random.uniform(.1, 1, 1),
                            transformations.Logexp())
        self.testmodel.kern.variance = variance
        self.testmodel.kern.lengthscale = lengthscale
        self.testmodel.kern.link_parameter(lengthscale)
        self.testmodel.kern.link_parameter(variance)
        self.testmodel.trigger_update()
Exemplo n.º 10
0
    def setUp(self):
        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 log_likelihood(self):
                return -self.objective_function()

            def parameters_changed(self):
                self._obj = (self.param_array**2).sum()
                self.gradient[:] = 2 * self.param_array

        import warnings
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            self.testmodel = M('testmodel', initialize=False)
            self.testmodel.kern = Parameterized('rbf', initialize=False)
            self.testmodel.likelihood = P('Gaussian_noise',
                                          variance=Param(
                                              'variance',
                                              np.random.uniform(0.1, 0.5),
                                              transformations.Logexp()),
                                          initialize=False)
        self.testmodel.link_parameter(self.testmodel.kern)
        self.testmodel.link_parameter(self.testmodel.likelihood)
        variance = Param('variance', np.random.uniform(0.1, 0.5),
                         transformations.Logexp())
        lengthscale = Param('lengthscale', np.random.uniform(.1, 1, 1),
                            transformations.Logexp())
        self.testmodel.kern.variance = variance
        self.testmodel.kern.lengthscale = lengthscale
        self.testmodel.kern.link_parameter(lengthscale)
        self.testmodel.kern.link_parameter(variance)
Exemplo n.º 11
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)
Exemplo n.º 12
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.º 13
0
        def __init__(self, param1=2., param2=3.):
            super(TestLikelihood, self).__init__("TestLike")
            self.p1 = Param('param1', param1)
            self.p2 = Param('param2', param2)

            self.link_parameter(self.p1)
            self.link_parameter(self.p2)

            self.p1.fix()
            self.p1.unfix()

            self['.*param'].constrain_positive()

            self.p2.constrain_negative()
            self.p1.fix()
            self.p2.constrain_positive()
            self.p2.fix()
            self.p2.constrain_positive()

            self['.*param1'].unconstrain(transformations.Logexp())
Exemplo n.º 14
0
    def test_remove_parameter(self):
        self.white.fix()
        self.test1.kern.unlink_parameter(self.white)
        self.assertIs(self.test1._fixes_, None)

        self.assertIsInstance(self.white.constraints, ParameterIndexOperations)
        self.assertListEqual(self.white._fixes_.tolist(),
                             [transformations.FIXED])
        self.assertIs(self.test1.constraints,
                      self.rbf.constraints._param_index_ops)
        self.assertIs(self.test1.constraints,
                      self.param.constraints._param_index_ops)

        self.test1.link_parameter(self.white, 0)
        self.assertIs(self.test1.constraints,
                      self.white.constraints._param_index_ops)
        self.assertIs(self.test1.constraints,
                      self.rbf.constraints._param_index_ops)
        self.assertIs(self.test1.constraints,
                      self.param.constraints._param_index_ops)
        self.assertListEqual(
            self.test1.constraints[transformations.__fixed__].tolist(), [0])
        self.assertIs(self.white._fixes_, None)
        self.assertListEqual(self.test1._fixes_.tolist(),
                             [transformations.FIXED] +
                             [transformations.UNFIXED] * 52)

        self.test1.unlink_parameter(self.white)
        self.assertIs(self.test1._fixes_, None)
        self.assertListEqual(self.white._fixes_.tolist(),
                             [transformations.FIXED])
        self.assertIs(self.test1.constraints,
                      self.rbf.constraints._param_index_ops)
        self.assertIs(self.test1.constraints,
                      self.param.constraints._param_index_ops)
        self.assertListEqual(
            self.test1.constraints[transformations.Logexp()].tolist(),
            list(range(self.param.size, self.param.size + self.rbf.size)))