diff --git a/GPy/examples/non_gaussian.py b/GPy/examples/non_gaussian.py index 622b3edd..620efc5f 100644 --- a/GPy/examples/non_gaussian.py +++ b/GPy/examples/non_gaussian.py @@ -83,13 +83,6 @@ def student_t_approx(optimize=True, plot=True): print "Corrupt student t" m4.optimize(optimizer, messages=1) - if False: - print m1 - print m3 - plt.figure(3) - plt.scatter(X, m1.likelihood.Y, c='g') - plt.scatter(X, m3.likelihood.Y, c='r') - if plot: plt.figure(1) plt.suptitle('Gaussian likelihood') diff --git a/GPy/examples/stochastic.py b/GPy/examples/stochastic.py index 21011901..73daef36 100644 --- a/GPy/examples/stochastic.py +++ b/GPy/examples/stochastic.py @@ -32,10 +32,3 @@ def toy_1d(): m.plot_traces() return m - - - - - - - diff --git a/GPy/likelihoods/laplace.py b/GPy/likelihoods/laplace.py index e5dcdd19..0def0c8b 100644 --- a/GPy/likelihoods/laplace.py +++ b/GPy/likelihoods/laplace.py @@ -209,7 +209,9 @@ class Laplace(likelihood): + 0.5*ln_det_Wi_K - 0.5*self.f_Ki_f + 0.5*y_Wi_K_i_y + + self.NORMAL_CONST ) + #Convert to float as its (1, 1) and Z must be a scalar self.Z = np.float64(Z_tilde) self.Y = Y_tilde @@ -271,7 +273,7 @@ class Laplace(likelihood): :returns: (W12BiW12, ln_B_det) """ if not self.noise_model.log_concave: - #print "Under 1e-10: {}".format(np.sum(W < 1e-10)) + #print "Under 1e-10: {}".format(np.sum(W < 1e-6)) W[W < 1e-6] = 1e-6 # FIXME-HACK: This is a hack since GPy can't handle negative variances which can occur # If the likelihood is non-log-concave. We wan't to say that there is a negative variance # To cause the posterior to become less certain than the prior and likelihood, @@ -281,10 +283,7 @@ class Laplace(likelihood): #W is diagonal so its sqrt is just the sqrt of the diagonal elements W_12 = np.sqrt(W) B = np.eye(self.N) + W_12*K*W_12.T - try: - L = jitchol(B) - except: - import ipdb; ipdb.set_trace() + L = jitchol(B) W12BiW12a = W_12*dpotrs(L, np.asfortranarray(W_12*a), lower=1)[0] ln_B_det = 2*np.sum(np.log(np.diag(L))) diff --git a/GPy/testing/likelihoods_tests.py b/GPy/testing/likelihoods_tests.py index 9b7b7eb6..58c9a64b 100644 --- a/GPy/testing/likelihoods_tests.py +++ b/GPy/testing/likelihoods_tests.py @@ -593,6 +593,95 @@ class LaplaceTests(unittest.TestCase): grad.checkgrad(verbose=1) self.assertTrue(grad.checkgrad()) + #@unittest.skip('Not working yet, needs to be checked') + def test_laplace_log_likelihood(self): + debug = False + real_std = 0.1 + initial_var_guess = 0.5 + + #Start a function, any function + X = np.linspace(0.0, np.pi*2, 100)[:, None] + Y = np.sin(X) + np.random.randn(*X.shape)*real_std + Y = Y/Y.max() + #Yc = Y.copy() + #Yc[75:80] += 1 + kernel1 = GPy.kern.rbf(X.shape[1]) + GPy.kern.white(X.shape[1]) + kernel2 = kernel1.copy() + + m1 = GPy.models.GPRegression(X, Y.copy(), kernel=kernel1) + m1.constrain_fixed('white', 1e-6) + m1['noise'] = initial_var_guess + m1.constrain_bounded('noise', 1e-4, 10) + m1.constrain_bounded('rbf', 1e-4, 10) + m1.ensure_default_constraints() + m1.randomize() + + gauss_distr = GPy.likelihoods.gaussian(variance=initial_var_guess, D=1, N=Y.shape[0]) + laplace_likelihood = GPy.likelihoods.Laplace(Y.copy(), gauss_distr) + m2 = GPy.models.GPRegression(X, Y.copy(), kernel=kernel2, likelihood=laplace_likelihood) + m2.ensure_default_constraints() + m2.constrain_fixed('white', 1e-6) + m2.constrain_bounded('rbf', 1e-4, 10) + m2.constrain_bounded('noise', 1e-4, 10) + m2.randomize() + + if debug: + print m1 + print m2 + optimizer = 'scg' + print "Gaussian" + m1.optimize(optimizer, messages=debug) + print "Laplace Gaussian" + m2.optimize(optimizer, messages=debug) + if debug: + print m1 + print m2 + + m2._set_params(m1._get_params()) + + #Predict for training points to get posterior mean and variance + post_mean, post_var, _, _ = m1.predict(X) + post_mean_approx, post_var_approx, _, _ = m2.predict(X) + + if debug: + import pylab as pb + pb.figure(5) + pb.title('posterior means') + pb.scatter(X, post_mean, c='g') + pb.scatter(X, post_mean_approx, c='r', marker='x') + + pb.figure(6) + pb.title('plot_f') + m1.plot_f(fignum=6) + m2.plot_f(fignum=6) + fig, axes = pb.subplots(2, 1) + fig.suptitle('Covariance matricies') + a1 = pb.subplot(121) + a1.matshow(m1.likelihood.covariance_matrix) + a2 = pb.subplot(122) + a2.matshow(m2.likelihood.covariance_matrix) + + pb.figure(8) + pb.scatter(X, m1.likelihood.Y, c='g') + pb.scatter(X, m2.likelihood.Y, c='r', marker='x') + + + + #Check Y's are the same + np.testing.assert_almost_equal(Y, m2.likelihood.Y, decimal=5) + #Check marginals are the same + np.testing.assert_almost_equal(m1.log_likelihood(), m2.log_likelihood(), decimal=2) + #Check marginals are the same with random + m1.randomize() + m2._set_params(m1._get_params()) + np.testing.assert_almost_equal(m1.log_likelihood(), m2.log_likelihood(), decimal=2) + + #Check they are checkgradding + #m1.checkgrad(verbose=1) + #m2.checkgrad(verbose=1) + self.assertTrue(m1.checkgrad()) + self.assertTrue(m2.checkgrad()) + if __name__ == "__main__": print "Running unit tests" unittest.main()