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

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

Introduction

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

Prototype

int getColumnDimension();

Source Link

Document

Returns the number of columns in the matrix.

Usage

From source file:edu.dfci.cccb.mev.domain.Heatmap.java

private RealMatrix transpose(final RealMatrix original) {
    return new AbstractRealMatrix() {

        @Override//  w w  w. java2 s .c o  m
        public void setEntry(int row, int column, double value) throws OutOfRangeException {
            original.setEntry(column, row, value);
        }

        @Override
        public int getRowDimension() {
            return original.getColumnDimension();
        }

        @Override
        public double getEntry(int row, int column) throws OutOfRangeException {
            return original.getEntry(column, row);
        }

        @Override
        public int getColumnDimension() {
            return original.getRowDimension();
        }

        @Override
        public RealMatrix createMatrix(int rowDimension, int columnDimension)
                throws NotStrictlyPositiveException {
            return original.createMatrix(rowDimension, columnDimension);
        }

        @Override
        public RealMatrix copy() {
            final RealMatrix result = createMatrix(getRowDimension(), getColumnDimension());
            walkInOptimizedOrder(new RealMatrixPreservingVisitor() {

                @Override
                public void visit(int row, int column, double value) {
                    result.setEntry(row, column, value);
                }

                @Override
                public void start(int rows, int columns, int startRow, int endRow, int startColumn,
                        int endColumn) {
                }

                @Override
                public double end() {
                    return NaN;
                }
            });
            return result;
        }
    };
}

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

/**
 * This test shows the least amount of agreement with R. Only one decimal place of precision.
 * I am not sure why because the MINRES method worked fine for the other test.
 *//* w w  w.j av a 2s .co  m*/
@Test
public void m255TestMINRES() {
    System.out.println("MINRES Factor Analysis Test (no rotation): m255 data");
    RealMatrix R = new Array2DRowRealMatrix(readM255());
    ExploratoryFactorAnalysis fa = new ExploratoryFactorAnalysis(R, 3);
    fa.estimateParameters(EstimationMethod.MINRES);
    //        System.out.println();
    System.out.println(fa.printOutput(3));

    double[][] true_loadings = { { 0.6836, -0.3908, 0.1130 }, { 0.7002, -0.3712, 0.1542 },
            { 0.7200, -0.2217, 0.1227 }, { 0.6285, -0.1843, 0.1545 }, { 0.7622, -0.0188, 0.1045 },
            { 0.7216, 0.3458, 0.1759 }, { 0.5962, 0.4292, 0.2326 }, { 0.5214, 0.1782, 0.1026 },
            { 0.7102, 0.1378, -0.0139 }, { 0.5910, 0.2662, -0.0076 }, { 0.8389, 0.0129, -0.3197 },
            { 0.6957, 0.0292, -0.3029 } };

    double[] true_uniqueness = { 0.3675, 0.3482, 0.4172, 0.5472, 0.4075, 0.3284, 0.4082, 0.6869, 0.4762, 0.5798,
            0.1942, 0.4219 };

    FactorMethod factorMethod = fa.getFactorMethod();
    for (int i = 0; i < R.getColumnDimension(); i++) {
        for (int j = 0; j < 3; j++) {
            assertEquals("  Factor loadings: ", true_loadings[i][j],
                    Precision.round(factorMethod.getFactorLoadingAt(i, j), 4), 1e-2);
        }
    }

    for (int i = 0; i < R.getColumnDimension(); i++) {
        assertEquals("  Uniqueness: ", true_uniqueness[i], Precision.round(factorMethod.getUniquenessAt(i), 4),
                1e-2);
    }

}

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

