public void FunctionTest() { Gaussian dense = new Gaussian(3.6); SparseGaussian target = new SparseGaussian(3.6); double[] sx = { 1, -0.555556, 2, +0.250000, 3, -0.864407, 4, -0.916667 }; double[] sy = { 1, -0.666667, 2, -0.166667, 3, -0.864407, 4, -0.916667 }; double[] sz = { 1, -0.944444, 3, -0.898305, 4, -0.916667 }; double[] dx = { -0.555556, +0.250000, -0.864407, -0.916667 }; double[] dy = { -0.666667, -0.166667, -0.864407, -0.916667 }; double[] dz = { -0.944444, +0.000000, -0.898305, -0.916667 }; double expected, actual; expected = dense.Function(dx, dy); actual = target.Function(sx, sy); Assert.AreEqual(expected, actual); expected = dense.Function(dx, dz); actual = target.Function(sx, sz); Assert.AreEqual(expected, actual); expected = dense.Function(dy, dz); actual = target.Function(sy, sz); Assert.AreEqual(expected, actual); }
public void FunctionTest_EqualInputs() { var x = new double[] { 1, 2, 5, 1 }; var y = new double[] { 1, 2, 5, 1 }; var target = new Gaussian <DynamicTimeWarping>(new DynamicTimeWarping(1), 4.2); double expected = target.Function(x, y); double actual = target.Function(x, x); Assert.AreEqual(expected, actual, 0.000001); }
public void FunctionTest_EqualInputs() { var x = new double[] { 1, 2, 5, 1 }; var y = new double[] { 1, 2, 5, 1 }; var target = new Gaussian <Linear>(new Linear(1), 4.2); double expected = target.Function(x, y); double actual = target.Function(x, x); Assert.AreEqual(expected, actual); }
public void KernelFunctionCacheConstructorTest6() { IKernel kernel = new Gaussian(0.6); int cacheSize = inputs.Length; KernelFunctionCache target = new KernelFunctionCache(kernel, inputs, cacheSize); Assert.AreEqual(10, target.Size); Assert.AreEqual(0, target.Hits); Assert.AreEqual(0, target.Misses); Assert.IsTrue(target.Enabled); for (int i = 0; i < inputs.Length; i++) { double expected = kernel.Function(inputs[i], inputs[i]); double actual = target.GetOrCompute(i); Assert.AreEqual(expected, actual); } Assert.AreEqual(0, target.Hits); for (int i = 0; i < inputs.Length; i++) { for (int j = 0; j < inputs.Length; j++) { double expected = kernel.Function(inputs[i], inputs[j]); double actual = target.GetOrCompute(i, j); Assert.AreEqual(expected, actual); } } for (int i = 0; i < inputs.Length; i++) { for (int j = 0; j < inputs.Length; j++) { double expected = kernel.Function(inputs[i], inputs[j]); double actual = target.GetOrCompute(i, j); Assert.AreEqual(expected, actual); } } Assert.AreEqual(0, target.Misses); Assert.AreEqual(0, target.Hits); Assert.AreEqual(0, target.Usage); }
public void FunctionTest() { double sigma = 0.1; var target = new Gaussian<Linear>(new Linear(0), sigma); double[] x = { 2.0, 3.1, 4.0 }; double[] y = { 2.0, 3.1, 4.0 }; double expected = 1; double actual; actual = target.Function(x, y); Assert.AreEqual(expected, actual); actual = target.Function(x, x); Assert.AreEqual(expected, actual); actual = target.Function(y, y); Assert.AreEqual(expected, actual); }
public void FunctionTest2() { // Tested against R's kernlab double[][] data = { new double[] { 5.1, 3.5, 1.4, 0.2 }, new double[] { 5.0, 3.6, 1.4, 0.2 }, new double[] { 4.9, 3.0, 1.4, 0.2 }, new double[] { 5.8, 4.0, 1.2, 0.2 }, new double[] { 4.7, 3.2, 1.3, 0.2 }, }; // rbf <- rbfdot(sigma = 1) // R's sigma is framework's Gaussian's gamma: Gaussian kernel = new Gaussian() { Gamma = 1 }; // Compute the kernel matrix double[,] actual = new double[5, 5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { actual[i, j] = kernel.Function(data[i], data[j]); } } double[,] expected = { { 1.0000000, 0.9801987, 0.7482636, 0.4584060, 0.7710516 }, { 0.9801987, 1.0000000, 0.6907343, 0.4317105, 0.7710516 }, { 0.7482636, 0.6907343, 1.0000000, 0.1572372, 0.9139312 }, { 0.4584060, 0.4317105, 0.1572372, 1.0000000, 0.1556726 }, { 0.7710516, 0.7710516, 0.9139312, 0.1556726, 1.0000000 }, }; // Assert both are equal for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { Assert.AreEqual(expected[i, j], actual[i, j], 1e-6); } } }
public void rbf_kernel() { #region doc_rbf // Let's say we created a Gaussian RBF kernel: IRadialBasisKernel rbf = new Gaussian(sigma: 3.6); // Normally, kernel functions are always defined // between two points. For example, if we were to // compute the Gaussian kernel between points double[] x = { 1, 3 }; double[] y = { 4, 2 }; // The most straightforward way would be to use double k = rbf.Function(x, y); // should be 0.6799048146023815 // However, since this kernel is a RBF, it means we // can also compute its kernel function through the // Euclidean distance between the two points: double z = Distance.Euclidean(x, y); double a = rbf.Function(z); // should also be 0.6799048146023815 #endregion Assert.AreEqual(k, a); }
public void FunctionTest2() { double[][] data = { new double[] { 5.1, 3.5, 1.4, 0.2 }, new double[] { 5.0, 3.6, 1.4, 0.2 }, new double[] { 4.9, 3.0, 1.4, 0.2 }, new double[] { 5.8, 4.0, 1.2, 0.2 }, new double[] { 4.7, 3.2, 1.3, 0.2 }, }; var kernel = new Gaussian <SquareEuclidean>(new SquareEuclidean()) { Gamma = 1 }; // Compute the kernel matrix double[,] actual = new double[5, 5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { actual[i, j] = kernel.Function(data[i], data[j]); } } double[,] expected = { { 1.0000000, 0.9801987, 0.7482636, 0.4584060, 0.7710516 }, { 0.9801987, 1.0000000, 0.6907343, 0.4317105, 0.7710516 }, { 0.7482636, 0.6907343, 1.0000000, 0.1572372, 0.9139312 }, { 0.4584060, 0.4317105, 0.1572372, 1.0000000, 0.1556726 }, { 0.7710516, 0.7710516, 0.9139312, 0.1556726, 1.0000000 }, }; // Assert both are equal for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { Assert.AreEqual(expected[i, j], actual[i, j], 1e-6); } } }
public void doc_test_generic() { #region doc_distance_generic // Let's say we would like to create a composite Gaussian // kernel based on a Hamming distance defined for strings: var gaussian = new Gaussian <Hamming, string>(new Hamming(), sigma: 4.2); string x = "abba"; string y = "aaab"; // Now, we can compute the kernel function as: double k = gaussian.Function(x, y); // 0.8436074263840595 // We can also obtain the distance in kernel space as: double d = gaussian.Distance(x, y); // 0.312785147231881 #endregion Assert.AreEqual(0.8436074263840595, k, 1e-10); Assert.AreEqual(0.312785147231881, d, 1e-10); }
public void doc_test_generic() { #region doc_kernel_generic // Let's say we would like to create a composite Gaussian // kernel based on the String Subsequence Kernel (SSK): var gaussian = new Gaussian <StringSubsequence, string>( new StringSubsequence(k: 2), sigma: 4.2); string x = "abba"; string y = "aaab"; // Now, we can compute the kernel function as: double k = gaussian.Function(x, y); // 0.97588765410825273 // We can also obtain the distance in kernel space as: double d = gaussian.Distance(x, y); // 0.048224691783494533 #endregion Assert.AreEqual(0.97588765410825273, k, 1e-10); Assert.AreEqual(0.048224691783494533, d, 1e-10); }
public void GaussianFunctionTest() { var x = new double[] { 0, 4, 2, 1 }; var y = new double[] { 3, 2, }; var dtw = new DynamicTimeWarping(1); IKernel gaussian = new Gaussian <DynamicTimeWarping>(dtw, 1); double actual = gaussian.Function(x, y); Assert.AreEqual(0.3407192298459587, actual); gaussian = new Gaussian <DynamicTimeWarping>(dtw, 11.5); x = new double[] { 0.2, 5 }; y = new double[] { 3, 0.7 }; actual = gaussian.Function(x, y); Assert.AreEqual(0.99065918303292089, actual); }
public void GaussianFunctionTest() { IKernel gaussian = new Gaussian <Linear>(new Linear(0), 1); double[] x = { 1, 1 }; double[] y = { 1, 1 }; double actual = gaussian.Function(x, y); double expected = 1; Assert.AreEqual(expected, actual); gaussian = new Gaussian(11.5); x = new double[] { 0.2, 5 }; y = new double[] { 3, 0.7 }; actual = gaussian.Function(x, y); expected = 0.9052480234; Assert.AreEqual(expected, actual, 1e-10); }
public void doc_test() { #region doc_kernel // Let's say we would like to create a composite Gaussian kernel // based on an underlying Polynomial kernel of the second degree var gaussian = new Gaussian <Polynomial>(new Polynomial(degree: 2), sigma: 4.2); double[] x = { 2, 4 }; double[] y = { 1, 3 }; // Now, we can compute the kernel function as: double k = gaussian.Function(x, y); // 0.041810692337960746 // We can also obtain the distance in kernel space as: double d = gaussian.Distance(x, y); // 1.9163786153240785 // Whereas the Euclidean distance in input space would be: double e = Distance.Euclidean(x, y); // 1.4142135623730952 #endregion Assert.AreEqual(0.041810692337960746, k, 1e-10); Assert.AreEqual(1.9163786153240785, d, 1e-10); Assert.AreEqual(1.4142135623730952, e, 1e-10); }
public void doc_test() { #region doc_distance // Let's say we would like to create a composite // Gaussian kernel based on an Euclidean distance: var gaussian = new Gaussian <Euclidean>(new Euclidean(), sigma: 4.2); double[] x = { 2, 4 }; double[] y = { 1, 3 }; // Now, we can compute the kernel function as: double k = gaussian.Function(x, y); // 0.96070737352744806 // We can also obtain the distance in kernel space as: double d = gaussian.Distance(x, y); // 0.078585252945103878 // Whereas the Euclidean distance in input space would be: double e = Distance.Euclidean(x, y); // 1.4142135623730952 #endregion Assert.AreEqual(0.96070737352744806, k, 1e-10); Assert.AreEqual(0.078585252945103878, d, 1e-10); Assert.AreEqual(1.4142135623730952, e, 1e-10); }