Function() public method

Polynomial kernel function.
public Function ( double z ) : double
z double Distance z in input space.
return double
        public void FunctionTest()
        {
            Polynomial dense = new Polynomial(3);
            SparsePolynomial target = new SparsePolynomial(3);

            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, 1e-10);

            expected = dense.Function(dx, dz);
            actual = target.Function(sx, sz);
            Assert.AreEqual(expected, actual, 1e-10);

            expected = dense.Function(dy, dz);
            actual = target.Function(sy, sz);
            Assert.AreEqual(expected, actual, 1e-10);
        }
Beispiel #2
0
        public void FunctionTest()
        {
            Polynomial target = new Polynomial(1, 0);

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

            double expected = 2;
            double actual = target.Function(x, y);
            Assert.AreEqual(expected, actual);


            x = new double[] { 0.5, 2.0 };
            y = new double[] { 1.3, -0.2 };

            expected = 0.25;
            actual = target.Function(x, y);

            Assert.AreEqual(expected, actual);


            target = new Polynomial(3, 0);

            x = new double[] { 9.4, 22.1 };
            y = new double[] { -6.21, 4 };

            expected = System.Math.Pow(x.InnerProduct(y), 3);
            actual = target.Function(x, y);
            Assert.AreEqual(expected, actual, 0.0001);
        }
Beispiel #3
0
        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 <- polydot(3)

            Polynomial kernel = new Polynomial(degree: 3, constant: 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 =
            {
                { 70240.51, 69426.53, 57022.17,  99252.85, 55002.06 },
                { 69426.53, 68719.48, 56181.89,  98099.75, 54353.80 },
                { 57022.17, 56181.89, 46694.89,  80286.11, 44701.08 },
                { 99252.85, 98099.75, 80286.11, 141583.69, 77635.89 },
                { 55002.06, 54353.80, 44701.08,  77635.89, 43095.88 },

            };

            // 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-2);
        }
        public void KernelFunctionCacheConstructorTest7()
        {
            double[][] inputs =
            {
                new double[] { 0, 1 },
                new double[] { 1, 0 },
                new double[] { 1, 1 },
            };

            IKernel kernel = new Polynomial(2);

            int cacheSize = inputs.Length;

            KernelFunctionCache target = new KernelFunctionCache(kernel, inputs, cacheSize);

            Assert.AreEqual(3, target.Size);
            Assert.AreEqual(0, target.Hits);
            Assert.AreEqual(0, target.Misses);

            // upper half
            for (int i = 0; i < inputs.Length; i++)
            {
                for (int j = i + 1; j < inputs.Length; j++)
                {
                    double expected = kernel.Function(inputs[i], inputs[j]);
                    double actual = target.GetOrCompute(i, j);

                    Assert.AreEqual(expected, actual);
                }
            }

            var lruList1 = target.GetLeastRecentlyUsedList();

            Assert.AreEqual(3, target.Misses);
            Assert.AreEqual(0, target.Hits);
            Assert.AreEqual(1.0, target.Usage);

            // upper half, backwards
            for (int i = inputs.Length - 1; i >= 0; i--)
            {
                for (int j = inputs.Length - 1; j >= i; j--)
                {
                    double expected = kernel.Function(inputs[i], inputs[j]);
                    double actual = target.GetOrCompute(j, i);

                    Assert.AreEqual(expected, actual);
                }
            }

            var lruList2 = target.GetLeastRecentlyUsedList();

            Assert.IsTrue(lruList2.SequenceEqual(lruList1.Reverse())); 

            Assert.AreEqual(3, target.Misses);
            Assert.AreEqual(3, target.Hits);
            Assert.AreEqual(1.0, target.Usage);
        }
Beispiel #5
0
        public void FunctionTest()
        {
            Polynomial target = new Polynomial(1, 0);

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

            double expected = 2;
            double actual = target.Function(x, y);
            Assert.AreEqual(expected, actual);


            x = new double[] { 0.5, 2.0 };
            y = new double[] { 1.3, -0.2 };

            expected = 0.25;
            actual = target.Function(x, y);

            Assert.AreEqual(expected, actual);


            target = new Polynomial(3, 0);

            x = new double[] { 9.4, 22.1 };
            y = new double[] { -6.21, 4 };

            expected = 27070.26085757601;
            actual = target.Function(x, y);
            Assert.AreEqual(expected, actual, 0.0001);
        }