Example usage for org.apache.commons.math3.linear RealMatrix getRowDimension

List of usage examples for org.apache.commons.math3.linear RealMatrix getRowDimension

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealMatrix getRowDimension.

Prototype

int getRowDimension();

Source Link

Document

Returns the number of rows in the matrix.

Usage

From source file:edu.cudenver.bios.power.glmm.GLMMTest.java

/**
 * Create a test to perform data analysis
 * @param fMethod// w w w .j a v a2 s  .  co m
 * @param cdfMethod
 * @param X
 * @param XtXInverse
 * @param rank
 * @param Y
 * @param C
 * @param U
 * @param thetaNull
 */
public GLMMTest(FApproximation fMethod, RealMatrix X, RealMatrix XtXInverse, int rank, RealMatrix Y,
        RealMatrix C, RealMatrix U, RealMatrix thetaNull) {
    this.fMethod = fMethod;
    this.Xessence = null;
    this.XtXInverse = XtXInverse;
    if (this.XtXInverse == null) {
        this.XtXInverse = new LUDecomposition(X.transpose().multiply(X)).getSolver().getInverse();
    }
    this.totalN = X.getRowDimension();
    this.rank = rank;
    this.C = C;
    this.U = U;
    this.thetaNull = thetaNull;
    this.beta = this.XtXInverse.multiply(X.transpose()).multiply(Y);
    RealMatrix YHat = X.multiply(this.beta);
    RealMatrix Ydiff = Y.subtract(YHat);
    this.sigmaError = (Ydiff.transpose().multiply(Ydiff))
            .scalarMultiply(((double) 1 / (double) (totalN - rank)));

    // check if uni/multivariate design
    multivariate = (Y.getColumnDimension() > 1);

    // cache the value of M
    RealMatrix cxxcEssence = C.multiply((this.XtXInverse).multiply(C.transpose()));
    M = new LUDecomposition(cxxcEssence).getSolver().getInverse();
}

From source file:com.joptimizer.algebra.QRSparseFactorizationTest.java

public void testDecompose() throws Exception {
    log.debug("testDecompose");
    final double[][] A = new double[][] { { 1, 0, 0, 2 }, { 0, 0, 2, 0 }, { 2, 3, 0, 0 }, { 0, 0, 4, 4 } };

    double[][] EQ = { { 0.447214, -0.894427, 0., 0. }, { 0., 0., 0.447214, -0.894427 },
            { 0.894427, 0.447214, 0., 0. }, { 0., 0., 0.894427, 0.447214 } };
    double[][] ER = { { 2.23607, 2.68328, 0., 0.894427 }, { 0., 1.34164, 0., -1.78885 },
            { 0., 0., 4.47214, 3.57771 }, { 0., 0., 0., 1.78885 } };

    QRDecomposition dFact = new QRDecomposition(new Array2DRowRealMatrix(A));
    RealMatrix Q = dFact.getQ();/* ww w  . j a  v  a 2  s . co m*/
    RealMatrix R = dFact.getR();
    RealMatrix H = dFact.getH();
    log.debug("Q: " + ArrayUtils.toString(Q.getData()));
    log.debug("R: " + ArrayUtils.toString(R.getData()));
    //log.debug("H: " + ArrayUtils.toString(H.getData()));

    SparseDoubleMatrix2D S = new SparseDoubleMatrix2D(A);
    QRSparseFactorization qr = new QRSparseFactorization(S);
    qr.factorize();
    log.debug("R: " + ArrayUtils.toString(qr.getR().toArray()));
    for (int i = 0; i < R.getRowDimension(); i++) {
        for (int j = 0; j < R.getColumnDimension(); j++) {
            assertEquals(ER[i][j], qr.getR().getQuick(i, j), 1.e-5);
        }
    }
    assertTrue(qr.hasFullRank());
}

From source file:edu.ucdenver.bios.powersvc.resource.PowerResourceHelper.java

/**
 * Create a sigma outcomes/covariate matrix from the study design.
 * @param studyDesign study design object
 * @return sigma outcomes/covariate matrix
 *///from   w  w w .  j ava2 s  . c  o  m
