Example usage for org.apache.commons.math.linear RealMatrix isSingular

List of usage examples for org.apache.commons.math.linear RealMatrix isSingular

Introduction

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

Prototype

@Deprecated
boolean isSingular();

Source Link

Document

Is this a singular matrix?

Usage

From source file:lib.regressions.MultipleRegression.java

/**
 * Perform the regression computations//from   w  ww  . j  a v  a 2 s.  c o  m
 */
private void compute() {
    // Set everything to 0.
    for (int i = 0; i < (myNumVar + 1); i++) {
        myCoef[i] = 0.0;
        myStdErr[i] = 0.0;
        myTStat[i] = 0.0;
    }
    myChiSq = 0.0;
    myRSq = 0.0;
    myAdjustedRSq = 0.0;

    // Set coefficients, t-stat, etc. if there has been enough data added.
    if (myCount >= (myNumVar + 1)) {
        RealMatrix dataMatrix = new RealMatrixImpl(mySums.getSumXX());
        RealMatrix xxMatrix = dataMatrix.getSubMatrix(1, myNumVar + 1, 1, myNumVar + 1);
        RealMatrix xyMatrix = dataMatrix.getSubMatrix(1, myNumVar + 1, 0, 0);

        computeOkX(); // Determine which X components to use.
        int[] listX = getListX();
        int[] listY = { 0 };
        int numX = listX.length;
        RealMatrix xxSubMatrix = xxMatrix.getSubMatrix(listX, listX);
        RealMatrix xySubMatrix = xyMatrix.getSubMatrix(listX, listY);

        double sumY = mySums.getSumXX()[0][1];
        double sumYY = mySums.getSumXX()[0][0];

        if (!xxSubMatrix.isSingular()) {
            RealMatrix xxInverse = xxSubMatrix.inverse();
            RealMatrix coefMatrix = xxInverse.multiply(xySubMatrix);
            double[] coef = coefMatrix.getColumn(0);

            // Compute chi-squared
            myChiSq = sumYY - 2 * coefMatrix.transpose().multiply(xySubMatrix).getEntry(0, 0)
                    + +coefMatrix.transpose().multiply(xxSubMatrix).multiply(coefMatrix).getEntry(0, 0);

            // Compute R^2 and adjusted R^2
            int offset = getUseIntercept() ? 1 : 0;
            myRSq = 1.0 - myChiSq / (sumYY - sumY * sumY / myCount);
            myAdjustedRSq = 1 - ((1 - myRSq) * (myCount - 1) + 1 - offset) / (myCount - numX);

            // Compute standard errors and t-stats
            int j = 0;
            for (int i = 0; i < (myNumVar + 1); i++) {
                if (myOkX[i]) {
                    j++;
                    myCoef[i] = coef[j - 1];
                    myStdErr[i] = Math.sqrt(myChiSq * xxInverse.getEntry(j - 1, j - 1) / (myCount - numX));
                    myTStat[i] = myCoef[i] / myStdErr[i];
                }
            }
        }

    }
    myIsComputed = true;
}