Fixed step size for likelihood tests and allowed randomizing of laplace

This commit is contained in:
Alan Saul 2013-11-27 13:21:11 +00:00
parent 0f60fba125
commit f59125d4a1

View file

@ -6,6 +6,8 @@ import functools
import inspect import inspect
from GPy.likelihoods.noise_models import gp_transformations from GPy.likelihoods.noise_models import gp_transformations
from functools import partial from functools import partial
#np.random.seed(300)
np.random.seed(690)
def dparam_partial(inst_func, *args): def dparam_partial(inst_func, *args):
""" """
@ -144,7 +146,7 @@ class TestNoiseModels(object):
"model": GPy.likelihoods.student_t(deg_free=5, sigma2=self.var), "model": GPy.likelihoods.student_t(deg_free=5, sigma2=self.var),
"grad_params": { "grad_params": {
"names": ["t_noise"], "names": ["t_noise"],
"vals": [1], "vals": [1.0],
"constraints": [constrain_positive] "constraints": [constrain_positive]
}, },
"laplace": True "laplace": True
@ -158,6 +160,15 @@ class TestNoiseModels(object):
}, },
"laplace": True "laplace": True
}, },
"Student_t_large_var": {
"model": GPy.likelihoods.student_t(deg_free=5, sigma2=self.var),
"grad_params": {
"names": ["t_noise"],
"vals": [10.0],
"constraints": [constrain_positive]
},
"laplace": True
},
"Student_t_approx_gauss": { "Student_t_approx_gauss": {
"model": GPy.likelihoods.student_t(deg_free=1000, sigma2=self.var), "model": GPy.likelihoods.student_t(deg_free=1000, sigma2=self.var),
"grad_params": { "grad_params": {
@ -315,9 +326,11 @@ class TestNoiseModels(object):
def t_logpdf(self, model, Y, f): def t_logpdf(self, model, Y, f):
print "\n{}".format(inspect.stack()[0][3]) print "\n{}".format(inspect.stack()[0][3])
print model print model
print model._get_params()
np.testing.assert_almost_equal( np.testing.assert_almost_equal(
np.log(model.pdf(f.copy(), Y.copy())), model.pdf(f.copy(), Y.copy()),
model.logpdf(f.copy(), Y.copy())) np.exp(model.logpdf(f.copy(), Y.copy()))
)
@with_setup(setUp, tearDown) @with_setup(setUp, tearDown)
def t_dlogpdf_df(self, model, Y, f): def t_dlogpdf_df(self, model, Y, f):
@ -363,7 +376,7 @@ class TestNoiseModels(object):
assert ( assert (
dparam_checkgrad(model.logpdf, model.dlogpdf_dtheta, dparam_checkgrad(model.logpdf, model.dlogpdf_dtheta,
params, args=(f, Y), constraints=param_constraints, params, args=(f, Y), constraints=param_constraints,
randomize=False, verbose=True) randomize=True, verbose=True)
) )
@with_setup(setUp, tearDown) @with_setup(setUp, tearDown)
@ -373,7 +386,7 @@ class TestNoiseModels(object):
assert ( assert (
dparam_checkgrad(model.dlogpdf_df, model.dlogpdf_df_dtheta, dparam_checkgrad(model.dlogpdf_df, model.dlogpdf_df_dtheta,
params, args=(f, Y), constraints=param_constraints, params, args=(f, Y), constraints=param_constraints,
randomize=False, verbose=True) randomize=True, verbose=True)
) )
@with_setup(setUp, tearDown) @with_setup(setUp, tearDown)
@ -383,7 +396,7 @@ class TestNoiseModels(object):
assert ( assert (
dparam_checkgrad(model.d2logpdf_df2, model.d2logpdf_df2_dtheta, dparam_checkgrad(model.d2logpdf_df2, model.d2logpdf_df2_dtheta,
params, args=(f, Y), constraints=param_constraints, params, args=(f, Y), constraints=param_constraints,
randomize=False, verbose=True) randomize=True, verbose=True)
) )
################ ################
@ -478,7 +491,7 @@ class TestNoiseModels(object):
print "\n{}".format(inspect.stack()[0][3]) print "\n{}".format(inspect.stack()[0][3])
#Normalize #Normalize
Y = Y/Y.max() Y = Y/Y.max()
white_var = 0.001 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.likelihoods.Laplace(Y.copy(), model) laplace_likelihood = GPy.likelihoods.Laplace(Y.copy(), model)
m = GPy.models.GPRegression(X.copy(), Y.copy(), kernel, likelihood=laplace_likelihood) m = GPy.models.GPRegression(X.copy(), Y.copy(), kernel, likelihood=laplace_likelihood)
@ -490,12 +503,13 @@ class TestNoiseModels(object):
m[name] = param_vals[param_num] m[name] = param_vals[param_num]
constraints[param_num](name, m) constraints[param_num](name, m)
print m
m.randomize() m.randomize()
m.optimize(max_iters=8) #m.optimize(max_iters=8)
print m print m
m.checkgrad(verbose=1, step=step) m.checkgrad(verbose=1, step=step)
if not m.checkgrad(step=step): #if not m.checkgrad(step=step):
m.checkgrad(verbose=1, step=step) #m.checkgrad(verbose=1, step=step)
#import ipdb; ipdb.set_trace() #import ipdb; ipdb.set_trace()
#NOTE this test appears to be stochastic for some likelihoods (student t?) #NOTE this test appears to be stochastic for some likelihoods (student t?)
# appears to all be working in test mode right now... # appears to all be working in test mode right now...
@ -509,7 +523,7 @@ class TestNoiseModels(object):
print "\n{}".format(inspect.stack()[0][3]) print "\n{}".format(inspect.stack()[0][3])
#Normalize #Normalize
Y = Y/Y.max() Y = Y/Y.max()
white_var = 0.001 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_likelihood = GPy.likelihoods.EP(Y.copy(), model) ep_likelihood = GPy.likelihoods.EP(Y.copy(), model)
m = GPy.models.GPRegression(X.copy(), Y.copy(), kernel, likelihood=ep_likelihood) m = GPy.models.GPRegression(X.copy(), Y.copy(), kernel, likelihood=ep_likelihood)