public static RealMatrix sigmaOutcomesCovariateMatrixFromStudyDesign(StudyDesign studyDesign, RealMatrix sigmaG,
        RealMatrix sigmaY) {
    if (studyDesign.getViewTypeEnum() == StudyDesignViewTypeEnum.MATRIX_MODE) {
        return toRealMatrix(studyDesign.getNamedMatrix(PowerConstants.MATRIX_SIGMA_OUTCOME_GAUSSIAN));
    } else {
        RealMatrix sigmaYG = toRealMatrix(
                studyDesign.getNamedMatrix(PowerConstants.MATRIX_SIGMA_OUTCOME_GAUSSIAN));
        /*
         * In guided mode, sigmaYG is specified as correlation values.  We adjust
         * to make it into a covariance matrix.  We also expand for clustering
         */
        if (sigmaYG != null) {
            /*
             * Make into a covariance.  We first make sure the other sigma matrices are
             * of appropriate dimension to allow this
             */
            if (sigmaG == null || sigmaG.getRowDimension() <= 0 || sigmaG.getColumnDimension() <= 0) {
                throw new IllegalArgumentException("Invalid covariance for Gaussian covariate");
            }
            if (sigmaY == null || sigmaY.getRowDimension() < sigmaYG.getRowDimension()
                    || sigmaY.getColumnDimension() != sigmaY.getRowDimension()) {
                throw new IllegalArgumentException("Invalid covariance for outcome");
            }
            // assumes sigmaG is already updated to be a variance
            double varG = sigmaG.getEntry(0, 0);
            for (int row = 0; row < sigmaYG.getRowDimension(); row++) {
                double corrYG = sigmaYG.getEntry(row, 0);
                double varY = sigmaY.getEntry(row, row);
                sigmaYG.setEntry(row, 0, corrYG * Math.sqrt(varG * varY));
            }
            // calculate cluster size
            List<ClusterNode> clusterNodeList = studyDesign.getClusteringTree();
            if (clusterNodeList != null && clusterNodeList.size() > 0) {
                int totalRows = 1;
                for (ClusterNode node : clusterNodeList) {
                    totalRows *= node.getGroupSize();
                }

                // kronecker product the sigmaYG matrix with a matrix of ones to 
                // generate the proper dimensions for a cluster sample
                RealMatrix oneMatrix = MatrixUtils.getRealMatrixWithFilledValue(totalRows, 1, 1);
                sigmaYG = MatrixUtils.getKroneckerProduct(oneMatrix, sigmaYG);
            }
        }
        return sigmaYG;
    }
}

From source file:com.clust4j.algo.HierarchicalAgglomerative.java

/** {@inheritDoc} */
@Override/* w  w w. java2  s  .  com*/
public int[] predict(RealMatrix newData) {
    final int[] fit_labels = getLabels(); // throws the MNF exception if not fit
    final int numSamples = newData.getRowDimension(), n = newData.getColumnDimension();

    // Make sure matches dimensionally
    if (n != this.data.getColumnDimension())
        throw new DimensionMismatchException(n, data.getColumnDimension());

    /*
     * There's no great way to predict on a hierarchical
     * algorithm, so we'll treat this like a CentroidLearner,
     * create centroids from the k clusters formed, then
     * predict via the CentroidUtils. This works because
     * Hierarchical is not a NoiseyClusterer
     */

    // CORNER CASE: num_clusters == 1, return only label (0)
    if (1 == num_clusters)
        return VecUtils.repInt(fit_labels[0], numSamples);

    return new NearestCentroidParameters().setMetric(this.dist_metric) // if it fails, falls back to default Euclidean...
            .setVerbose(false) // just to be sure in case default ever changes...
            .fitNewModel(this.getData(), fit_labels).predict(newData);
}

From source file:edu.cmu.tetrad.search.IndTestMultiFisherZ.java

