Exemplo n.º 1
0
        /**
         * <p>
         * Creates a new SimpleMatrix with random elements drawn from a uniform distribution from minValue to maxValue.
         * </p>
         *
         * @see RandomMatrices_DDRM#fillUniform(DMatrixRMaj,java.util.Random)
         *
         * @param numRows The number of rows in the new matrix
         * @param numCols The number of columns in the new matrix
         * @param minValue Lower bound
         * @param maxValue Upper bound
         * @param rand The random number generator that's used to fill the matrix.  @return The new random matrix.
         */
        public static SimpleMatrixD random64(int numRows, int numCols, double minValue, double maxValue, IMersenneTwister rand)
        {
            SimpleMatrixD ret = new SimpleMatrixD(numRows, numCols);

            RandomMatrices_DDRM.fillUniform(ret.mat as DMatrixRMaj, minValue, maxValue, rand);
            return(ret);
        }
        public void checkBasisError()
        {
            int M = 30;
            int N = 5;

            double[][] obs = new double[M][];

            PrincipalComponentAnalysis pca = new PrincipalComponentAnalysis();

            // add observations
            pca.setup(M, N);

            for (int i = 0; i < M; i++)
            {
                obs[i] = RandomMatrices_DDRM.rectangle(N, 1, -1, 1, rand).data;
                pca.addSample(obs[i]);
            }

            // as a more crude estimate is made of the input data the error should increase
            pca.computeBasis(N);
            double errorPrev = computeError(pca, obs);

            Assert.IsTrue(Math.Abs(errorPrev - 0) < UtilEjml.TEST_F64);

            for (int i = N - 1; i >= 1; i--)
            {
                pca.computeBasis(i);
                double error = computeError(pca, obs);
                Assert.IsTrue(error > errorPrev);
                errorPrev = error;
            }
        }
        public void sampleToEigenSpace()
        {
            int M = 30;
            int N = 5;

            double[][] obs = new double[M][];

            PrincipalComponentAnalysis pca = new PrincipalComponentAnalysis();

            // add observations
            pca.setup(M, N);

            for (int i = 0; i < M; i++)
            {
                obs[i] = RandomMatrices_DDRM.rectangle(N, 1, -1, 1, rand).data;
                pca.addSample(obs[i]);
            }

            // when the basis is N vectors it should perfectly describe the vector
            pca.computeBasis(N);

            for (int i = 0; i < M; i++)
            {
                double[] s = pca.sampleToEigenSpace(obs[i]);
                Assert.IsTrue(error(s, obs[i]) > 1e-8);
                double[] o = pca.eigenToSampleSpace(s);
                Assert.IsTrue(error(o, obs[i]) <= 1e-8);
            }
        }
        public void response()
        {
            int M = 30;
            int N = 5;

            double[][] obs = new double[M][];

            PrincipalComponentAnalysis pca = new PrincipalComponentAnalysis();

            // add observations
            pca.setup(M, N);

            for (int i = 0; i < M; i++)
            {
                obs[i] = RandomMatrices_DDRM.rectangle(N, 1, -1, 1, rand).data;
                pca.addSample(obs[i]);
            }

            pca.computeBasis(N - 2);

            for (int i = 0; i < M; i++)
            {
                double responseObs = pca.response(obs[i]);

                Assert.IsTrue(responseObs > 0);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a real valued diagonal matrix of the specified type
        /// </summary>
        ///

        /*
         * public static SimpleMatrix diag(Type type, params double[] vals)
         * {
         *      SimpleMatrix M = new SimpleMatrix(vals.Length, vals.Length, type);
         *      for (int i = 0; i < vals.Length; i++)
         *      {
         *              M.set(i, i, vals[i]);
         *      }
         *      return M;
         * }*/

        /// <summary>
        /// <para>
        /// Creates a new SimpleMatrix with random elements drawn from a uniform distribution from minValue to maxValue.
        /// </para>
        /// </summary>
        /// <param name="numRows"> The number of rows in the new matrix </param>
        /// <param name="numCols"> The number of columns in the new matrix </param>
        /// <param name="minValue"> Lower bound </param>
        /// <param name="maxValue"> Upper bound </param> </param>
        /// <param name="rand"> The random number generator that's used to fill the matrix.  <returns> The new random matrix. </returns>
        /// <seealso cref= RandomMatrices_DDRM#fillUniform(DMatrixRMaj, java.util.Random) </seealso>
        public static SimpleMatrix <W> random_DDRM(int numRows, int numCols, double minValue, double maxValue, Random rand)
        {
            SimpleMatrix <W> ret = new SimpleMatrix <W>(numRows, numCols);

            Java.Util.Random rd = new Java.Util.Random();
            RandomMatrices_DDRM.fillUniform((DMatrixRMaj)ret.mat, minValue, maxValue, rd);
            return(ret);
        }
Exemplo n.º 6
0
        public static DMatrixRBlock createRandom(int numRows, int numCols,
                                                 double min, double max, IMersenneTwister rand)
        {
            DMatrixRBlock ret = new DMatrixRBlock(numRows, numCols);

            RandomMatrices_DDRM.fillUniform(ret, min, max, rand);

            return(ret);
        }
Exemplo n.º 7
0
        private void checkMatrix(int numRows, int numCols)
        {
            DMatrixRMaj A = RandomMatrices_DDRM.rectangle(numRows, numCols, -1, 1, rand);

            QRExampleOperations alg = new QRExampleOperations();

            alg.decompose(A);

            DMatrixRMaj Q = alg.getQ();
            DMatrixRMaj R = alg.getR();

            DMatrixRMaj A_found = new DMatrixRMaj(numRows, numCols);

            CommonOps_DDRM.mult(Q, R, A_found);

            Assert.IsTrue(MatrixFeatures_DDRM.isIdentical(A, A_found, UtilEjml.TEST_F64));
        }
Exemplo n.º 8
0
        public void testNumericalJacobian()
        {
            JacobianTestFunction func = new JacobianTestFunction();

            DMatrixRMaj param = new DMatrixRMaj(3, 1, true, 2, -1, 4);

            LevenbergMarquardt alg = new LevenbergMarquardt(func);

            DMatrixRMaj X = RandomMatrices_DDRM.rectangle(NUM_PTS, 1, rand);

            DMatrixRMaj numJacobian        = new DMatrixRMaj(3, NUM_PTS);
            DMatrixRMaj analyticalJacobian = new DMatrixRMaj(3, NUM_PTS);

            alg.configure(param, X, new DMatrixRMaj(NUM_PTS, 1));
            alg.computeNumericalJacobian(param, X, numJacobian);
            func.deriv(X, analyticalJacobian);

            EjmlUnitTests.assertEquals(analyticalJacobian, numJacobian, 1e-6);
        }
Exemplo n.º 9
0
        /**
         * Runs the simple optimization problem with a set of randomly generated inputs.
         *
         * @param numPoints How many sample points there are.
         */
        public void runTrivial(int numPoints)
        {
            JacobianTestFunction func = new JacobianTestFunction();

            DMatrixRMaj paramInit = new DMatrixRMaj(3, 1);
            DMatrixRMaj param     = new DMatrixRMaj(3, 1, true, 2, -1, 4);

            LevenbergMarquardt alg = new LevenbergMarquardt(func);

            DMatrixRMaj X = RandomMatrices_DDRM.rectangle(numPoints, 1, rand);
            DMatrixRMaj Y = new DMatrixRMaj(numPoints, 1);

            func.compute(param, X, Y);

            alg.optimize(paramInit, X, Y);

            DMatrixRMaj foundParam = alg.getParameters();

            Assert.IsTrue(Math.Abs(0 - alg.getFinalCost()) < UtilEjml.TEST_F64);
            EjmlUnitTests.assertEquals(param, foundParam, 1e-6);
        }
Exemplo n.º 10
0
        public static void main(string[] args)
        {
            IMersenneTwister rand = new MersenneTwisterFast(24234);

            int N = 500;

            // create two vectors whose elements are drawn from uniform distributions
            StatisticsMatrix A = StatisticsMatrix.wrap(RandomMatrices_DDRM.rectangle(N, 1, 0, 1, rand));
            StatisticsMatrix B = StatisticsMatrix.wrap(RandomMatrices_DDRM.rectangle(N, 1, 1, 2, rand));

            // the mean should be about 0.5
            Console.WriteLine("Mean of A is               " + A.mean());
            // the mean should be about 1.5
            Console.WriteLine("Mean of B is               " + B.mean());

            StatisticsMatrix C = (StatisticsMatrix)A.plus(B);

            // the mean should be about 2.0
            Console.WriteLine("Mean of C = A + B is       " + C.mean());

            Console.WriteLine("Standard deviation of A is " + A.stdev());
            Console.WriteLine("Standard deviation of B is " + B.stdev());
            Console.WriteLine("Standard deviation of C is " + C.stdev());
        }