@Test(expected = Test.None.class) public void testLongley() throws Exception { System.out.println("longley"); MathEx.setSeed(19650218); double[][] longley = MathEx.clone(Longley.x); MathEx.standardize(longley); KernelMachine<double[]> model = GaussianProcessRegression.fit(longley, Longley.y, new GaussianKernel(8.0), 0.2); System.out.println(model); double[] prediction = LOOCV.regression(longley, Longley.y, (xi, yi) -> GaussianProcessRegression.fit(xi, yi, new GaussianKernel(8.0), 0.2)); double rmse = RMSE.of(Longley.y, prediction); System.out.println("RMSE = " + rmse); assertEquals(3.978109808216234, rmse, 1E-4); java.nio.file.Path temp = smile.data.Serialize.write(model); smile.data.Serialize.read(temp); }
public static <T> KernelMachine<T> fit(T[] x, double[] y, MercerKernel<T> kernel, double lambda) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length)); } if (lambda < 0.0) { throw new IllegalArgumentException("Invalid regularization parameter lambda = " + lambda); } int n = x.length; Matrix K = new Matrix(n, n); K.uplo(UPLO.LOWER); for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { double k = kernel.k(x[i], x[j]); K.set(i, j, k); K.set(j, i, k); } K.add(i, i, lambda); } Matrix.Cholesky cholesky = K.cholesky(true); double[] w = cholesky.solve(y); return new KernelMachine<>(kernel, x, w); }
GaussianProcessRegression { public static <T> KernelMachine<T> fit(T[] x, double[] y, MercerKernel<T> kernel, double lambda) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length)); } if (lambda < 0.0) { throw new IllegalArgumentException("Invalid regularization parameter lambda = " + lambda); } int n = x.length; Matrix K = new Matrix(n, n); K.uplo(UPLO.LOWER); for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { double k = kernel.k(x[i], x[j]); K.set(i, j, k); K.set(j, i, k); } K.add(i, i, lambda); } Matrix.Cholesky cholesky = K.cholesky(true); double[] w = cholesky.solve(y); return new KernelMachine<>(kernel, x, w); } }
GaussianProcessRegression { public static <T> KernelMachine<T> fit(T[] x, double[] y, MercerKernel<T> kernel, double lambda) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length)); } if (lambda < 0.0) { throw new IllegalArgumentException("Invalid regularization parameter lambda = " + lambda); } int n = x.length; Matrix K = new Matrix(n, n); K.uplo(UPLO.LOWER); for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { double k = kernel.k(x[i], x[j]); K.set(i, j, k); K.set(j, i, k); } K.add(i, i, lambda); } Matrix.Cholesky cholesky = K.cholesky(true); double[] w = cholesky.solve(y); return new KernelMachine<>(kernel, x, w); }  }
GaussianProcessRegression { public static <T> KernelMachine<T> fit(T[] x, double[] y, MercerKernel<T> kernel, double lambda) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length)); } if (lambda < 0.0) { throw new IllegalArgumentException("Invalid regularization parameter lambda = " + lambda); } int n = x.length; Matrix K = new Matrix(n, n); K.uplo(UPLO.LOWER); for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { double k = kernel.k(x[i], x[j]); K.set(i, j, k); K.set(j, i, k); } K.add(i, i, lambda); } Matrix.Cholesky cholesky = K.cholesky(true); double[] w = cholesky.solve(y); return new KernelMachine<>(kernel, x, w); }  static KernelMachine<T> fit(T[] x, double[] y, MercerKernel<T> kernel, double lambda); static KernelMachine<T> fit(T[] x, double[] y, T[] t, MercerKernel<T> kernel, double lambda); static KernelMachine<T> nystrom(T[] x, double[] y, T[] t, MercerKernel<T> kernel, double lambda); }
GaussianProcessRegression { public static <T> KernelMachine<T> fit(T[] x, double[] y, MercerKernel<T> kernel, double lambda) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length)); } if (lambda < 0.0) { throw new IllegalArgumentException("Invalid regularization parameter lambda = " + lambda); } int n = x.length; Matrix K = new Matrix(n, n); K.uplo(UPLO.LOWER); for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { double k = kernel.k(x[i], x[j]); K.set(i, j, k); K.set(j, i, k); } K.add(i, i, lambda); } Matrix.Cholesky cholesky = K.cholesky(true); double[] w = cholesky.solve(y); return new KernelMachine<>(kernel, x, w); }  static KernelMachine<T> fit(T[] x, double[] y, MercerKernel<T> kernel, double lambda); static KernelMachine<T> fit(T[] x, double[] y, T[] t, MercerKernel<T> kernel, double lambda); static KernelMachine<T> nystrom(T[] x, double[] y, T[] t, MercerKernel<T> kernel, double lambda);  }