public boolean isIndependent(Node x, Node y, List<Node> z) {
    if (verbose) {
        System.out.println("\n" + x + " _||_ " + y + " | " + z);
    }/*w  ww  . ja v  a 2 s  .  com*/

    List<Node> aa = nodeMap.get(x);
    List<Node> bb = nodeMap.get(y);

    List<Node> cc = new ArrayList<Node>();

    for (Node _z : z) {
        cc.addAll(nodeMap.get(_z));
    }

    TetradMatrix submatrix = subMatrix(cov, aa, bb, cc);

    TetradMatrix inverse;
    int rank;

    try {
        inverse = submatrix.inverse();
        rank = inverse.columns();
    } catch (Exception e) {
        SingularValueDecomposition svd = new SingularValueDecomposition(submatrix.getRealMatrix());
        RealMatrix _inverse = svd.getSolver().getInverse();
        inverse = new TetradMatrix(_inverse, _inverse.getRowDimension(), _inverse.getColumnDimension());
        rank = svd.getRank();
    }

    final List<Double> pValues = new ArrayList<Double>();
    List<Integer> _i = new ArrayList<Integer>();
    List<Integer> _m = new ArrayList<Integer>();

    TetradMatrix stdSubmatrix = TestHippocampusUtils.subset(stdData, aa, bb, cc);

    for (int i = 0; i < aa.size(); i++) {
        for (int m = 0; m < bb.size(); m++) {
            int j = aa.size() + m;
            double a = -1.0 * inverse.get(i, j);
            double v0 = inverse.get(i, i);
            double v1 = inverse.get(j, j);
            double b = sqrt(v0 * v1);

            double r = a / b;

            int dof = cov.getSampleSize() - 1 - rank;

            if (dof < 0) {
                //                    out.println("Negative dof: " + dof + " n = " + cov.getSampleSize() + " cols = " + inverse.columns());
                dof = 0;
            }

            double t2 = moment22(stdSubmatrix, i, j);

            double z_ = sqrt(dof) * 0.5 * (Math.log(1.0 + r) - Math.log(1.0 - r));
            double p = 2.0 * (1.0 - RandomUtil.getInstance().normalCdf(0, sqrt(t2), abs(z_)));

            //                if (p < alpha) p = 0;

            //                System.out.println("z = " + z_ + " p = " + p);

            pValues.add(p);
            _i.add(i);
            _m.add(m);
        }
    }

    List<Integer> indices = new ArrayList<Integer>();
    for (int i = 0; i < pValues.size(); i++) {
        indices.add(i);
    }

    Collections.sort(indices, new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return pValues.get(o1).compareTo(pValues.get(o2));
        }
    });

    List<Double> pValues2 = new ArrayList<Double>();
    List<Integer> _iIndices = new ArrayList<Integer>();
    List<Integer> _mIndices = new ArrayList<Integer>();

    for (int _y = 0; _y < indices.size(); _y++) {
        pValues2.add(pValues.get(indices.get(_y)));
        _iIndices.add(_i.get(indices.get(_y)));
        _mIndices.add(_m.get(indices.get(_y)));
    }

    int k = StatUtils.fdr(alpha, pValues2, false);
    double cutoff = StatUtils.fdrCutoff(alpha, pValues2, false);
    System.out.println("** cutoff = " + cutoff);

    int nonzero = -1;

    for (int i = 0; i < pValues2.size(); i++) {
        if (pValues2.get(i) != 0) {
            nonzero = i;
            break;
        }
    }

    double q = StatUtils.fdrQ(pValues2, nonzero);
    System.out.println("Q = " + q);

    //        boolean independent = q >= alpha;

    ////        boolean independent = k > nonzero;
    //
    System.out.println("k = " + k);
    System.out.println("nonzero = " + nonzero);
    System.out.println("fisher = " + fisherMethodP(pValues2));
    //
    //        boolean independent = k <= nonzero;

    double ratio = ((double) k) / pValues2.size();
    System.out.println("ratio = " + ratio);
    boolean independent = ratio < 0.1;
    //
    System.out.println(independent ? "Independent" : "Dependent");

    return independent;
}

