List of usage examples for org.apache.commons.math3.linear RealMatrix getColumnDimension
int getColumnDimension();
From source file:eagle.security.userprofile.model.kde.UserProfileKDEModeler.java
@Override public List<UserProfileKDEModel> 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);// w ww. java 2 s. c om computeProbabilityDensityEstimation(matrix); UserProfileKDEModel userprofileKDEModel = new UserProfileKDEModel(System.currentTimeMillis(), site, user, statistics, minProbabilityEstimate, maxProbabilityEstimate, nintyFivePercentileEstimate, medianProbabilityEstimate); return Arrays.asList(userprofileKDEModel); }
From source file:com.clust4j.algo.preprocess.StandardScaler.java
@Override public StandardScaler fit(RealMatrix data) { synchronized (fitLock) { final int m = data.getRowDimension(); final int n = data.getColumnDimension(); if (m < 2) throw new IllegalArgumentException( "cannot " + "meaningfully compute standard deviation " + "on fewer than two observations"); // need to mean center... this.means = new double[n]; this.stdevs = new double[n]; final double[][] X = data.getData(); for (int col = 0; col < n; col++) { double var, std, mn; double sumSq = 0.0; double sum = 0.0; for (int row = 0; row < m; row++) { sumSq += X[row][col] * X[row][col]; sum += X[row][col];/*from w ww . j a v a 2 s .c om*/ } /* * A naive algorithm to calculate the estimated variance (1M): * * Let n = 0, Sum = 0, SumSq = 0 * For each datum x: * n = n + 1 * Sum = Sum + x * SumSq = SumSq + x * x * Var = (SumSq - (Sum * Sum) / n) / (n - 1) * * @see https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance */ var = (sumSq - (sum * sum) / (double) m) / ((double) m - 1.0); std = m < 2 ? Double.NaN : FastMath.sqrt(var); mn = sum / (double) m; means[col] = mn; stdevs[col] = std; } return this; } }
From source file:edu.cudenver.bios.matrix.test.TestMatrixOrthonormalization.java
/** * Verify that the A = QxR for the Q, R matrices produced by the * orthonormalization/*from w w w . j a va 2s .c om*/ */ public void testQRshouldBeA() { RealMatrix Q = norm.getQ(); RealMatrix R = norm.getR(); // verify that QR = A RealMatrix shouldBeOriginalMatrix = Q.multiply(R); // Make sure that QR has same dimension as original A matrix if (shouldBeOriginalMatrix.getRowDimension() != data.length || shouldBeOriginalMatrix.getColumnDimension() != data[0].length) { fail(); } // make sure elements of QR match elements of A (within tolerance) for (int r = 0; r < shouldBeOriginalMatrix.getRowDimension(); r++) { for (int c = 0; c < shouldBeOriginalMatrix.getColumnDimension(); c++) { if (Precision.compareTo(shouldBeOriginalMatrix.getEntry(r, c), data[r][c], TOLERANCE) != 0) fail(); } } assertTrue(true); }
From source file:eagle.security.userprofile.impl.UserProfileAnomalyEigenEvaluator.java
private RealMatrix normalizeData(RealMatrix matrix, UserProfileEigenModel model) { RealMatrix normalizedData = new Array2DRowRealMatrix(matrix.getRowDimension(), matrix.getColumnDimension()); if (LOG.isDebugEnabled()) LOG.debug("model statistics size: " + model.statistics().length); for (int i = 0; i < matrix.getRowDimension(); i++) { for (int j = 0; j < matrix.getColumnDimension(); j++) { double value = (matrix.getEntry(i, j) - model.statistics()[j].getMean()) / model.statistics()[j].getStddev(); normalizedData.setEntry(i, j, value); }/*from w w w .j a v a2 s . c om*/ } return normalizedData; }
From source file:com.joptimizer.util.ColtUtils.java
/** * Returns a lower and an upper bound for the condition number * <br>kp(A) = Norm[A, p] / Norm[A^-1, p] * <br>where//from w ww.j a v a 2 s . c o m * <br> Norm[A, p] = sup ( Norm[A.x, p]/Norm[x, p] , x !=0 ) * <br>for a matrix and * <br> Norm[x, 1] := Sum[Math.abs(x[i]), i] * <br> Norm[x, 2] := Math.sqrt(Sum[Math.pow(x[i], 2), i]) * <br> Norm[x, 00] := Max[Math.abs(x[i]), i] * <br>for a vector. * * @param A matrix you want the condition number of * @param p norm order (2 or Integer.MAX_VALUE) * @return an array with the two bounds (lower and upper bound) * * @see Ravindra S. Gajulapalli, Leon S. Lasdon "Scaling Sparse Matrices for Optimization Algorithms" */ public static double[] getConditionNumberRange(RealMatrix A, int p) { double infLimit = Double.NEGATIVE_INFINITY; double supLimit = Double.POSITIVE_INFINITY; List<Double> columnNormsList = new ArrayList<Double>(); switch (p) { case 2: for (int j = 0; j < A.getColumnDimension(); j++) { columnNormsList.add(A.getColumnVector(j).getL1Norm()); } Collections.sort(columnNormsList); //kp >= Norm[Ai, p]/Norm[Aj, p], for each i, j = 0,1,...,n, Ak columns of A infLimit = columnNormsList.get(columnNormsList.size() - 1) / columnNormsList.get(0); break; case Integer.MAX_VALUE: double normAInf = A.getNorm(); for (int j = 0; j < A.getColumnDimension(); j++) { columnNormsList.add(A.getColumnVector(j).getLInfNorm()); } Collections.sort(columnNormsList); //k1 >= Norm[A, +oo]/min{ Norm[Aj, +oo], for each j = 0,1,...,n }, Ak columns of A infLimit = normAInf / columnNormsList.get(0); break; default: throw new IllegalArgumentException("p must be 2 or Integer.MAX_VALUE"); } return new double[] { infLimit, supLimit }; }
From source file:fi.smaa.jsmaa.model.MultivariateGaussianCriterionMeasurement.java
private RealMatrix setCovarianceMatrixInternal(RealMatrix newValue) { if (newValue.getRowDimension() != alternatives.size() || newValue.getColumnDimension() != alternatives.size()) { throw new IllegalArgumentException( "Incorrect matrix size " + newValue.getRowDimension() + "x" + newValue.getColumnDimension() + ", expected " + alternatives.size() + "x" + alternatives.size()); }/*w ww.j ava 2 s . c o m*/ try { mvgGenerator = random == null ? new MultivariateGaussianGenerator(meanVector, newValue, null) : random.createMultivariateGaussian(meanVector, newValue); } catch (Exception e) { throw new IllegalArgumentException(e); } RealMatrix oldValue = covarianceMatrix; covarianceMatrix = newValue; return oldValue; }
From source file:com.clust4j.algo.preprocess.RobustScaler.java
@Override public RobustScaler fit(RealMatrix X) { synchronized (fitLock) { this.centerer = new MedianCenterer().fit(X); // Get percentile final int n = X.getColumnDimension(); double[][] transpose = X.transpose().getData(); // top row will be 25th, bottom 75 double[][] quantiles_25_75 = new double[2][n]; // Quantile engine DescriptiveStatistics stats;/*w w w. ja v a 2 s . com*/ for (int j = 0; j < n; j++) { stats = new DescriptiveStatistics(); for (int i = 0; i < transpose[j].length; i++) { stats.addValue(transpose[j][i]); } quantiles_25_75[0][j] = stats.getPercentile(25); quantiles_25_75[0][j] = stats.getPercentile(75); } // set the scale this.scale = VecUtils.subtract(quantiles_25_75[1], quantiles_25_75[0]); // If we have a constant value, we might get zeroes in the scale: for (int i = 0; i < scale.length; i++) { if (scale[i] == 0) { scale[i] = 1.0; } } return this; } }
From source file:edu.cudenver.bios.matrix.GramSchmidtOrthonormalization.java
/** * Perform Gram Schmidt Orthonormalization on the specified * matrix. The matrix A (mxn) is decomposed into two matrices * Q (mxn), R (nxn) such that// w ww. ja v a 2 s . co m * <ul> * <li>A = QR * <li>Q'Q = Identity * <li>R is upper triangular * </ul> * The resulting Q, R matrices can be retrieved with the getQ() * and getR() functions. * * @param matrix */ public GramSchmidtOrthonormalization(RealMatrix matrix) { if (matrix == null) throw new IllegalArgumentException("Null matrix"); // create the Q, R matrices int m = matrix.getRowDimension(); int n = matrix.getColumnDimension(); Q = MatrixUtils.createRealMatrix(m, n); R = MatrixUtils.createRealMatrix(n, n); // perform Gram Schmidt process using the following algorithm // let w<n> be the resulting orthonormal column vectors // let v<n> be the columns of the incoming matrix // w1 = (1/norm(v1))*v1 // ... // wj = 1/norm(vj - projectionVj-1Vj)*[vj - projectionVj-1Vj] // where projectionVj-1Vj = (w1 * vj) * w1 + (w2 * vj) * w2 + ... + (wj-1 * vj) * wj-1 // for (int i = 0; i < n; i++) { RealMatrix v = matrix.getColumnMatrix(i); for (int j = 0; j < i; j++) { RealMatrix Qj = Q.getColumnMatrix(j); double value = Qj.transpose().multiply(v).getEntry(0, 0); R.setEntry(j, i, value); v = v.subtract(Qj.scalarMultiply(value)); } double norm = v.getFrobeniusNorm(); R.setEntry(i, i, norm); Q.setColumnMatrix(i, v.scalarMultiply(1 / norm)); } }
From source file:com.cloudera.oryx.common.math.RRQRDecomposition.java
/** * Return the effective numerical matrix rank. * <p>The effective numerical rank is the number of non-negligible * singular values.</p>/*from w w w . j a va 2 s.c o m*/ * <p>This implementation looks at Frobenius norms of the sequence of * bottom right submatrices. When a large fall in norm is seen, * the rank is returned. The drop is computed as:</p> * {@code (thisNorm/lastNorm) * rNorm < dropThreshold } * <p> * where thisNorm is the Frobenius norm of the current submatrix, * lastNorm is the Frobenius norm of the previous submatrix, * rNorm is is the Frobenius norm of the complete matrix * </p> * * @param dropThreshold threshold triggering rank computation * @return effective numerical matrix rank */ public int getRank(final double dropThreshold) { RealMatrix r = getR(); int rows = r.getRowDimension(); int columns = r.getColumnDimension(); int rank = 1; double lastNorm = r.getFrobeniusNorm(); double rNorm = lastNorm; while (rank < Math.min(rows, columns)) { double thisNorm = r.getSubMatrix(rank, rows - 1, rank, columns - 1).getFrobeniusNorm(); if (thisNorm == 0 || (thisNorm / lastNorm) * rNorm < dropThreshold) { break; } lastNorm = thisNorm; rank++; } return rank; }
From source file:com.clust4j.algo.preprocess.WeightTransformer.java
@Override public WeightTransformer fit(RealMatrix X) { synchronized (fitLock) { // Only enforce this to prevent accidental exceptions later if the user // tries a fit(X).transform(X) and later gets a dim mismatch... if (X.getColumnDimension() != n) throw new DimensionMismatchException(n, X.getColumnDimension()); return this; }/* w w w . j a v a 2 s . co m*/ }