2013-01-27 18:34:40 +00:00
|
|
|
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
|
|
|
|
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
import numpy as np
|
|
|
|
|
import GPy
|
|
|
|
|
|
|
|
|
|
class PriorTests(unittest.TestCase):
|
|
|
|
|
def test_lognormal(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]
|
2013-06-05 13:02:03 +01:00
|
|
|
m = GPy.models.GPRegression(X, y)
|
2013-06-04 17:38:05 +01:00
|
|
|
lognormal = GPy.priors.LogGaussian(1, 2)
|
2014-03-14 12:31:28 +00:00
|
|
|
m.rbf.set_prior(lognormal)
|
2013-01-27 18:34:40 +00:00
|
|
|
m.randomize()
|
|
|
|
|
self.assertTrue(m.checkgrad())
|
|
|
|
|
|
2013-06-04 17:38:05 +01:00
|
|
|
def test_Gamma(self):
|
2013-01-27 18:34:40 +00:00
|
|
|
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]
|
2013-06-05 13:02:03 +01:00
|
|
|
m = GPy.models.GPRegression(X, y)
|
2013-06-04 17:38:05 +01:00
|
|
|
Gamma = GPy.priors.Gamma(1, 1)
|
2014-03-14 12:31:28 +00:00
|
|
|
m.rbf.set_prior(Gamma)
|
2013-01-27 18:34:40 +00:00
|
|
|
m.randomize()
|
|
|
|
|
self.assertTrue(m.checkgrad())
|
|
|
|
|
|
|
|
|
|
def test_incompatibility(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]
|
2013-06-05 13:02:03 +01:00
|
|
|
m = GPy.models.GPRegression(X, y)
|
2013-01-27 18:34:40 +00:00
|
|
|
gaussian = GPy.priors.Gaussian(1, 1)
|
|
|
|
|
# setting a Gaussian prior on non-negative parameters
|
|
|
|
|
# should raise an assertionerror.
|
2014-03-14 12:31:28 +00:00
|
|
|
self.assertRaises(AssertionError, m.rbf.set_prior, gaussian)
|
2013-01-27 18:34:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
print "Running unit tests, please be (very) patient..."
|
|
|
|
|
unittest.main()
|