private boolean indepCollection2(List<Node> aa, List<Node> bb, List<Node> cc, int n, double alpha) {
    //        List<Double> ret = getCutoff1(aa, bb, cc);
    //        List<Double> ret = getCutoff2(aa, bb, cc);
    //        double mean = ret.get(0);
    //        double sd = ret.get(1);

    int numPerm = 10;

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

    TetradMatrix inverse;//from  ww w . j  a  va2  s .c o  m
    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();
    }

    List<Double> pValues = new ArrayList<Double>();

    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 = Math.sqrt(v0 * v1);

            double r = a / b;

            int dof = n - 1 - rank;

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

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

            pValues.add(p);
        }
    }

    List<Double> zeroes = new ArrayList<Double>();

    for (double d : pValues) {
        if (d == 0)
            zeroes.add(d);
    }

    int k = 0;

    for (double p : pValues) {
        if (p < alpha) {
            k++;
        }
    }

    numTests++;

    int count = countGreater(aa, bb, cc, numPerm, k);
    //        int count = countGreater2(aa, bb, cc, numPerm, k);

    double p = count / (double) numPerm;

    boolean indep = p > alpha;

    //        double p = (1.0 - RandomUtil.getInstance().normalCdf(0, 1, (k - mean) / sd));

    //        boolean indep = p > alpha;

    if (verbose) {
        //            System.out.println("\n#accepted " + k + " cutoff = " + kCutoff + " indep = " + indep);
        //            System.out.println("\n#accepted " + k + " meam = " + mean + " sd = " + sd + " indep = " + indep);
        //            System.out.println("standard deviations " + (k - mean) / sd + " p = " + p + " alpha = " + alpha);
        System.out.println("\n#accepted " + k + " indep = " + indep);
        System.out.println("p = " + p + " alpha = " + alpha);
    }

    numTests++;
    if (!indep)
        numDependent++;

    return indep;

}

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

private boolean indepCollection(List<Node> aa, List<Node> bb, List<Node> cc, int n, double alpha) {
    //        List<Double> ret = getCutoff1(aa, bb, cc);
    //        List<Double> ret = getCutoff2(aa, bb, cc);
    //        double mean = ret.get(0);
    //        double sd = ret.get(1);

    int numPerm = 10;

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

    TetradMatrix inverse;//from   w  w  w . j ava2 s  .c  om
    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();
    }

    List<Double> pValues = new ArrayList<Double>();

    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 = Math.sqrt(v0 * v1);

            double r = a / b;

            int dof = n - 1 - rank;

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

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

            pValues.add(p);
        }
    }

    List<Double> zeroes = new ArrayList<Double>();

    for (double d : pValues) {
        if (d == 0)
            zeroes.add(d);
    }

    int k = 0;

    for (double p : pValues) {
        if (p < alpha) {
            k++;
        }
    }

    numTests++;

    int count = countGreater(aa, bb, cc, numPerm, k);
    //        int count = countGreater2(aa, bb, cc, numPerm, k);
    //        int count = countGreater3(aa, bb, cc, numPerm, k, submatrix, inverse);
    //        int count = countGreater4(aa, bb, cc, numPerm, k);

    double p = count / (double) numPerm;

    boolean indep = p > alpha;

    //        double p = (1.0 - RandomUtil.getInstance().normalCdf(0, 1, (k - mean) / sd));

    //        boolean indep = p > alpha;

    if (verbose) {
        //            System.out.println("\n#accepted " + k + " cutoff = " + kCutoff + " indep = " + indep);
        //            System.out.println("\n#accepted " + k + " meam = " + mean + " sd = " + sd + " indep = " + indep);
        //            System.out.println("standard deviations " + (k - mean) / sd + " p = " + p + " alpha = " + alpha);
        System.out.println("\n#accepted " + k + " indep = " + indep);
        System.out.println("p = " + p + " alpha = " + alpha);
    }

    numTests++;
    if (!indep)
        numDependent++;

    return indep;

}

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

