diff --git a/GPy/core/parameterization/parameter_core.py b/GPy/core/parameterization/parameter_core.py index 115acf3a..fb6b3a8c 100644 --- a/GPy/core/parameterization/parameter_core.py +++ b/GPy/core/parameterization/parameter_core.py @@ -518,7 +518,7 @@ class Indexable(Nameable, Observable): self.constrain_negative(warning) elif prior.domain is _REAL: rav_i = self._raveled_index() - assert all(all(c.domain is _REAL for c in con) for con in self.constraints.properties_for(rav_i)) + assert all(all(False if c is __fixed__ else c.domain is _REAL for c in con) for con in self.constraints.properties_for(rav_i)), 'Domain of prior and constraint have to match, please unconstrain if you REALLY wish to use this prior' def unset_priors(self, *priors): """ @@ -824,7 +824,7 @@ class OptimizationHandlable(Indexable): #=========================================================================== # Randomizeable #=========================================================================== - def randomize(self, rand_gen=np.random.normal, *args, **kwargs): + def randomize(self, rand_gen=None, *args, **kwargs): """ Randomize the model. Make this draw from the prior if one exists, else draw from given random generator @@ -834,6 +834,8 @@ class OptimizationHandlable(Indexable): :param float scale: scale parameter for random number generator :param args, kwargs: will be passed through to random number generator """ + if rand_gen is None: + rand_gen = np.random.normal # first take care of all parameters (from N(0,1)) x = rand_gen(size=self._size_transformed(), *args, **kwargs) updates = self.update_model() diff --git a/GPy/testing/prior_tests.py b/GPy/testing/prior_tests.py index db6cc685..6a61fbb5 100644 --- a/GPy/testing/prior_tests.py +++ b/GPy/testing/prior_tests.py @@ -45,6 +45,69 @@ class PriorTests(unittest.TestCase): # should raise an assertionerror. self.assertRaises(AssertionError, m.rbf.set_prior, gaussian) + def test_set_prior(self): + xmin, xmax = 1, 2.5*np.pi + b, C, SNR = 1, 0, 0.1 + X = np.linspace(xmin, xmax, 500) + y = b*X + C + 1*np.sin(X) + y += 0.05*np.random.randn(len(X)) + X, y = X[:, None], y[:, None] + m = GPy.models.GPRegression(X, y) + + gaussian = GPy.priors.Gaussian(1, 1) + #m.rbf.set_prior(gaussian) + # setting a Gaussian prior on non-negative parameters + # should raise an assertionerror. + self.assertRaises(AssertionError, m.rbf.set_prior, gaussian) + + def test_set_gaussian_for_reals(self): + xmin, xmax = 1, 2.5*np.pi + b, C, SNR = 1, 0, 0.1 + X = np.linspace(xmin, xmax, 500) + y = b*X + C + 1*np.sin(X) + y += 0.05*np.random.randn(len(X)) + X, y = X[:, None], y[:, None] + m = GPy.models.SparseGPRegression(X, y) + + gaussian = GPy.priors.Gaussian(1, 1) + m.Z.set_prior(gaussian) + # setting a Gaussian prior on non-negative parameters + # should raise an assertionerror. + #self.assertRaises(AssertionError, m.Z.set_prior, gaussian) + + + + def test_fixed_domain_check(self): + xmin, xmax = 1, 2.5*np.pi + b, C, SNR = 1, 0, 0.1 + X = np.linspace(xmin, xmax, 500) + y = b*X + C + 1*np.sin(X) + y += 0.05*np.random.randn(len(X)) + X, y = X[:, None], y[:, None] + m = GPy.models.GPRegression(X, y) + + m.rbf.fix() + gaussian = GPy.priors.Gaussian(1, 1) + # setting a Gaussian prior on non-negative parameters + # should raise an assertionerror. + self.assertRaises(AssertionError, m.rbf.set_prior, gaussian) + + def test_fixed_domain_check1(self): + xmin, xmax = 1, 2.5*np.pi + b, C, SNR = 1, 0, 0.1 + X = np.linspace(xmin, xmax, 500) + y = b*X + C + 1*np.sin(X) + y += 0.05*np.random.randn(len(X)) + X, y = X[:, None], y[:, None] + m = GPy.models.GPRegression(X, y) + + m.kern.lengthscale.fix() + gaussian = GPy.priors.Gaussian(1, 1) + # setting a Gaussian prior on non-negative parameters + # should raise an assertionerror. + self.assertRaises(AssertionError, m.rbf.set_prior, gaussian) + + if __name__ == "__main__": print "Running unit tests, please be (very) patient..."