From source file:msi.gama.util.matrix.GamaIntMatrix.java

public GamaIntMatrix(final RealMatrix rm) {
    super(rm.getColumnDimension(), rm.getRowDimension(), Types.INT);
    matrix = new int[rm.getColumnDimension() * rm.getRowDimension()];
    updateMatrix(rm);//from  w  w w  . java2s .  c  om
}

From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotation.java

private RotationResults GPFoblq(RealMatrix A, boolean normalize, int maxIter, double eps)
        throws ConvergenceException {
    int ncol = A.getColumnDimension();

    RealMatrix Tinner = null;/*from   ww  w  .  j  a  v a 2 s. c  o m*/
    RealMatrix TinnerInv = null;
    RealMatrix Tmat = new IdentityMatrix(ncol);

    if (normalize) {
        //elementwise division by normalizing weights
        final RealMatrix W = getNormalizingWeights(A, true);
        A.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
            @Override
            public double visit(int row, int column, double value) {
                return value / W.getEntry(row, column);
            }
        });
    }

    RealMatrix TmatInv = new LUDecomposition(Tmat).getSolver().getInverse();
    RealMatrix L = A.multiply(TmatInv.transpose());

    //compute gradientAt and function value
    gpFunction.computeValues(L);
    RealMatrix VgQ = gpFunction.getGradient();
    RealMatrix VgQt = VgQ;
    double f = gpFunction.getValue();
    double ft = f;
    RealMatrix G = ((L.transpose().multiply(VgQ).multiply(TmatInv)).transpose()).scalarMultiply(-1.0);

    int iter = 0;
    double alpha = 1.0;
    double s = eps + 0.5;
    double s2 = Math.pow(s, 2);
    int innerMaxIter = 10;
    int innerCount = 0;

    IdentityMatrix I = new IdentityMatrix(G.getRowDimension());
    RealMatrix V1 = MatrixUtils.getVector(ncol, 1.0);

    while (iter < maxIter) {
        RealMatrix M = MatrixUtils.multiplyElements(Tmat, G);
        RealMatrix diagP = new DiagonalMatrix(V1.multiply(M).getRow(0));
        RealMatrix Gp = G.subtract(Tmat.multiply(diagP));
        s = Math.sqrt(Gp.transpose().multiply(Gp).getTrace());
        s2 = Math.pow(s, 2);

        if (s < eps) {
            break;
        }
        alpha = 2.0 * alpha;

        innerCount = 0;
        for (int i = 0; i < innerMaxIter; i++) {
            RealMatrix X = Tmat.subtract(Gp.scalarMultiply(alpha));
            RealMatrix X2 = MatrixUtils.multiplyElements(X, X);
            RealMatrix V = V1.multiply(X2);
            V.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
                @Override
                public double visit(int row, int column, double value) {
                    return 1.0 / Math.sqrt(value);
                }
            });

            //compute new value of T, its inverse, and the rotated loadings
            RealMatrix diagV = new DiagonalMatrix(V.getRow(0));
            Tinner = X.multiply(diagV);
            TinnerInv = new LUDecomposition(Tinner).getSolver().getInverse();
            L = A.multiply(TinnerInv.transpose());

            //compute new values of the gradientAt and the rotation criteria
            gpFunction.computeValues(L);
            VgQt = gpFunction.getGradient();
            ft = gpFunction.getValue();

            innerCount++;
            if (ft < f - 0.5 * s2 * alpha) {
                break;
            }
            alpha = alpha / 2.0;
        }

        //            System.out.println(iter + "  " + f + "  " + s + "  " + Math.log10(s) + "  " + alpha + "  " + innerCount);

        Tmat = Tinner;
        f = ft;
        G = (L.transpose().multiply(VgQt).multiply(TinnerInv)).transpose().scalarMultiply(-1.0);
        iter++;
    }

    boolean convergence = s < eps;
    if (!convergence) {
        throw new ConvergenceException();
    }

    if (normalize) {
        //elementwise multiplication by normalizing weights
        final RealMatrix W = getNormalizingWeights(A, true);
        A.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
            @Override
            public double visit(int row, int column, double value) {
                return value * W.getEntry(row, column);
            }
        });
    }

    RealMatrix Phi = Tmat.transpose().multiply(Tmat);
    RotationResults result = new RotationResults(gpFunction.getValue(), L, Phi, Tmat, rotationMethod);
    return result;

}

