Exemple #1
0
        public static void Test01e_2()
        {
            var X = new DoubleMatrix(new double[, ] {
                { 73, 746, 743, 106 }, { 584, 531, 420, 579 }, { 255, 562, 234, 693 }, { 360, 474, 381, 484 }, { 301, 78, 68, 313 }
            });
            var y = new DoubleMatrix(new double[, ] {
                { 803 }, { 292 }, { 230 }, { 469 }, { 655 }
            });

            var XtX = X.GetTranspose() * X;
            var Xty = X.GetTranspose() * y;

            var solver   = new DoubleLUDecomp(XtX);
            var expected = solver.Solve(Xty);

            FastNonnegativeLeastSquares.Execution(XtX, Xty, (i) => false, null, out var x, out var w);

            Assert.AreEqual(expected[0, 0], x[0, 0], 1e-4);
            Assert.AreEqual(expected[1, 0], x[1, 0], 1e-4);
            Assert.AreEqual(expected[2, 0], x[2, 0], 1e-4);
            Assert.AreEqual(expected[3, 0], x[3, 0], 1e-4);

            Assert.AreEqual(0, w[0, 0], 1e-8);
            Assert.AreEqual(0, w[1, 0], 1e-8);
            Assert.AreEqual(0, w[2, 0], 1e-8);
            Assert.AreEqual(0, w[3, 0], 1e-8);
        }
Exemple #2
0
        public static void Test02()
        {
            int NR = 100;
            int NC = 5;

            // erzeuge Basisfunktionen
            var X = new DoubleMatrix(NR, NC);

            for (int c = 0; c < 5; ++c)
            {
                double rt = (c + 1) * 4;
                for (int r = 0; r < NR; ++r)
                {
                    X[r, c] = Math.Exp(-r / rt);
                }
            }

            var y = new DoubleMatrix(NR, 1);

            for (int r = 0; r < NR; ++r)
            {
                double sum = 0;
                for (int c = 0; c < 5; ++c)
                {
                    double amp = 1 - 0.4 * Math.Abs(c - 2);
                    sum += amp * X[r, c];
                }
                y[r, 0] = sum;
            }

            var XtX = new DoubleMatrix(5, 5);

            MatrixMath.MultiplyFirstTransposed(X, X, XtX);

            var Xty = new DoubleMatrix(5, 1);

            MatrixMath.MultiplyFirstTransposed(X, y, Xty);
            FastNonnegativeLeastSquares.Execution(XtX, Xty, null, out var x, out var w);

            Assert.AreEqual(0.2, x[0, 0], 1e-6);
            Assert.AreEqual(0.6, x[1, 0], 1e-6);
            Assert.AreEqual(1.0, x[2, 0], 1e-6);
            Assert.AreEqual(0.6, x[3, 0], 1e-6);
            Assert.AreEqual(0.2, x[4, 0], 1e-6);
        }
Exemple #3
0
        public static void Test01a()
        {
            var X = new DoubleMatrix(new double[, ] {
                { 73, 71, 52 }, { 87, 74, 46 }, { 72, 2, 7 }, { 80, 89, 71 }
            });
            var y = new DoubleMatrix(new double[, ] {
                { 49 }, { 67 }, { 68 }, { 20 }
            });

            var XtX = X.GetTranspose() * X;
            var Xty = X.GetTranspose() * y;

            FastNonnegativeLeastSquares.Execution(XtX, Xty, null, out var x, out var w);

            Assert.AreEqual(0.65, x[0, 0], 0.01);
            Assert.AreEqual(0, x[1, 0], 0.01);
            Assert.AreEqual(0, x[2, 0], 0.01);

            Assert.AreEqual(0, w[0, 0], 1e-8);
            Assert.Less(w[1, 0], 0);
            Assert.Less(w[2, 0], 0);
        }
Exemple #4
0
        public static void Test01c()
        {
            var X = new DoubleMatrix(new double[, ] {
                { 106, 743, 746, 73 }, { 579, 420, 531, 584 }, { 693, 234, 562, 255 }, { 484, 381, 474, 360 }, { 313, 68, 78, 301 }
            });
            var y = new DoubleMatrix(new double[, ] {
                { 803 }, { 292 }, { 230 }, { 469 }, { 655 }
            });

            var XtX = X.GetTranspose() * X;
            var Xty = X.GetTranspose() * y;

            FastNonnegativeLeastSquares.Execution(XtX, Xty, null, out var x, out var w);

            Assert.AreEqual(0, x[0, 0], 1e-4);
            Assert.AreEqual(0.90443, x[1, 0], 1e-4);
            Assert.AreEqual(0, x[2, 0], 1e-4);
            Assert.AreEqual(0.29507, x[3, 0], 1e-4);

            Assert.Less(w[0, 0], 0);
            Assert.AreEqual(0, w[1, 0], 1e-8);
            Assert.Less(w[2, 0], 0);
            Assert.AreEqual(0, w[3, 0], 1e-8);
        }
Exemple #5
0
        public static void Test01b()
        {
            var X = new DoubleMatrix(new double[, ] {
                { 771, 307, 765, 280 }, { 404, 802, 29, 703 }, { 166, 446, 8, 236 }, { 985, 225, 510, 731 }, { 109, 12, 382, 89 }
            });
            var y = new DoubleMatrix(new double[, ] {
                { 83 }, { 339 }, { 330 }, { 731 }, { 896 }
            });

            var XtX = X.GetTranspose() * X;
            var Xty = X.GetTranspose() * y;

            FastNonnegativeLeastSquares.Execution(XtX, Xty, null, out var x, out var w);

            Assert.AreEqual(0, x[0, 0], 1e-4);
            Assert.AreEqual(0, x[1, 0], 1e-4);
            Assert.AreEqual(0.41813, x[2, 0], 1e-4);
            Assert.AreEqual(0.58480, x[3, 0], 1e-4);

            Assert.Less(w[0, 0], 0);
            Assert.Less(w[1, 0], 0);
            Assert.AreEqual(0, w[2, 0], 1e-8);
            Assert.AreEqual(0, w[3, 0], 1e-8);
        }
Exemple #6
0
        public static void Test01d2()
        {
            var X = new DoubleMatrix(new double[, ] {
                { 73, 746, 743, 106 }, { 584, 531, 420, 579 }, { 255, 562, 234, 693 }, { 360, 474, 381, 484 }, { 301, 78, 68, 313 }
            });
            var y = new DoubleMatrix(new double[, ] {
                { 803 }, { 292 }, { 230 }, { 469 }, { 655 }
            });

            var XtX = X.GetTranspose() * X;
            var Xty = X.GetTranspose() * y;

            FastNonnegativeLeastSquares.Execution(XtX, Xty, (i) => i != 3, null, out var x, out var w);

            Assert.AreEqual(0.37911, x[0, 0], 1e-4);
            Assert.AreEqual(0, x[1, 0], 1e-4);
            Assert.AreEqual(0.91034, x[2, 0], 1e-4);
            Assert.AreEqual(-0.07097, x[3, 0], 1e-4);

            Assert.AreEqual(0, w[0, 0], 1e-8);
            Assert.Less(w[1, 0], 0);
            Assert.AreEqual(0, w[2, 0], 1e-8);
            Assert.AreEqual(0, w[3, 0], 1e-8);
        }