List of usage examples for org.apache.commons.math3.linear RealMatrix addToEntry
void addToEntry(int row, int column, double increment) throws OutOfRangeException;
From source file:com.rapidminer.tools.math.LinearRegression.java
/** Calculates the coefficients of linear ridge regression. */ public static double[] performRegression(Matrix a, Matrix b, double ridge) { RealMatrix x = MatrixUtils.createRealMatrix(a.getArray()); RealMatrix y = MatrixUtils.createRealMatrix(b.getArray()); int numberOfColumns = x.getColumnDimension(); double[] coefficients = new double[numberOfColumns]; RealMatrix xTransposed = x.transpose(); Matrix result;/*from ww w . j av a 2 s. com*/ boolean finished = false; while (!finished) { RealMatrix xTx = xTransposed.multiply(x); for (int i = 0; i < numberOfColumns; i++) { xTx.addToEntry(i, i, ridge); } RealMatrix xTy = xTransposed.multiply(y); coefficients = xTy.getColumn(0); try { // do not use Apache LUDecomposition for solve instead because it creates different // results result = new Matrix(xTx.getData()).solve(new Matrix(coefficients, coefficients.length)); for (int i = 0; i < numberOfColumns; i++) { coefficients[i] = result.get(i, 0); } finished = true; } catch (Exception ex) { double ridgeOld = ridge; if (ridge > 0) { ridge *= 10; } else { ridge = 0.0000001; } finished = false; logger.warning("Error during calculation: " + ex.getMessage() + ": Increasing ridge factor from " + ridgeOld + " to " + ridge); } } return coefficients; }
From source file:edu.byu.nlp.stats.DirichletMLEOptimizableTest.java
/** * Computes a Newton-Raphson update in-place to alpha. *//*from w w w . j a va 2 s .c o m*/ private static RealVector newtonRaphsonUpdate(final double[][] data, double[] alpha) { // We'll compute the gold-standard value the "long" way (taking the inverse of the Hessian) RealMatrix hessian = new Array2DRowRealMatrix(alpha.length, alpha.length); for (int r = 0; r < hessian.getRowDimension(); r++) { for (int c = 0; c < hessian.getColumnDimension(); c++) { hessian.addToEntry(r, c, data.length * Gamma.trigamma(DoubleArrays.sum(alpha))); if (r == c) { hessian.addToEntry(r, c, -data.length * Gamma.trigamma(alpha[r])); } } } RealVector derivative = new ArrayRealVector(alpha.length); for (int k = 0; k < alpha.length; k++) { derivative.setEntry(k, data.length * (Gamma.digamma(DoubleArrays.sum(alpha)) - Gamma.digamma(alpha[k]))); for (double[] theta : data) { derivative.addToEntry(k, theta[k]); } } RealMatrix hessianInverse = new LUDecomposition(hessian).getSolver().getInverse(); RealVector negDiff = hessianInverse.preMultiply(derivative); negDiff.mapMultiplyToSelf(-1.0); RealVector expected = new ArrayRealVector(alpha, true); return expected.add(negDiff); }
From source file:com.anhth12.lambda.common.math.VectorMath.java
public static RealMatrix transposeTimesSelf(Collection<float[]> M) { if (M == null || M.isEmpty()) { return null; }//from w w w .j a va2s . c o m int features = 0; RealMatrix result = null; for (float[] vector : M) { if (result == null) { features = vector.length; result = new Array2DRowRealMatrix(features, features); } for (int row = 0; row < features; row++) { float rowValue = vector[row]; for (int col = 0; col < features; col++) { result.addToEntry(row, col, rowValue * vector[col]); } } } return result; }
From source file:com.cloudera.oryx.als.computation.iterate.row.RowReduceFn.java
private static RealMatrix partialTransposeTimesSelf(LongObjectMap<float[]> M, int dimension, Iterable<LongFloatMap.MapEntry> entries) { RealMatrix result = new Array2DRowRealMatrix(dimension, dimension); for (LongFloatMap.MapEntry entry : entries) { float[] vector = M.get(entry.getKey()); for (int row = 0; row < dimension; row++) { float rowValue = vector[row]; for (int col = 0; col < dimension; col++) { result.addToEntry(row, col, rowValue * vector[col]); }// ww w .j av a 2 s. c om } } return result; }
From source file:com.cloudera.oryx.kmeans.common.ClusterValidityStatistics.java
public static ClusterValidityStatistics create(Iterable<WeightedRealVector> points, Centers test, Centers train, int replicaId) { Preconditions.checkArgument(test.size() == train.size()); int k = test.size(); double n = 0.0; double testCost = 0.0; double trainCost = 0.0; RealMatrix contingencyMatrix = new Array2DRowRealMatrix(k, k); double[] rowSums = new double[k]; double[] colSums = new double[k]; for (WeightedRealVector wv : points) { Distance d1 = test.getDistance(wv.thing()); Distance d2 = train.getDistance(wv.thing()); contingencyMatrix.addToEntry(d1.getClosestCenterId(), d2.getClosestCenterId(), wv.weight()); rowSums[d1.getClosestCenterId()] += wv.weight(); colSums[d2.getClosestCenterId()] += wv.weight(); testCost += d1.getSquaredDistance() * wv.weight(); trainCost += d2.getSquaredDistance() * wv.weight(); n += wv.weight();/*from ww w.ja va 2 s. co m*/ } return new ClusterValidityStatistics(k, replicaId, testCost, trainCost, normVarInformation(contingencyMatrix, rowSums, colSums, n), normVanDongen(contingencyMatrix, rowSums, colSums, n)); }
From source file:com.cloudera.oryx.common.math.VectorMath.java
/** * @param M tall, skinny matrix//from w w w. j ava 2 s. co m * @return MT * M as a dense matrix */ public static RealMatrix transposeTimesSelf(Collection<float[]> M) { if (M == null || M.isEmpty()) { return null; } int features = 0; RealMatrix result = null; for (float[] vector : M) { if (result == null) { features = vector.length; result = new Array2DRowRealMatrix(features, features); } for (int row = 0; row < features; row++) { float rowValue = vector[row]; for (int col = 0; col < features; col++) { result.addToEntry(row, col, rowValue * vector[col]); } } } return result; }
From source file:cooccurrence.Omer_Levy.java
/** * Generic Method to take squareRoot of individual Values. * @param coVariance/*ww w .j av a2 s . c o m*/ * @return */ private static RealMatrix squareRoot(RealMatrix coVariance) { RealMatrix squareRoot = MatrixUtils.createRealMatrix(coVariance.getRowDimension(), coVariance.getColumnDimension()); for (int i = 0; i < coVariance.getRowDimension(); i++) { for (int j = 0; j < coVariance.getColumnDimension(); j++) { double val = coVariance.getEntry(i, j); val = Math.sqrt(val); squareRoot.addToEntry(i, j, val); } } return squareRoot; }
From source file:com.cloudera.oryx.common.math.MatrixUtils.java
/** * @param M tall, skinny matrix/*from ww w . ja v a 2 s .c o m*/ * @return MT * M as a dense matrix */ public static RealMatrix transposeTimesSelf(LongObjectMap<float[]> M) { if (M == null || M.isEmpty()) { return null; } RealMatrix result = null; for (LongObjectMap.MapEntry<float[]> entry : M.entrySet()) { float[] vector = entry.getValue(); int dimension = vector.length; if (result == null) { result = new Array2DRowRealMatrix(dimension, dimension); } for (int row = 0; row < dimension; row++) { float rowValue = vector[row]; for (int col = 0; col < dimension; col++) { result.addToEntry(row, col, rowValue * vector[col]); } } } Preconditions.checkNotNull(result); return result; }
From source file:com.yahoo.egads.utilities.SpectralMethods.java
public static RealMatrix averageHankelMatrix(RealMatrix hankelMat, int windowSize) { int k = hankelMat.getRowDimension(); int m = hankelMat.getColumnDimension() / windowSize; int n = k + windowSize - 1; RealMatrix result = MatrixUtils.createRealMatrix(n, m); for (int t = 0; t < n; ++t) { int i = (t < windowSize) ? 0 : (t - windowSize + 1); int j = (t < windowSize) ? t : (windowSize - 1); int counter = 0; for (; i < k && j >= 0; ++i, --j, ++counter) { for (int h = 0; h < m; ++h) { result.addToEntry(t, h, hankelMat.getEntry(i, j * m + h)); }/* ww w. j av a 2s .c om*/ } for (int h = 0; h < m; ++h) { result.setEntry(t, h, result.getEntry(t, h) / counter); } } return result; }
From source file:net.myrrix.common.math.MatrixUtils.java
/** * @param M tall, skinny matrix//from ww w .ja v a2 s. c o m * @return MT * M as a dense matrix */ public static RealMatrix transposeTimesSelf(FastByIDMap<float[]> M) { if (M == null || M.isEmpty()) { return null; } RealMatrix result = null; for (FastByIDMap.MapEntry<float[]> entry : M.entrySet()) { float[] vector = entry.getValue(); int dimension = vector.length; if (result == null) { result = new Array2DRowRealMatrix(dimension, dimension); } for (int row = 0; row < dimension; row++) { float rowValue = vector[row]; for (int col = 0; col < dimension; col++) { result.addToEntry(row, col, rowValue * vector[col]); } } } Preconditions.checkNotNull(result); return result; }