List of usage examples for org.apache.commons.math.linear RealMatrix inverse
@Deprecated
RealMatrix inverse() throws InvalidMatrixException;
From source file:lib.regressions.MultipleRegression.java
/** * Perform the regression computations//from w w w. j a v a 2 s .c om */ 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; }
From source file:tools.help.java
public static RealMatrix makeRealKronReduction(RealMatrix source, int ground_wires) { if (ground_wires == 0) { //no Kron Reduction needed return source; } else {//from w w w. ja v a 2 s. com int rows_total = source.getRowDimension(); int cols_total = source.getColumnDimension(); int nn_rows = rows_total - ground_wires; int nn_cols = cols_total - ground_wires; int nm_rows = rows_total - ground_wires; int nm_cols = ground_wires; int mn_rows = ground_wires; int mn_cols = cols_total - ground_wires; int mm_rows = ground_wires; int mm_cols = ground_wires; RealMatrix NN = new Array2DRowRealMatrix(nn_rows, nn_cols); RealMatrix MN = new Array2DRowRealMatrix(mn_rows, mn_cols); RealMatrix NM = new Array2DRowRealMatrix(nm_rows, nm_cols); RealMatrix MM = new Array2DRowRealMatrix(mm_rows, mm_cols); for (int i = 0; i < rows_total; i++) { for (int j = 0; j < cols_total; j++) { if (i < nn_rows && j < nn_cols) { NN.setEntry(i, j, source.getEntry(i, j)); } else if (i >= nn_rows && j < nn_cols) { MN.setEntry(i - nn_rows, j, source.getEntry(i, j)); } else if (i < nn_rows && j >= nn_cols) { NM.setEntry(i, j - nn_cols, source.getEntry(i, j)); } else if (i >= nn_rows && j >= nn_cols) { MM.setEntry(i - nn_rows, j - nn_cols, source.getEntry(i, j)); } } } return NN.subtract(NM.multiply(MM.inverse()).multiply(MN)); } }