@Test
public void m255TestMINRESVarimax() {
    System.out.println("MINRES Factor Analysis Test (Varimax rotation): m255 data");
    RealMatrix R = new Array2DRowRealMatrix(readM255());
    ExploratoryFactorAnalysis fa = new ExploratoryFactorAnalysis(R, 3);
    fa.estimateParameters(EstimationMethod.MINRES, RotationMethod.VARIMAX);
    System.out.println();// w ww .ja v a 2s. c om
    System.out.println(fa.printOutput());

    double[][] true_loadings = { { 0.6836, -0.3908, 0.1130 }, { 0.7002, -0.3712, 0.1542 },
            { 0.7200, -0.2217, 0.1227 }, { 0.6285, -0.1843, 0.1545 }, { 0.7622, -0.0188, 0.1045 },
            { 0.7216, 0.3458, 0.1759 }, { 0.5962, 0.4292, 0.2326 }, { 0.5214, 0.1782, 0.1026 },
            { 0.7102, 0.1378, -0.0139 }, { 0.5910, 0.2662, -0.0076 }, { 0.8389, 0.0129, -0.3197 },
            { 0.6957, 0.0292, -0.3029 } };

    double[] true_uniqueness = { 0.3675, 0.3482, 0.4172, 0.5472, 0.4075, 0.3284, 0.4082, 0.6869, 0.4762, 0.5798,
            0.1942, 0.4219 };

    FactorMethod factorMethod = fa.getFactorMethod();
    for (int i = 0; i < R.getColumnDimension(); i++) {
        for (int j = 0; j < 3; j++) {
            //                assertEquals("  Factor loadings: ", true_loadings[i][j], Precision.round(factorMethod.getFactorLoadingAt(i,j), 4), 1e-1);
        }
    }

    for (int i = 0; i < R.getColumnDimension(); i++) {
        //            assertEquals("  Uniqueness: ", true_uniqueness[i], Precision.round(factorMethod.getUniquenessAt(i), 4), 1e-1);
    }

}

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

public boolean isIndependent(Node x, Node y, List<Node> z) {
    if (verbose) {
        System.out.println("\n" + x + " _||_ " + y + " | " + z);
    }//  ww w . java  2 s . c o  m

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

    int[][] twod = new int[aa.size()][bb.size()];

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

    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 = Math.sqrt(v0 * v1);

            double r = a / b;

            int dof = cov.getSampleSize() - 1 - inverse.columns();

            if (dof < 0) {
                dof = 0;
            }

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

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

    // Sort pvalues, _i, and _m together.
    List<Double> pValues2 = new ArrayList<Double>();
    List<Integer> _i2 = new ArrayList<Integer>();
    List<Integer> _m2 = new ArrayList<Integer>();

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

    Collections.sort(pValues2);

    int k = StatUtils.fdr(alpha, pValues, false);

    int nonzero = -1;

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

    if (nonzero < k) {
        for (int g = 0; g < k; g++) {
            int x3 = _i2.get(g);
            int y3 = _m2.get(g);
            twod[x3][y3] = 1;
        }

        //            if (verbose) {
        //                System.out.println("Dependent");
        //            }

        return false;
    } else {
        if (verbose) {
            System.out.println("Independent");
        }

        return true;
    }
}

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

@Override
public int[] predict(RealMatrix newData) {
    @SuppressWarnings("unused")
    final int[] fit_labels = getLabels(); // throws the exception if not fit
    final int n = newData.getColumnDimension();

    if (n != this.data.getColumnDimension())
        throw new DimensionMismatchException(n, newData.getColumnDimension());

    // TODO: how to predict these???
    throw new UnsupportedOperationException("HDBSCAN does not currently support predictions");
}

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

