Fixed likelihood tests

This commit is contained in:
Alan Saul 2014-02-24 13:05:39 +00:00
parent 632a702532
commit 3b5a86ec84

View file

@ -10,6 +10,7 @@ from functools import partial
#np.random.seed(300) #np.random.seed(300)
#np.random.seed(7) #np.random.seed(7)
np.seterr(divide='raise')
def dparam_partial(inst_func, *args): def dparam_partial(inst_func, *args):
""" """
If we have a instance method that needs to be called but that doesn't If we have a instance method that needs to be called but that doesn't
@ -149,9 +150,9 @@ class TestNoiseModels(object):
noise_models = {"Student_t_default": { noise_models = {"Student_t_default": {
"model": GPy.likelihoods.StudentT(deg_free=5, sigma2=self.var), "model": GPy.likelihoods.StudentT(deg_free=5, sigma2=self.var),
"grad_params": { "grad_params": {
"names": ["t_noise"], "names": [".*t_noise"],
"vals": [self.var], "vals": [self.var],
"constraints": [("t_noise", constrain_positive), ("deg_free", constrain_fixed)] "constraints": [(".*t_noise", constrain_positive), (".*deg_free", constrain_fixed)]
#"constraints": [("t_noise", constrain_positive), ("deg_free", partial(constrain_fixed, value=5))] #"constraints": [("t_noise", constrain_positive), ("deg_free", partial(constrain_fixed, value=5))]
}, },
"laplace": True "laplace": True
@ -159,63 +160,63 @@ class TestNoiseModels(object):
"Student_t_1_var": { "Student_t_1_var": {
"model": GPy.likelihoods.StudentT(deg_free=5, sigma2=self.var), "model": GPy.likelihoods.StudentT(deg_free=5, sigma2=self.var),
"grad_params": { "grad_params": {
"names": ["t_noise"], "names": [".*t_noise"],
"vals": [1.0], "vals": [1.0],
"constraints": [("t_noise", constrain_positive), ("deg_free", constrain_fixed)] "constraints": [(".*t_noise", constrain_positive), (".*deg_free", constrain_fixed)]
}, },
"laplace": True "laplace": True
}, },
"Student_t_small_deg_free": { "Student_t_small_deg_free": {
"model": GPy.likelihoods.StudentT(deg_free=1.5, sigma2=self.var), "model": GPy.likelihoods.StudentT(deg_free=1.5, sigma2=self.var),
"grad_params": { "grad_params": {
"names": ["t_noise"], "names": [".*t_noise"],
"vals": [self.var], "vals": [self.var],
"constraints": [("t_noise", constrain_positive), ("deg_free", constrain_fixed)] "constraints": [(".*t_noise", constrain_positive), (".*deg_free", constrain_fixed)]
}, },
"laplace": True "laplace": True
}, },
"Student_t_small_var": { "Student_t_small_var": {
"model": GPy.likelihoods.StudentT(deg_free=5, sigma2=self.var), "model": GPy.likelihoods.StudentT(deg_free=5, sigma2=self.var),
"grad_params": { "grad_params": {
"names": ["t_noise"], "names": [".*t_noise"],
"vals": [0.0001], "vals": [0.001],
"constraints": [("t_noise", constrain_positive), ("deg_free", constrain_fixed)] "constraints": [(".*t_noise", constrain_positive), (".*deg_free", constrain_fixed)]
}, },
"laplace": True "laplace": True
}, },
"Student_t_large_var": { "Student_t_large_var": {
"model": GPy.likelihoods.StudentT(deg_free=5, sigma2=self.var), "model": GPy.likelihoods.StudentT(deg_free=5, sigma2=self.var),
"grad_params": { "grad_params": {
"names": ["t_noise"], "names": [".*t_noise"],
"vals": [10.0], "vals": [10.0],
"constraints": [("t_noise", constrain_positive), ("deg_free", constrain_fixed)] "constraints": [(".*t_noise", constrain_positive), (".*deg_free", constrain_fixed)]
}, },
"laplace": True "laplace": True
}, },
"Student_t_approx_gauss": { "Student_t_approx_gauss": {
"model": GPy.likelihoods.StudentT(deg_free=1000, sigma2=self.var), "model": GPy.likelihoods.StudentT(deg_free=1000, sigma2=self.var),
"grad_params": { "grad_params": {
"names": ["t_noise"], "names": [".*t_noise"],
"vals": [self.var], "vals": [self.var],
"constraints": [("t_noise", constrain_positive), ("deg_free", constrain_fixed)] "constraints": [(".*t_noise", constrain_positive), (".*deg_free", constrain_fixed)]
}, },
"laplace": True "laplace": True
}, },
"Student_t_log": { "Student_t_log": {
"model": GPy.likelihoods.StudentT(gp_link=link_functions.Log(), deg_free=5, sigma2=self.var), "model": GPy.likelihoods.StudentT(gp_link=link_functions.Log(), deg_free=5, sigma2=self.var),
"grad_params": { "grad_params": {
"names": ["t_noise"], "names": [".*t_noise"],
"vals": [self.var], "vals": [self.var],
"constraints": [("t_noise", constrain_positive), ("deg_free", constrain_fixed)] "constraints": [(".*t_noise", constrain_positive), (".*deg_free", constrain_fixed)]
}, },
"laplace": True "laplace": True
}, },
"Gaussian_default": { "Gaussian_default": {
"model": GPy.likelihoods.Gaussian(variance=self.var), "model": GPy.likelihoods.Gaussian(variance=self.var),
"grad_params": { "grad_params": {
"names": ["variance"], "names": [".*variance"],
"vals": [self.var], "vals": [self.var],
"constraints": [("variance", constrain_positive)] "constraints": [(".*variance", constrain_positive)]
}, },
"laplace": True, "laplace": True,
"ep": False # FIXME: Should be True when we have it working again "ep": False # FIXME: Should be True when we have it working again
@ -515,10 +516,10 @@ class TestNoiseModels(object):
#Normalize #Normalize
Y = Y/Y.max() Y = Y/Y.max()
white_var = 1e-6 white_var = 1e-6
kernel = GPy.kern.rbf(X.shape[1]) + GPy.kern.white(X.shape[1]) kernel = GPy.kern.RBF(X.shape[1]) + GPy.kern.White(X.shape[1])
laplace_likelihood = GPy.inference.latent_function_inference.Laplace() laplace_likelihood = GPy.inference.latent_function_inference.Laplace()
m = GPy.core.GP(X.copy(), Y.copy(), kernel, likelihood=model, inference_method=laplace_likelihood) m = GPy.core.GP(X.copy(), Y.copy(), kernel, likelihood=model, inference_method=laplace_likelihood)
m['white'].constrain_fixed(white_var) m['.*white'].constrain_fixed(white_var)
#Set constraints #Set constraints
for constrain_param, constraint in constraints: for constrain_param, constraint in constraints:
@ -552,10 +553,10 @@ class TestNoiseModels(object):
#Normalize #Normalize
Y = Y/Y.max() Y = Y/Y.max()
white_var = 1e-6 white_var = 1e-6
kernel = GPy.kern.rbf(X.shape[1]) + GPy.kern.white(X.shape[1]) kernel = GPy.kern.RBF(X.shape[1]) + GPy.kern.White(X.shape[1])
ep_inf = GPy.inference.latent_function_inference.EP() ep_inf = GPy.inference.latent_function_inference.EP()
m = GPy.core.GP(X.copy(), Y.copy(), kernel=kernel, likelihood=model, inference_method=ep_inf) m = GPy.core.GP(X.copy(), Y.copy(), kernel=kernel, likelihood=model, inference_method=ep_inf)
m['white'].constrain_fixed(white_var) m['.*white'].constrain_fixed(white_var)
for param_num in range(len(param_names)): for param_num in range(len(param_names)):
name = param_names[param_num] name = param_names[param_num]
@ -631,26 +632,24 @@ class LaplaceTests(unittest.TestCase):
Y = Y/Y.max() Y = Y/Y.max()
#Yc = Y.copy() #Yc = Y.copy()
#Yc[75:80] += 1 #Yc[75:80] += 1
kernel1 = GPy.kern.rbf(X.shape[1]) + GPy.kern.white(X.shape[1]) kernel1 = GPy.kern.RBF(X.shape[1]) + GPy.kern.White(X.shape[1])
#FIXME: Make sure you can copy kernels when params is fixed #FIXME: Make sure you can copy kernels when params is fixed
#kernel2 = kernel1.copy() #kernel2 = kernel1.copy()
kernel2 = GPy.kern.rbf(X.shape[1]) + GPy.kern.white(X.shape[1]) kernel2 = GPy.kern.RBF(X.shape[1]) + GPy.kern.White(X.shape[1])
gauss_distr1 = GPy.likelihoods.Gaussian(variance=initial_var_guess) gauss_distr1 = GPy.likelihoods.Gaussian(variance=initial_var_guess)
exact_inf = GPy.inference.latent_function_inference.ExactGaussianInference() exact_inf = GPy.inference.latent_function_inference.ExactGaussianInference()
m1 = GPy.core.GP(X, Y.copy(), kernel=kernel1, likelihood=gauss_distr1, inference_method=exact_inf) m1 = GPy.core.GP(X, Y.copy(), kernel=kernel1, likelihood=gauss_distr1, inference_method=exact_inf)
m1['white'].constrain_fixed(1e-6) m1['.*white'].constrain_fixed(1e-6)
m1['variance'] = initial_var_guess m1['.*rbf.variance'] = initial_var_guess
m1['variance'].constrain_bounded(1e-4, 10) m1['.*rbf.variance'].constrain_bounded(1e-4, 10)
m1['rbf'].constrain_bounded(1e-4, 10)
m1.randomize() m1.randomize()
gauss_distr2 = GPy.likelihoods.Gaussian(variance=initial_var_guess) gauss_distr2 = GPy.likelihoods.Gaussian(variance=initial_var_guess)
laplace_inf = GPy.inference.latent_function_inference.Laplace() laplace_inf = GPy.inference.latent_function_inference.Laplace()
m2 = GPy.core.GP(X, Y.copy(), kernel=kernel2, likelihood=gauss_distr2, inference_method=laplace_inf) m2 = GPy.core.GP(X, Y.copy(), kernel=kernel2, likelihood=gauss_distr2, inference_method=laplace_inf)
m2['white'].constrain_fixed(1e-6) m2['.*white'].constrain_fixed(1e-6)
m2['rbf'].constrain_bounded(1e-4, 10) m2['.*rbf.variance'].constrain_bounded(1e-4, 10)
m2['variance'].constrain_bounded(1e-4, 10)
m2.randomize() m2.randomize()
if debug: if debug: