List of usage examples for org.apache.commons.math3.linear RealMatrix getRowDimension
int getRowDimension();
From source file:gov.anl.cue.arcane.engine.matrix.MatrixModel.java
/** * Simple maximum fitness function.//from w ww. j a v a2s . c o m * * @return the double */ public double simpleMaximumFitnessFunction() { // Declare the results holder. double fitnessAccumulator = 0.0; // Scan the variables. for (MatrixVariable matrixVariable : this) { // Scan the matrix rows. RealMatrix coefficients = matrixVariable.coefficients; for (int row = 0; row < coefficients.getRowDimension(); row++) { // Scan the matrix columns. double rowMax = 0.0; for (int column = 0; column < coefficients.getColumnDimension(); column++) { // Check the next cell. if (!Double.isNaN(coefficients.getEntry(row, column))) { rowMax = Math.max(rowMax, coefficients.getEntry(row, column)); } } // Accumulate the value. fitnessAccumulator += rowMax; } } // Return the results. return fitnessAccumulator; }
From source file:com.datumbox.framework.machinelearning.featureselection.continuous.PCA.java
@Override protected void estimateModelParameters(Dataset originaldata) { ModelParameters modelParameters = knowledgeBase.getModelParameters(); int n = originaldata.size(); int d = originaldata.getColumnSize(); //convert data into matrix Map<Object, Integer> feature2ColumnId = modelParameters.getFeature2ColumnId(); MatrixDataset matrixDataset = MatrixDataset.newInstance(originaldata, false, feature2ColumnId); RealMatrix X = matrixDataset.getX(); //calculate means and subtract them from data double[] meanValues = new double[d]; for (Map.Entry<Object, Integer> entry : feature2ColumnId.entrySet()) { Object feature = entry.getKey(); Integer columnId = entry.getValue(); meanValues[columnId] = Descriptives .mean(originaldata.extractColumnValues(feature).toFlatDataCollection()); for (int row = 0; row < n; ++row) { X.addToEntry(row, columnId, -meanValues[columnId]); //inplace subtraction!!! }//w w w . j a v a 2s . co m } modelParameters.setMean(meanValues); RealMatrix components; double[] eigenValues; /* if(d>n) { //turned off because of the algorithm could not be validated //Karhunen Lowe Transform to speed up calculations //nxn matrix RealMatrix covarianceNN = (X.multiply(X.transpose())).scalarMultiply(1.0/(n-1.0)); EigenDecomposition decomposition = new EigenDecomposition(covarianceNN); eigenValues = decomposition.getRealEigenvalues(); RealMatrix eigenVectors = decomposition.getV(); double[] sqrtInverseEigenValues = new double[eigenValues.length]; for(int i=0;i<eigenValues.length;++i) { if(eigenValues[i]==0.0) { sqrtInverseEigenValues[i] = 0.0; } else { sqrtInverseEigenValues[i] = 1.0/Math.sqrt(eigenValues[i]); } } components = X.transpose().multiply(eigenVectors); //Components = X'*V*L^-0.5; To whiten them we multiply with L^0.5 which //cancels out the previous multiplication. So below we multiply by //L^-0.5 ONLY if we don't whiten. if(!knowledgeBase.getTrainingParameters().isWhitened()) { components = components.multiply(new DiagonalMatrix(sqrtInverseEigenValues)); } } else { //Normal PCA goes here } */ //dxd matrix RealMatrix covarianceDD = (X.transpose().multiply(X)).scalarMultiply(1.0 / (n - 1.0)); EigenDecomposition decomposition = new EigenDecomposition(covarianceDD); eigenValues = decomposition.getRealEigenvalues(); components = decomposition.getV(); //Whiten Components W = U*L^0.5; To whiten them we multiply with L^0.5. if (knowledgeBase.getTrainingParameters().isWhitened()) { double[] sqrtEigenValues = new double[eigenValues.length]; for (int i = 0; i < eigenValues.length; ++i) { sqrtEigenValues[i] = Math.sqrt(eigenValues[i]); } components = components.multiply(new DiagonalMatrix(sqrtEigenValues)); } //the eigenvalues and their components are sorted by descending order no need to resort them Integer maxDimensions = knowledgeBase.getTrainingParameters().getMaxDimensions(); Double varianceThreshold = knowledgeBase.getTrainingParameters().getVarianceThreshold(); if (varianceThreshold != null && varianceThreshold <= 1) { double sum = 0.0; double totalVariance = StatUtils.sum(eigenValues); int varCounter = 0; for (double l : eigenValues) { sum += l / totalVariance; ++varCounter; if (sum >= varianceThreshold) { break; } } if (maxDimensions == null || maxDimensions > varCounter) { maxDimensions = varCounter; } } if (maxDimensions != null && maxDimensions < d) { //keep only the maximum selected eigenvalues double[] newEigenValues = new double[maxDimensions]; System.arraycopy(eigenValues, 0, newEigenValues, 0, maxDimensions); eigenValues = newEigenValues; //keep only the maximum selected eigenvectors components = components.getSubMatrix(0, components.getRowDimension() - 1, 0, maxDimensions - 1); } modelParameters.setRows(components.getRowDimension()); modelParameters.setCols(components.getColumnDimension()); modelParameters.setEigenValues(eigenValues); modelParameters.setComponents(components.getData()); }
From source file:nova.core.util.math.MatrixUtil.java
public static RealMatrix augment(RealMatrix matrix, int rows, int columns) { if (matrix.getRowDimension() > rows) throw new DimensionMismatchException(of("rows: {0} !< {1}"), matrix.getRowDimension(), rows); if (matrix.getColumnDimension() > columns) throw new DimensionMismatchException(of("columns: {0} !< {1}"), matrix.getColumnDimension(), columns); RealMatrix augmented = MatrixUtils.createRealMatrix(rows, columns); augmented.setSubMatrix(matrix.getData(), 0, 0); return augmented; }
From source file:nova.core.util.math.MatrixUtil.java
public static RealMatrix augmentWithIdentity(RealMatrix matrix, int dimensions) { RealMatrix augmented = augment(matrix, dimensions, dimensions); for (int i = MathUtil.max(matrix.getRowDimension(), matrix.getColumnDimension()) + 1; i <= dimensions; i++) { augmented.setEntry(i - 1, i - 1, 1); }// w w w . j av a2s . c om return augmented; }
From source file:org.akvo.caddisfly.sensor.colorimetry.strip.calibration.CalibrationCard.java
@NonNull private static Mat doIlluminationCorrection(@NonNull Mat imgLab, @NonNull CalibrationData calData) { // create HLS image for homogeneous illumination calibration int pHeight = imgLab.rows(); int pWidth = imgLab.cols(); RealMatrix points = createWhitePointMatrix(imgLab, calData); // create coefficient matrix for all three variables L,A,B // the model for all three is y = ax + bx^2 + cy + dy^2 + exy + f // 6th row is the constant 1 RealMatrix coefficient = new Array2DRowRealMatrix(points.getRowDimension(), 6); coefficient.setColumnMatrix(0, points.getColumnMatrix(0)); coefficient.setColumnMatrix(2, points.getColumnMatrix(1)); //create constant, x^2, y^2 and xy terms for (int i = 0; i < points.getRowDimension(); i++) { coefficient.setEntry(i, 1, Math.pow(coefficient.getEntry(i, 0), 2)); // x^2 coefficient.setEntry(i, 3, Math.pow(coefficient.getEntry(i, 2), 2)); // y^2 coefficient.setEntry(i, 4, coefficient.getEntry(i, 0) * coefficient.getEntry(i, 2)); // xy coefficient.setEntry(i, 5, 1d); // constant = 1 }//from w ww . j a v a 2 s.c o m // create vectors RealVector L = points.getColumnVector(2); RealVector A = points.getColumnVector(3); RealVector B = points.getColumnVector(4); // solve the least squares problem for all three variables DecompositionSolver solver = new SingularValueDecomposition(coefficient).getSolver(); RealVector solutionL = solver.solve(L); RealVector solutionA = solver.solve(A); RealVector solutionB = solver.solve(B); // get individual coefficients float La = (float) solutionL.getEntry(0); float Lb = (float) solutionL.getEntry(1); float Lc = (float) solutionL.getEntry(2); float Ld = (float) solutionL.getEntry(3); float Le = (float) solutionL.getEntry(4); float Lf = (float) solutionL.getEntry(5); float Aa = (float) solutionA.getEntry(0); float Ab = (float) solutionA.getEntry(1); float Ac = (float) solutionA.getEntry(2); float Ad = (float) solutionA.getEntry(3); float Ae = (float) solutionA.getEntry(4); float Af = (float) solutionA.getEntry(5); float Ba = (float) solutionB.getEntry(0); float Bb = (float) solutionB.getEntry(1); float Bc = (float) solutionB.getEntry(2); float Bd = (float) solutionB.getEntry(3); float Be = (float) solutionB.getEntry(4); float Bf = (float) solutionB.getEntry(5); // compute mean (the luminosity value of the plane in the middle of the image) float L_mean = (float) (0.5 * La * pWidth + 0.5 * Lc * pHeight + Lb * pWidth * pWidth / 3.0 + Ld * pHeight * pHeight / 3.0 + Le * 0.25 * pHeight * pWidth + Lf); float A_mean = (float) (0.5 * Aa * pWidth + 0.5 * Ac * pHeight + Ab * pWidth * pWidth / 3.0 + Ad * pHeight * pHeight / 3.0 + Ae * 0.25 * pHeight * pWidth + Af); float B_mean = (float) (0.5 * Ba * pWidth + 0.5 * Bc * pHeight + Bb * pWidth * pWidth / 3.0 + Bd * pHeight * pHeight / 3.0 + Be * 0.25 * pHeight * pWidth + Bf); // Correct image // we do this per row. We tried to do it in one block, but there is no speed difference. byte[] temp = new byte[imgLab.cols() * imgLab.channels()]; int valL, valA, valB; int ii, ii3; float iiSq, iSq; int imgCols = imgLab.cols(); int imgRows = imgLab.rows(); // use lookup tables to speed up computation // create lookup tables float[] L_aii = new float[imgCols]; float[] L_biiSq = new float[imgCols]; float[] A_aii = new float[imgCols]; float[] A_biiSq = new float[imgCols]; float[] B_aii = new float[imgCols]; float[] B_biiSq = new float[imgCols]; float[] Lci = new float[imgRows]; float[] LdiSq = new float[imgRows]; float[] Aci = new float[imgRows]; float[] AdiSq = new float[imgRows]; float[] Bci = new float[imgRows]; float[] BdiSq = new float[imgRows]; for (ii = 0; ii < imgCols; ii++) { iiSq = ii * ii; L_aii[ii] = La * ii; L_biiSq[ii] = Lb * iiSq; A_aii[ii] = Aa * ii; A_biiSq[ii] = Ab * iiSq; B_aii[ii] = Ba * ii; B_biiSq[ii] = Bb * iiSq; } for (int i = 0; i < imgRows; i++) { iSq = i * i; Lci[i] = Lc * i; LdiSq[i] = Ld * iSq; Aci[i] = Ac * i; AdiSq[i] = Ad * iSq; Bci[i] = Bc * i; BdiSq[i] = Bd * iSq; } // We can also improve the performance of the i,ii term, if we want, but it won't make much difference. for (int i = 0; i < imgRows; i++) { // y imgLab.get(i, 0, temp); ii3 = 0; for (ii = 0; ii < imgCols; ii++) { //x valL = capValue( Math.round((temp[ii3] & 0xFF) - (L_aii[ii] + L_biiSq[ii] + Lci[i] + LdiSq[i] + Le * i * ii + Lf) + L_mean), 0, 255); valA = capValue( Math.round((temp[ii3 + 1] & 0xFF) - (A_aii[ii] + A_biiSq[ii] + Aci[i] + AdiSq[i] + Ae * i * ii + Af) + A_mean), 0, 255); valB = capValue( Math.round((temp[ii3 + 2] & 0xFF) - (B_aii[ii] + B_biiSq[ii] + Bci[i] + BdiSq[i] + Be * i * ii + Bf) + B_mean), 0, 255); temp[ii3] = (byte) valL; temp[ii3 + 1] = (byte) valA; temp[ii3 + 2] = (byte) valB; ii3 += 3; } imgLab.put(i, 0, temp); } return imgLab; }
From source file:org.apache.eagle.security.userprofile.model.eigen.UserProfileEigenModeler.java
@Override public List<UserProfileEigenModel> generate(String site, String user, RealMatrix matrix) { LOG.info(String.format("Receive aggregated user activity matrix: %s size: %s x %s", user, matrix.getRowDimension(), matrix.getColumnDimension())); computeStats(matrix);/*from w w w . j av a 2 s .c o m*/ RealMatrix normalizedInputMatrix = normalizeData(matrix); int lowVariantColumnCount = 0; for (int j = 0; j < normalizedInputMatrix.getColumnDimension(); j++) { if (statistics[j].isLowVariant()) { lowVariantColumnCount++; } } if (normalizedInputMatrix.getColumnDimension() == lowVariantColumnCount) { LOG.info("found user: " + user + " with all features being low variant. Nothing to do..."); UserProfileEigenModel noopModel = new UserProfileEigenModel(System.currentTimeMillis(), site, user, null, null, 0, null, null, null, null, null, statistics); return Arrays.asList(noopModel); } else { computeCovarianceAndSVD(normalizedInputMatrix, lowVariantColumnCount); computeDimensionWithMaxVariance(); computePrincipalComponents(); maximumL2Norm = new ArrayRealVector(principalComponents.length); minimumL2Norm = new ArrayRealVector(principalComponents.length); for (int i = 0; i < principalComponents.length; i++) { RealMatrix trainingDataTranspose = computeMaxDistanceOnPCs(i); } UserProfileEigenModel userprofileEigenModel = new UserProfileEigenModel(System.currentTimeMillis(), site, user, uMatrix, diagonalMatrix, dimension, minVector, maxVector, principalComponents, maximumL2Norm, minimumL2Norm, statistics); return Arrays.asList(userprofileEigenModel); } }
From source file:org.apache.eagle.security.userprofile.TestUserProfileAnomalyEigenEvaluator.java
@Test public void testDetect() { UserProfileAnomalyEigenEvaluator eigenEvaluator = new UserProfileAnomalyEigenEvaluator(); String[] testCmdType = { "getfileinfo", "open", "listStatus", "setTimes", "setPermission", "rename", "mkdirs", "create", "setReplication", "contentSummary", "delete", "setOwner", "fsck" }; List<String> tmpCmdTypesAsList = new ArrayList<String>(); tmpCmdTypesAsList = Arrays.asList(testCmdType); Seq<String> testCmd = JavaConversions.asScalaBuffer(tmpCmdTypesAsList); String testSite = "sandbox"; long testTimestamp = 14054440; String testUser = "test_user"; RealMatrix testMatrix = new Array2DRowRealMatrix(1, testCmdType.length); for (int i = 0; i < testMatrix.getColumnDimension(); i++) testMatrix.addToEntry(0, i, 3.0); UserActivityAggModel testAggModel = new UserActivityAggModel(testUser, testMatrix, testCmd, testSite, testTimestamp);/*from ww w. j av a 2 s . c o m*/ Long testVersion = new Long(1); RealMatrix testUMat = new Array2DRowRealMatrix(testCmdType.length, testCmdType.length); RealMatrix testDiagonalMat = new Array2DRowRealMatrix(testCmdType.length, testCmdType.length); for (int i = 0; i < testCmdType.length; i++) { for (int j = 0; j < testCmdType.length; j++) { testUMat.addToEntry(i, j, 1.0); testDiagonalMat.addToEntry(i, j, 1.0); } } int dimension = testCmdType.length - 1; double[] minVector = new double[testCmdType.length]; double[] maxVector = new double[testCmdType.length]; for (int i = 0; i < minVector.length; i++) { minVector[i] = 1; maxVector[i] = 1; } RealVector testMinVec = new ArrayRealVector(minVector); RealVector testMaxVec = new ArrayRealVector(maxVector); RealVector[] testPCs = new ArrayRealVector[testCmdType.length]; for (int i = 0; i < testCmdType.length; i++) { testPCs[i] = new ArrayRealVector(testCmdType.length); for (int j = 0; j < testPCs[i].getDimension(); j++) { testPCs[i].addToEntry(j, 1.0); } } RealVector testMaxL2Norm = new ArrayRealVector(maxVector); RealVector testMinL2Norm = new ArrayRealVector(minVector); UserCommandStatistics userCommandStatistics[] = new UserCommandStatistics[testCmdType.length]; for (int i = 0; i < testCmdType.length; i++) { userCommandStatistics[i] = new UserCommandStatistics(); userCommandStatistics[i].setCommandName(testCmdType[i]); userCommandStatistics[i].setLowVariant(false); userCommandStatistics[i].setMean(1.0); userCommandStatistics[i].setStddev(1.0); } UserProfileEigenModel testEigenModel = new UserProfileEigenModel(testVersion, testSite, testUser, testUMat, testDiagonalMat, dimension, testMinVec, testMaxVec, testPCs, testMaxL2Norm, testMinL2Norm, userCommandStatistics); List<MLCallbackResult> testResults = eigenEvaluator.detect("test_user", "eigen", testAggModel, testEigenModel); Assert.assertEquals(testResults.size(), testMatrix.getRowDimension()); for (MLCallbackResult result : testResults) { Assert.assertEquals(result.isAnomaly(), true); } }
From source file:org.briljantframework.array.Matrices.java
/** * Convert the real matrix to a double array. * /*from w ww. j a va 2s . c om*/ * @param matrix the matrix * @return a new array */ public static DoubleArray toArray(RealMatrix matrix) { DoubleArray array = Arrays.doubleArray(matrix.getRowDimension(), matrix.getColumnDimension()); matrix.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { @Override public void visit(int row, int column, double value) { array.set(row, column, value); } }); return array; }
From source file:org.drugis.addis.presentation.SMAASerializer.java
private void insertPerCriterionMeasurement(ObjectMapper mapper, FullJointMeasurements m, ArrayNode performancesNode) {// w w w. j av a 2 s .c o m PerCriterionMeasurements measurements = (PerCriterionMeasurements) m; for (Criterion criterion : measurements.getCriteria()) { ObjectNode measurementNode = (ObjectNode) mapper.createObjectNode(); measurementNode.put("criterion", toSlug(criterion.getName())); ObjectNode performanceNode = (ObjectNode) mapper.createObjectNode(); CriterionMeasurement criterionMeasurement = measurements.getCriterionMeasurement(criterion); Class<? extends CriterionMeasurement> measurementType = criterionMeasurement.getClass(); if (measurementType.equals(RelativeLogitGaussianCriterionMeasurement.class) || measurementType.equals(RelativeGaussianCriterionMeasurement.class)) { ObjectNode parameterNode = (ObjectNode) mapper.createObjectNode(); String type = measurementType.equals(RelativeLogitGaussianCriterionMeasurement.class) ? "relative-logit-normal" : "relative-normal"; performanceNode.put("type", type); GaussianMeasurement baseline; MultivariateGaussianCriterionMeasurement relativeMeasurement; CriterionMeasurement measurement = measurementType.cast(criterionMeasurement); if (measurementType.equals(RelativeLogitGaussianCriterionMeasurement.class)) { RelativeLogitGaussianCriterionMeasurement tmp = (RelativeLogitGaussianCriterionMeasurement) measurement; baseline = tmp.getGaussianMeasurement().getBaselineMeasurement(); relativeMeasurement = tmp.getGaussianMeasurement().getRelativeMeasurement(); } else { RelativeGaussianCriterionMeasurement tmp = (RelativeGaussianCriterionMeasurement) measurement; baseline = tmp.getBaselineMeasurement(); relativeMeasurement = tmp.getRelativeMeasurement(); } // Add baseline ObjectNode baselineNode = (ObjectNode) mapper.createObjectNode(); baselineNode.put("type", "dnorm"); baselineNode.put("name", toSlug(d_analysis.getBaseline().getLabel())); baselineNode.put("mu", baseline.getMean()); baselineNode.put("sigma", baseline.getStDev()); parameterNode.put("baseline", baselineNode); // Add relative ObjectNode relativeNode = (ObjectNode) mapper.createObjectNode(); relativeNode.put("type", "dmnorm"); ObjectNode relativeMuNode = (ObjectNode) mapper.createObjectNode(); ObjectNode relativeCovNode = (ObjectNode) mapper.createObjectNode(); ArrayNode relativeCovRowNames = (ArrayNode) mapper.createArrayNode(); ArrayNode relativeCovColNames = (ArrayNode) mapper.createArrayNode(); for (int i = 0; i < relativeMeasurement.getAlternatives().size(); ++i) { String alternative = toSlug(relativeMeasurement.getAlternatives().get(i).getName()); relativeCovRowNames.add(alternative); relativeCovColNames.add(alternative); relativeMuNode.put(alternative, relativeMeasurement.getMeanVector().getEntry(i)); } relativeCovNode.put("colnames", relativeCovColNames); relativeCovNode.put("rownames", relativeCovRowNames); ArrayNode relativeCovDataNode = (ArrayNode) mapper.createArrayNode(); RealMatrix covarianceMatrix = relativeMeasurement.getCovarianceMatrix(); for (int i = 0; i < covarianceMatrix.getRowDimension(); ++i) { ArrayNode row = (ArrayNode) mapper.createArrayNode(); for (int j = 0; j < covarianceMatrix.getRowDimension(); ++j) { row.add(covarianceMatrix.getRow(i)[j]); } relativeCovDataNode.add(row); } relativeCovNode.put("data", relativeCovDataNode); relativeNode.put("mu", relativeMuNode); relativeNode.put("cov", relativeCovNode); parameterNode.put("relative", relativeNode); performanceNode.put("parameters", parameterNode); } measurementNode.put("performance", performanceNode); performancesNode.add(measurementNode); } }
From source file:org.eclipse.dataset.LinearAlgebra.java
private static Dataset createDataset(RealMatrix m) { DoubleDataset r = new DoubleDataset(m.getRowDimension(), m.getColumnDimension()); if (m instanceof Array2DRowRealMatrix) { double[][] data = ((Array2DRowRealMatrix) m).getDataRef(); IndexIterator it = r.getIterator(true); int[] pos = it.getPos(); while (it.hasNext()) { r.setAbs(it.index, data[pos[0]][pos[1]]); }/*from ww w .ja v a2s . c om*/ } else { IndexIterator it = r.getIterator(true); int[] pos = it.getPos(); while (it.hasNext()) { r.setAbs(it.index, m.getEntry(pos[0], pos[1])); } } return r; }