public void DistanceTest()
        {
            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.Distance(dx, dy);
            actual   = target.Distance(sx, sy);
            Assert.AreEqual(expected, actual);

            expected = dense.Distance(dx, dz);
            actual   = target.Distance(sx, sz);
            Assert.AreEqual(expected, actual);

            expected = dense.Distance(dy, dz);
            actual   = target.Distance(sy, sz);
            Assert.AreEqual(expected, actual);
        }
Exemple #2
0
        public void GaussianReverseDistanceTest()
        {
            var gaussian = new Gaussian(4.2);

            var x = new double[] { 0.2, 0.5 };
            var y = new double[] { 0.3, -0.7 };

            double expected = Distance.SquareEuclidean(x, y);

            double df     = gaussian.Distance(x, y);
            double actual = gaussian.ReverseDistance(df);

            Assert.AreEqual(expected, actual, 1e-10);
        }
        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);
        }
Exemple #5
0
        public void GaussianDistanceTest()
        {
            var gaussian = new Gaussian(1);

            double[] x = { 1, 1 };
            double[] y = { 1, 1 };

            double actual   = gaussian.Distance(x, y);
            double expected = 0;

            Assert.AreEqual(expected, actual);


            gaussian = new Gaussian(11.5);

            x = new double[] { 0.2, 0.5 };
            y = new double[] { 0.3, -0.7 };

            actual   = gaussian.Distance(x, y);
            expected = Accord.Statistics.Tools.Distance(gaussian, x, y);

            Assert.AreEqual(expected, actual, 1e-10);
        }
Exemple #6
0
        public void GaussianDistanceTest()
        {
            IDistance gaussian = new Gaussian(1);

            double[] x = { 1, 1 };
            double[] y = { 1, 1 };

            double actual   = gaussian.Distance(x, y);
            double expected = 0;

            Assert.AreEqual(expected, actual);


            gaussian = new Gaussian(11.5);

            x = new double[] { 0.2, 0.5 };
            y = new double[] { 0.3, -0.7 };

            actual   = gaussian.Distance(x, y);
            expected = 341.46531595796711;

            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);
        }