From source file:edu.cmu.tetrad.util.TetradMatrix1.java

public TetradMatrix1(RealMatrix matrix, int rows, int columns) {
    if (matrix == null) {
        throw new IllegalArgumentException("Null matrix.");
    }/*  w w  w . jav a  2 s  .c  om*/

    this.apacheData = matrix;
    this.m = rows;
    this.n = columns;

    int _rows = matrix.getRowDimension();
    int _cols = matrix.getColumnDimension();
    if (_rows != 0 && _rows != rows)
        throw new IllegalArgumentException();
    if (_cols != 0 && _cols != columns)
        throw new IllegalArgumentException();
}

From source file:edu.cudenver.bios.power.glmm.GLMMTestUnivariateRepeatedMeasures.java

/**
 * Ensure that the within subject contrast is orthonormal for all
 * UNIREP tests//from www.jav  a  2 s. c  om
 */
protected void createOrthonormalU() {
    RealMatrix UtU = U.transpose().multiply(U);
    double upperLeft = UtU.getEntry(0, 0);
    if (upperLeft != 0)
        UtU = UtU.scalarMultiply(1 / upperLeft);

    RealMatrix diffFromIdentity = UtU.subtract(
            org.apache.commons.math3.linear.MatrixUtils.createRealIdentityMatrix(UtU.getRowDimension()));

    // get maximum absolute value in U'U
    double maxValue = Double.NEGATIVE_INFINITY;
    for (int r = 0; r < diffFromIdentity.getRowDimension(); r++) {
        for (int c = 0; c < diffFromIdentity.getColumnDimension(); c++) {
            double entryVal = Math.abs(diffFromIdentity.getEntry(r, c));
            if (entryVal > maxValue)
                maxValue = entryVal;
        }
    }

    if (maxValue > Precision.SAFE_MIN) {
        // U'U matrix deviates from identity, so create a U matrix that is orthonormal
        // TODO: thus UNIREP tests may use a different U matrix than HLT/PBT/WLR tests???
        // TODO: displayed matrix results are incorrect now?
        U = new GramSchmidtOrthonormalization(U).getQ();
        debug("U replaced by orthonormal", U);
    }
}

From source file:com.joptimizer.optimizers.OptimizationRequestHandler.java

/**
 * Find a solution of the linear (equalities) system A.x = b.
 * @see "S.Boyd and L.Vandenberghe, Convex Optimization, p. 682"
 *//*from  w w  w . ja  va 2s.  co m*/
protected double[] findEqFeasiblePoint(double[][] A, double[] b) throws Exception {
    RealMatrix AT = new Array2DRowRealMatrix(A).transpose();
    int p = A.length;

    SingularValueDecomposition dFact1 = new SingularValueDecomposition(AT);
    int rangoAT = dFact1.getRank();
    if (rangoAT != p) {
        throw new RuntimeException("Equalities matrix A must have full rank");
    }

    QRDecomposition dFact = new QRDecomposition(AT);
    //A = QR
    RealMatrix Q1Q2 = dFact.getQ();
    RealMatrix R0 = dFact.getR();
    RealMatrix Q1 = Q1Q2.getSubMatrix(0, AT.getRowDimension() - 1, 0, p - 1);
    RealMatrix R = R0.getSubMatrix(0, p - 1, 0, p - 1);
    double[][] rData = R.copy().getData();
    //inversion
    double[][] rInvData = Utils.upperTriangularMatrixUnverse(rData);
    RealMatrix RInv = new Array2DRowRealMatrix(rInvData);

    //w = Q1 *   Inv([R]T) . b
    double[] w = Q1.operate(RInv.transpose().operate(new ArrayRealVector(b))).toArray();
    return w;
}