@Test
public void m255TestMINRESOblimin() {
    System.out.println("MINRES Factor Analysis Test (Oblimin rotation): m255 data");
    RealMatrix R = new Array2DRowRealMatrix(readM255());
    ExploratoryFactorAnalysis fa = new ExploratoryFactorAnalysis(R, 3);
    fa.estimateParameters(EstimationMethod.MINRES, RotationMethod.OBLIMIN);
    System.out.println();/*www.j av a 2 s . co m*/
    System.out.println(fa.printOutput());

    //        double[][] true_loadings = {
    //                {0.6836,-0.3908,0.1130},
    //                {0.7002,-0.3712,0.1542},
    //                {0.7200,-0.2217,0.1227},
    //                {0.6285,-0.1843,0.1545},
    //                {0.7622,-0.0188,0.1045},
    //                {0.7216,0.3458,0.1759},
    //                {0.5962,0.4292,0.2326},
    //                {0.5214,0.1782,0.1026},
    //                {0.7102,0.1378,-0.0139},
    //                {0.5910,0.2662,-0.0076},
    //                {0.8389,0.0129,-0.3197},
    //                {0.6957,0.0292,-0.3029}
    //        };
    //
    //        double[] true_uniqueness = {0.3675, 0.3482, 0.4172, 0.5472, 0.4075, 0.3284, 0.4082, 0.6869, 0.4762, 0.5798,
    //                0.1942, 0.4219};

    FactorMethod factorMethod = fa.getFactorMethod();
    for (int i = 0; i < R.getColumnDimension(); i++) {
        for (int j = 0; j < 3; j++) {
            //                assertEquals("  Factor loadings: ", true_loadings[i][j], Precision.round(factorMethod.getFactorLoadingAt(i,j), 4), 1e-1);
        }
    }

    for (int i = 0; i < R.getColumnDimension(); i++) {
        //            assertEquals("  Uniqueness: ", true_uniqueness[i], Precision.round(factorMethod.getUniquenessAt(i), 4), 1e-1);
    }

}

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

@Test
public void m255TestPrincipalComponents() {
    System.out.println("Principal Components Analysis Test: m255 data");
    RealMatrix R = new Array2DRowRealMatrix(readM255());
    ExploratoryFactorAnalysis fa = new ExploratoryFactorAnalysis(R, 3);
    fa.estimateParameters(EstimationMethod.PRINCOMP);
    //        System.out.println();
    //        System.out.println(fa.printOutput());

    //True values obtained from R using code extracted from the psych package in R.
    //Needed to extract code from principal() function to get four decimal places.
    //principal(r=r, nfactors=3, rotate="none")
    double[][] true_loadings = { { 0.7020, -0.465531, 0.06718 }, { 0.7146, -0.437463, 0.11367 },
            { 0.7455, -0.299328, 0.12827 }, { 0.6779, -0.293400, 0.19308 }, { 0.7957, -0.073120, 0.05381 },
            { 0.7476, 0.354893, 0.13867 }, { 0.6342, 0.493008, 0.25213 }, { 0.5783, 0.298165, 0.36896 },
            { 0.7453, 0.159165, -0.14060 }, { 0.6373, 0.387494, -0.19624 }, { 0.8163, -0.009932, -0.34579 },
            { 0.7055, 0.016659, -0.51390 } };

    double[] true_uniqueness = { 0.2859, 0.2851, 0.3382, 0.4171, 0.3586, 0.2959, 0.2912, 0.4405, 0.3994, 0.4052,
            0.2140, 0.2379 };/*w ww.  j a v  a  2  s .c  o m*/

    FactorMethod factorMethod = fa.getFactorMethod();
    for (int i = 0; i < R.getColumnDimension(); i++) {
        for (int j = 0; j < 3; j++) {
            assertEquals("  Factor loadings: ", true_loadings[i][j],
                    Precision.round(factorMethod.getFactorLoadingAt(i, j), 4), 1e-4);
        }
    }

    for (int i = 0; i < R.getColumnDimension(); i++) {
        assertEquals("  Uniqueness: ", true_uniqueness[i], Precision.round(factorMethod.getUniquenessAt(i), 4),
                1e-4);
    }

}

From source file:edu.cudenver.bios.power.GLMMPowerCalculator.java

/**
 * Returns true if all values in the beta matrix are identical
 * @param beta beta matrix// w  w w. j a v a2s.  c  o m
 * @return true if no mean difference, false otherwise
 */
private boolean noMeanDifference(GLMMTest test) {
    // get the difference between theta null and the alternative
    RealMatrix sumSqHypothesis = test.getHypothesisSumOfSquares();
    // check if there is at least one non-zero value
    if (sumSqHypothesis != null) {
        for (int r = 0; r < sumSqHypothesis.getRowDimension(); r++) {
            for (int c = 0; c < sumSqHypothesis.getColumnDimension(); c++) {
                if (Math.abs(sumSqHypothesis.getEntry(r, c)) > EPSILON) {
                    return false;
                }
            }
        }
    }
    return true;
}