List of usage examples for org.apache.commons.math.linear RealMatrix getColumn
double[] getColumn(int column) throws MatrixIndexException;
col as an array. From source file:net.sf.maltcms.chromaui.features.spi.FeatureTable.java
public static RealMatrix normalize(RealMatrix sourceMatrix, boolean center, boolean normalize) { RealMatrix normalized = MatrixUtils.createRealMatrix(sourceMatrix.getRowDimension(), sourceMatrix.getColumnDimension()); for (int col = 0; col < sourceMatrix.getColumnDimension(); col++) { double[] columnVector = sourceMatrix.getColumn(col); double mean = StatUtils.mean(columnVector); double stdev = Math.sqrt(StatUtils.variance(columnVector, mean)); Logger.getLogger(FeatureTable.class.getName()).log(Level.INFO, "column {0}, mean={1} stdev={2}", new Object[] { col, mean, stdev }); for (int j = 0; j < columnVector.length; j++) { normalized.setEntry(j, col, (sourceMatrix.getEntry(j, col) - mean) / stdev); }/*from w w w .ja va2 s .co m*/ } return normalized; }
From source file:fi.smaa.libror.PerformanceMatrix.java
private void initializeLevels(RealMatrix perfMatrix) { levels = new RealVector[perfMatrix.getColumnDimension()]; for (int i = 0; i < levels.length; i++) { Set<Double> levelsSet = new TreeSet<Double>(); for (double d : perfMatrix.getColumn(i)) { levelsSet.add(d);/* w ww .ja v a2 s . co m*/ } RealVector lvl = new ArrayRealVector(levelsSet.toArray(new Double[0])); levels[i] = lvl; } }
From source file:Covariance.java
/** * Compute a covariance matrix from a matrix whose columns represent * covariates. //from ww w . j ava 2 s . c om * @param matrix input matrix (must have at least two columns and two rows) * @param biasCorrected determines whether or not covariance estimates are bias-corrected * @return covariance matrix */ protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected) { int dimension = matrix.getColumnDimension(); Variance variance = new Variance(biasCorrected); RealMatrix outMatrix = new BlockRealMatrix(dimension, dimension); for (int i = 0; i < dimension; i++) { for (int j = 0; j < i; j++) { double cov = covariance(matrix.getColumn(i), i, matrix.getColumn(j), j, biasCorrected); outMatrix.setEntry(i, j, cov); outMatrix.setEntry(j, i, cov); } outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i))); } return outMatrix; }
From source file:geogebra.common.kernel.statistics.RegressionMath.java
/** Does the Polynom regression for degree > 4 */ public final boolean doPolyN(GeoList gl, int degree) { error = false;/*from w w w . j ava2 s . c o m*/ geolist = gl; size = geolist.size(); getPoints(); // getPoints from geolist if (error) { return false; } try { /* * Old Jama version: long time=System.currentTimeMillis(); * makeMatrixArrays(degree); //make marray and yarray Matrix M=new * Matrix(marray); Matrix Y=new Matrix(yarray); Matrix * Par=M.solve(Y); //Par.print(3,3); * pararray=Par.getRowPackedCopy(); * System.out.println(System.currentTimeMillis()-time); */ makeMatrixArrays(degree); // make marray and yarray RealMatrix M = new Array2DRowRealMatrix(marray, false); DecompositionSolver solver = new QRDecompositionImpl(M).getSolver(); // time=System.currentTimeMillis(); RealMatrix Y = new Array2DRowRealMatrix(yarray, false); RealMatrix P = solver.solve(Y); pararray = P.getColumn(0); // System.out.println(System.currentTimeMillis()-time); // diff(pararray,par); } catch (Throwable t) { App.debug(t.toString()); error = true; } // try-catch. ToDo: A bit more fine-grained error-handling... return !error; }
From source file:geogebra.kernel.statistics.RegressionMath.java
/** Does the Polynom regression for degree > 4*/ public final boolean doPolyN(GeoList gl, int degree) { error = false;//from www . java 2s .co m geolist = gl; size = geolist.size(); getPoints(); //getPoints from geolist if (error) { return false; } try { /* Old Jama version: long time=System.currentTimeMillis(); makeMatrixArrays(degree); //make marray and yarray Matrix M=new Matrix(marray); Matrix Y=new Matrix(yarray); Matrix Par=M.solve(Y); //Par.print(3,3); pararray=Par.getRowPackedCopy(); System.out.println(System.currentTimeMillis()-time); */ makeMatrixArrays(degree); //make marray and yarray RealMatrix M = new Array2DRowRealMatrix(marray, false); DecompositionSolver solver = new QRDecompositionImpl(M).getSolver(); //time=System.currentTimeMillis(); RealMatrix Y = new Array2DRowRealMatrix(yarray, false); RealMatrix P = solver.solve(Y); pararray = P.getColumn(0); //System.out.println(System.currentTimeMillis()-time); //diff(pararray,par); } catch (Throwable t) { Application.debug(t.toString()); error = true; } //try-catch. ToDo: A bit more fine-grained error-handling... return !error; }
From source file:lib.regressions.MultipleRegression.java
/** * Perform the regression computations/* w w w .j a va2 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; }
From source file:com.googlecode.routingways.solver.simplex.SimplexGeneratorImpl.java
private void generateMatrix(List<Point> pontos) { final int size = pontos.size(); final int others = (size - 2); final int columns = size * others; final int lines = 2 * size - 2; final int subCons = others * (others - 1); varInicio = new StringBuilder(); varFim = new StringBuilder(); objFunction = new double[columns]; matrix = new double[lines + subCons][columns]; rSide = new double[lines + subCons]; equalities = new double[lines + subCons]; int[] pos = new int[subCons]; RealMatrix localMatrix = new Array2DRowRealMatrix(size, size); RealMatrix secondCons = new Array2DRowRealMatrix(subCons, others); int idxObj = 0; int idxLig = 0; int idxSubCons = 0; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { Point inicio = pontos.get(i); Point fim = pontos.get(j); if (satisfiesObjectiveFunc(i, j, size)) { if (isValidSubCons(i, j, size)) { secondCons.setEntry(idxSubCons, i - 1, 1); secondCons.setEntry(idxSubCons, j - 1, -1); pos[idxSubCons++] = idxObj; }//from www . ja va 2s. c o m String aux = "S" + inicio.getName().toLowerCase() + "_" + fim.getName().toLowerCase(); varInicio.append(aux).append(' '); Segment seg = new Segment(inicio, fim); segments.put(aux, seg); objFunction[idxObj++] = seg.getLength(); localMatrix.setEntry(i, j, 1); } if (idxLig < lines + subCons) { rSide[idxLig] = (idxLig < (lines)) ? 1 : others - 1; equalities[idxLig] = (idxLig < (lines)) ? 0 : -1; idxLig++; } } if (!(i == 0 || i == size - 1)) { Point pt = pontos.get(i); String aux = "U" + pt.getName().toLowerCase(); varFim.append(aux).append(' '); } } int novaLinha = 0; for (int i = 0; i < localMatrix.getRowDimension(); i++) { double[] line = localMatrix.getRow(i); double[] column = localMatrix.getColumn(i); if (i == 0) { //starting matrix[novaLinha++] = lastLine(line, columns, i); } else if (i == (localMatrix.getRowDimension() - 1)) { //ending matrix[novaLinha++] = firstLine(column, columns, i); } else { //common case matrix[novaLinha++] = firstLine(column, columns, i); matrix[novaLinha++] = lastLine(line, columns, i); } } int dobro = 0; for (int i = lines; i < lines + subCons; i++) { double[] aux1 = secondCons.getRow(dobro); double[] aux2 = matrix[i]; System.arraycopy(aux1, 0, aux2, columns - others, aux1.length); aux2[pos[dobro++]] = others; matrix[i] = aux2; } }
From source file:geogebra.kernel.implicit.GeoImplicitPoly.java
/** * make curve through given points/*from w w w.j av a 2s. c o m*/ * @param points ArrayList of points */ public void throughPoints(ArrayList<GeoPoint> points) { if ((int) Math.sqrt(9 + 8 * points.size()) != Math.sqrt(9 + 8 * points.size())) { setUndefined(); return; } int degree = (int) (0.5 * Math.sqrt(8 * (1 + points.size()))) - 1; int realDegree = degree; RealMatrix extendMatrix = new RealMatrixImpl(points.size(), points.size() + 1); RealMatrix matrix = new RealMatrixImpl(points.size(), points.size()); double[][] coeffMatrix = new double[degree + 1][degree + 1]; DecompositionSolver solver; double[] matrixRow = new double[points.size() + 1]; double[] results; for (int i = 0; i < points.size(); i++) { double x = points.get(i).x / points.get(i).z; double y = points.get(i).y / points.get(i).z; for (int j = 0, m = 0; j < degree + 1; j++) for (int k = 0; j + k != degree + 1; k++) matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k); extendMatrix.setRow(i, matrixRow); } int solutionColumn = 0, noPoints = points.size(); do { if (solutionColumn > noPoints) { noPoints = noPoints - realDegree - 1; if (noPoints < 2) { setUndefined(); return; } extendMatrix = new RealMatrixImpl(noPoints, noPoints + 1); realDegree -= 1; matrixRow = new double[noPoints + 1]; for (int i = 0; i < noPoints; i++) { double x = points.get(i).x; double y = points.get(i).y; for (int j = 0, m = 0; j < realDegree + 1; j++) for (int k = 0; j + k != realDegree + 1; k++) matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k); extendMatrix.setRow(i, matrixRow); } matrix = new RealMatrixImpl(noPoints, noPoints); solutionColumn = 0; } results = extendMatrix.getColumn(solutionColumn); for (int i = 0, j = 0; i < noPoints + 1; i++) if (i == solutionColumn) continue; else matrix.setColumn(j++, extendMatrix.getColumn(i)); solutionColumn++; solver = new LUDecompositionImpl(matrix).getSolver(); } while (!solver.isNonSingular()); for (int i = 0; i < results.length; i++) results[i] *= -1; double[] partialSolution = solver.solve(results); double[] solution = new double[partialSolution.length + 1]; for (int i = 0, j = 0; i < solution.length; i++) if (i == solutionColumn - 1) solution[i] = 1; else { solution[i] = (Kernel.isZero(partialSolution[j])) ? 0 : partialSolution[j]; j++; } for (int i = 0, k = 0; i < realDegree + 1; i++) for (int j = 0; i + j < realDegree + 1; j++) coeffMatrix[i][j] = solution[k++]; this.setCoeff(coeffMatrix, true); this.defined = true; for (int i = 0; i < points.size(); i++) if (!this.isOnPath(points.get(i), 1)) { this.setUndefined(); return; } }
From source file:geogebra.kernel.GeoImplicitPoly.java
/** * make curve through given points//from w ww. j a v a 2 s . c o m * @param points ArrayList of points */ public void throughPoints(ArrayList<GeoPoint> points) { if ((int) Math.sqrt(9 + 8 * points.size()) != Math.sqrt(9 + 8 * points.size())) { setUndefined(); return; } if (pointsOnCurve == null) pointsOnCurve = new Coords[points.size()]; for (int i = 0; i < points.size(); i++) { if (pointsOnCurve[i] == null) pointsOnCurve[i] = new Coords(points.get(i).x, points.get(i).y, points.get(i).z); else { pointsOnCurve[i].setX(points.get(i).x); pointsOnCurve[i].setY(points.get(i).y); pointsOnCurve[i].setZ(points.get(i).z); } } int degree = (int) (0.5 * Math.sqrt(8 * (1 + points.size()))) - 1; int realDegree = degree; RealMatrix extendMatrix = new RealMatrixImpl(points.size(), points.size() + 1); RealMatrix matrix = new RealMatrixImpl(points.size(), points.size()); double[][] coeffMatrix = new double[degree + 1][degree + 1]; DecompositionSolver solver; double[] matrixRow = new double[points.size() + 1]; double[] results = new double[points.size()]; for (int i = 0; i < points.size(); i++) { double x = points.get(i).x / points.get(i).z; double y = points.get(i).y / points.get(i).z; for (int j = 0, m = 0; j < degree + 1; j++) for (int k = 0; j + k != degree + 1; k++) matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k); extendMatrix.setRow(i, matrixRow); } int solutionColumn = 0, noPoints = points.size(); do { if (solutionColumn > noPoints) { noPoints = noPoints - realDegree - 1; if (noPoints < 2) { setUndefined(); return; } extendMatrix = new RealMatrixImpl(noPoints, noPoints + 1); realDegree -= 1; matrixRow = new double[noPoints + 1]; for (int i = 0; i < noPoints; i++) { double x = points.get(i).x; double y = points.get(i).y; for (int j = 0, m = 0; j < realDegree + 1; j++) for (int k = 0; j + k != realDegree + 1; k++) matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k); extendMatrix.setRow(i, matrixRow); } matrix = new RealMatrixImpl(noPoints, noPoints); solutionColumn = 0; } results = extendMatrix.getColumn(solutionColumn); for (int i = 0, j = 0; i < noPoints; i++) if (i == solutionColumn) continue; else matrix.setColumn(j++, extendMatrix.getColumn(i)); solutionColumn++; solver = new LUDecompositionImpl(matrix).getSolver(); } while (!solver.isNonSingular()); for (int i = 0; i < results.length; i++) results[i] *= -1; double[] partialSolution = solver.solve(results); for (int i = 0; i < partialSolution.length; i++) if (Kernel.isZero(partialSolution[i])) partialSolution[i] = 0; for (int i = 0; i < partialSolution.length; i++) if (Kernel.isZero(partialSolution[i])) partialSolution[i] = 0; for (int i = 0, k = 0; i < realDegree + 1; i++) for (int j = 0; j + i != realDegree + 1; j++) if (k == solutionColumn - 1) coeffMatrix[i][j] = 1; else coeffMatrix[i][j] = partialSolution[k++]; this.setCoeff(coeffMatrix); this.update(); this.defined = true; for (int i = 0; i < points.size(); i++) if (!this.isOnPath(points.get(i))) { this.setUndefined(); return; } this.type = IMPLICIT_POLY_THROUGH_POINTS; }
From source file:geogebra.common.kernel.implicit.GeoImplicitPoly.java
/** * make curve through given points/*w ww . ja v a2 s . c o m*/ * @param points ArrayList of points */ public void throughPoints(ArrayList<GeoPoint> points) { if ((int) Math.sqrt(9 + 8 * points.size()) != Math.sqrt(9 + 8 * points.size())) { setUndefined(); return; } int degree = (int) (0.5 * Math.sqrt(8 * (1 + points.size()))) - 1; int realDegree = degree; RealMatrix extendMatrix = new Array2DRowRealMatrix(points.size(), points.size() + 1); RealMatrix matrix = new Array2DRowRealMatrix(points.size(), points.size()); double[][] coeffMatrix = new double[degree + 1][degree + 1]; DecompositionSolver solver; double[] matrixRow = new double[points.size() + 1]; double[] results; for (int i = 0; i < points.size(); i++) { double x = points.get(i).x / points.get(i).z; double y = points.get(i).y / points.get(i).z; for (int j = 0, m = 0; j < degree + 1; j++) for (int k = 0; j + k != degree + 1; k++) matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k); extendMatrix.setRow(i, matrixRow); } int solutionColumn = 0, noPoints = points.size(); do { if (solutionColumn > noPoints) { noPoints = noPoints - realDegree - 1; if (noPoints < 2) { setUndefined(); return; } extendMatrix = new Array2DRowRealMatrix(noPoints, noPoints + 1); realDegree -= 1; matrixRow = new double[noPoints + 1]; for (int i = 0; i < noPoints; i++) { double x = points.get(i).x; double y = points.get(i).y; for (int j = 0, m = 0; j < realDegree + 1; j++) for (int k = 0; j + k != realDegree + 1; k++) matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k); extendMatrix.setRow(i, matrixRow); } matrix = new Array2DRowRealMatrix(noPoints, noPoints); solutionColumn = 0; } results = extendMatrix.getColumn(solutionColumn); for (int i = 0, j = 0; i < noPoints + 1; i++) { if (i == solutionColumn) continue; matrix.setColumn(j++, extendMatrix.getColumn(i)); } solutionColumn++; solver = new LUDecompositionImpl(matrix).getSolver(); } while (!solver.isNonSingular()); for (int i = 0; i < results.length; i++) results[i] *= -1; double[] partialSolution = solver.solve(results); double[] solution = new double[partialSolution.length + 1]; for (int i = 0, j = 0; i < solution.length; i++) if (i == solutionColumn - 1) solution[i] = 1; else { solution[i] = (Kernel.isZero(partialSolution[j])) ? 0 : partialSolution[j]; j++; } for (int i = 0, k = 0; i < realDegree + 1; i++) for (int j = 0; i + j < realDegree + 1; j++) coeffMatrix[i][j] = solution[k++]; this.setCoeff(coeffMatrix, true); setDefined(); for (int i = 0; i < points.size(); i++) if (!this.isOnPath(points.get(i), 1)) { this.setUndefined(); return; } }