def compare_against_mmd_test(): data = loadmat("../data/02-solar.mat") X = data["X"] y = data["y"] X_train, y_train, X_test, y_test, N, N_test = prepare_dataset(X, y) kernel = RBF(input_dim=1, variance=0.608, lengthscale=0.207) m = GPRegression(X_train, y_train, kernel, noise_var=0.283) m.optimize() pred_mean, pred_std = m.predict(X_test) s = GaussianQuadraticTest(None) gradients = compute_gp_regression_gradients(y_test, pred_mean, pred_std) U_matrix, stat = s.get_statistic_multiple_custom_gradient(y_test[:, 0], gradients[:, 0]) num_test_samples = 10000 null_samples = bootstrap_null(U_matrix, num_bootstrap=num_test_samples) # null_samples = sample_null_simulated_gp(s, pred_mean, pred_std, num_test_samples) p_value_ours = 1.0 - np.mean(null_samples <= stat) y_rep = np.random.randn(len(X_test)) * pred_std.flatten() + pred_mean.flatten() y_rep = np.atleast_2d(y_rep).T A = np.hstack((X_test, y_test)) B = np.hstack((X_test, y_rep)) feats_p = RealFeatures(A.T) feats_q = RealFeatures(B.T) width = 1 kernel = GaussianKernel(10, width) mmd = QuadraticTimeMMD() mmd.set_kernel(kernel) mmd.set_p(feats_p) mmd.set_q(feats_q) mmd_stat = mmd.compute_statistic() # sample from null num_null_samples = 10000 mmd_null_samples = np.zeros(num_null_samples) for i in range(num_null_samples): # fix y_rep from above, and change the other one (that would replace y_test) y_rep2 = np.random.randn(len(X_test)) * pred_std.flatten() + pred_mean.flatten() y_rep2 = np.atleast_2d(y_rep2).T A = np.hstack((X_test, y_rep2)) feats_p = RealFeatures(A.T) width = 1 kernel = GaussianKernel(10, width) mmd = QuadraticTimeMMD() mmd.set_kernel(kernel) mmd.set_p(feats_p) mmd.set_q(feats_q) mmd_null_samples[i] = mmd.compute_statistic() p_value_mmd = 1.0 - np.mean(mmd_null_samples <= mmd_stat) return p_value_ours, p_value_mmd
m.optimize() res = 100 pred_mean, pred_std = m.predict(X_test) plt.plot(X_test, pred_mean, 'b-') plt.plot(X_test, pred_mean + 2 * pred_std, 'b--') plt.plot(X_test, pred_mean - 2 * pred_std, 'b--') plt.plot(X_train, y_train, 'b.', markersize=3) plt.plot(X_test, y_test, 'r.', markersize=5) plt.grid(True) plt.xlabel(r"$X$") plt.ylabel(r"$y$") plt.savefig("gp_regression_data_fit.eps", bbox_inches='tight') plt.show() s = GaussianQuadraticTest(None) gradients = compute_gp_regression_gradients(y_test, pred_mean, pred_std) U_matrix, stat = s.get_statistic_multiple_custom_gradient(y_test[:, 0], gradients[:, 0]) num_test_samples = 10000 null_samples = bootstrap_null(U_matrix, num_bootstrap=num_test_samples) sns.distplot(null_samples, kde=False, norm_hist=True) plt.plot([stat, stat], [0, .012], 'black') plt.legend([r"$V_n$ test", r"Bootstrapped $B_n$"]) plt.xlabel(r"$V_n$") plt.ylabel(r"Frequency") plt.savefig("gp_regression_bootstrap_hist.eps", bbox_inches='tight') plt.show()