@Nonnull public static RealMatrix[] unflatten(@Nonnull final double[] data, final int rows, final int cols, final int len) { final RealMatrix[] grid = new RealMatrix[len]; int offset = 0; for (int k = 0; k < len; k++) { RealMatrix cell = new BlockRealMatrix(rows, cols); grid[k] = cell; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (offset >= data.length) { throw new IndexOutOfBoundsException("Offset " + offset + " exceeded data.length " + data.length); } double value = data[offset]; cell.setEntry(i, j, value); offset++; } } } if (offset != data.length) { throw new IllegalArgumentException("Invalid data for unflatten"); } return grid; }
@Nonnull public static RealMatrix combinedMatrices(@Nonnull final RealMatrix[][] grid, final int dimensions) { Preconditions.checkArgument(grid.length >= 1, "The number of rows must be greather than 1"); Preconditions.checkArgument(grid[0].length >= 1, "The number of cols must be greather than 1"); Preconditions.checkArgument(dimensions > 0, "Dimension should be more than 0: ", dimensions); final int rows = grid.length; final int cols = grid[0].length; final RealMatrix combined = new BlockRealMatrix(rows * dimensions, cols * dimensions); for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[row].length; col++) { combined.setSubMatrix(grid[row][col].getData(), row * dimensions, col * dimensions); } } return combined; }
@Nonnull public static RealMatrix combinedMatrices(@Nonnull final RealMatrix[] grid) { Preconditions.checkArgument(grid.length >= 1, "The number of rows must be greather than 0: " + grid.length); final int rows = grid.length; final int rowDims = grid[0].getRowDimension(); final int colDims = grid[0].getColumnDimension(); final RealMatrix combined = new BlockRealMatrix(rows * rowDims, colDims); for (int row = 0; row < grid.length; row++) { RealMatrix cell = grid[row]; Preconditions.checkArgument(cell.getRowDimension() == rowDims, "Mismatch in row dimensions at row ", row); Preconditions.checkArgument(cell.getColumnDimension() == colDims, "Mismatch in col dimensions at row ", row); combined.setSubMatrix(cell.getData(), row * rowDims, 0); } return combined; }
/** * Compute a covariance matrix from a matrix whose columns represent * covariates. * @param matrix input matrix (must have at least one column and two rows) * @param biasCorrected determines whether or not covariance estimates are bias-corrected * @return covariance matrix * @throws MathIllegalArgumentException if the matrix does not contain sufficient data */ protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected) throws MathIllegalArgumentException { 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), matrix.getColumn(j), biasCorrected); outMatrix.setEntry(i, j, cov); outMatrix.setEntry(j, i, cov); } outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i))); } return outMatrix; }
/** * Derives a correlation matrix from a covariance matrix. * * <p>Uses the formula <br/> * <code>r(X,Y) = cov(X,Y)/s(X)s(Y)</code> where * <code>r(·,·)</code> is the correlation coefficient and * <code>s(·)</code> means standard deviation.</p> * * @param covarianceMatrix the covariance matrix * @return correlation matrix */ public RealMatrix covarianceToCorrelation(RealMatrix covarianceMatrix) { int nVars = covarianceMatrix.getColumnDimension(); RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars); for (int i = 0; i < nVars; i++) { double sigma = FastMath.sqrt(covarianceMatrix.getEntry(i, i)); outMatrix.setEntry(i, i, 1d); for (int j = 0; j < i; j++) { double entry = covarianceMatrix.getEntry(i, j) / (sigma * FastMath.sqrt(covarianceMatrix.getEntry(j, j))); outMatrix.setEntry(i, j, entry); outMatrix.setEntry(j, i, entry); } } return outMatrix; }
@Test public void test2dDoubleArray() { final double[][] input = new double[][] { new double[] {2.0, 1.0, 2.0}, new double[] {1.0, 2.0, 1.0}, new double[] {0.0, 0.0, 0.0} }; final double[][] expected = new double[][] { new double[] {1.0, 1.0 / 3.0, 1.0}, new double[] {1.0 / 3.0, 1.0, 1.0 / 3.0}, new double[] {1.0, 1.0 / 3.0, 1.0}}; Assert.assertEquals(correlation.computeCorrelationMatrix(input), new BlockRealMatrix(expected)); }
@Test public void testBlockMatrix() { final double[][] input = new double[][] { new double[] {2.0, 1.0, 2.0}, new double[] {1.0, 2.0, 1.0}, new double[] {0.0, 0.0, 0.0} }; final double[][] expected = new double[][] { new double[] {1.0, 1.0 / 3.0, 1.0}, new double[] {1.0 / 3.0, 1.0, 1.0 / 3.0}, new double[] {1.0, 1.0 / 3.0, 1.0}}; Assert.assertEquals( correlation.computeCorrelationMatrix(new BlockRealMatrix(input)), new BlockRealMatrix(expected)); }
/** * Forked from Apache Commons Math * * @param stream the stream * @return covariance covariance */ public static RealMatrix getCovariance(final Supplier<Stream<double[]>> stream) { final int dimension = stream.get().findAny().get().length; final List<DoubleStatistics> statList = IntStream.range(0, dimension * dimension) .mapToObj(i -> new DoubleStatistics()).collect(Collectors.toList()); stream.get().forEach(array -> { for (int i = 0; i < dimension; i++) { for (int j = 0; j <= i; j++) { statList.get(i * dimension + j).accept(array[i] * array[j]); } } RecycleBinLong.DOUBLES.recycle(array, array.length); }); final RealMatrix covariance = new BlockRealMatrix(dimension, dimension); for (int i = 0; i < dimension; i++) { for (int j = 0; j <= i; j++) { final double v = statList.get(i * dimension + j).getAverage(); covariance.setEntry(i, j, v); covariance.setEntry(j, i, v); } } return covariance; }
@Test public void nonZeroScoreTest() { List<MultivariateDistribution> listDist = new ArrayList<>(3); double[] weights = {2. / 7, 3. / 7, 2. / 7}; double[][] distData = { {1.5, 2}, {0.5, 0.4, 0.4, 0.5}, {2000}, {2, 0}, {0.3, 0, 0, 0.6}, {3000}, {4.5, 1}, {0.9, 0.2, 0.2, 0.3}, {2000}}; for (int i = 0; i < distData.length; i += 3) { RealVector mean = new ArrayRealVector(distData[i + 0]); double[][] covArray = new double[2][2]; covArray[0] = Arrays.copyOfRange(distData[i + 1], 0, 2); covArray[1] = Arrays.copyOfRange(distData[i + 1], 2, 4); RealMatrix cov = new BlockRealMatrix(covArray); listDist.add(new MultivariateNormal(mean, cov)); } Mixture mixture = new Mixture(listDist, weights); assertEquals(0.155359, mixture.density(new ArrayRealVector(distData[0])), 1e-6); assertEquals(0.162771, mixture.density(new ArrayRealVector(distData[3])), 1e-6); assertEquals(0.094819, mixture.density(new ArrayRealVector(distData[6])), 1e-6); }
@Test public void sviStepTest() { assertEquals(0.9, VariationalInference.step(0, 1, 0.9), 1e-9); assertEquals(19.0, VariationalInference.step(10, 20, 0.9), 1e-9); assertEquals(12.0, VariationalInference.step(10, 20, 0.2), 1e-9); double[] array1 = {1, 2, 6}; double[] array2 = {2, 3, 8}; double[] array3 = {5, 6, 14}; RealVector start = new ArrayRealVector(array1); RealVector end = new ArrayRealVector(array3); assertEquals(new ArrayRealVector(array2), VariationalInference.step(start, end, 0.25)); double[][] matrix1 = {{1, 2}, {3, 10}}; double[][] matrix2 = {{2, 6}, {3, 100}}; double[][] matrix3 = {{5, 18}, {3, 370}}; RealMatrix s = new BlockRealMatrix(matrix1); RealMatrix e = new BlockRealMatrix(matrix3); assertEquals(new BlockRealMatrix(matrix2), VariationalInference.step(s, e, 0.25)); }
private void addSummary(String title, double[] sum1, double[] sum2) { BlockRealMatrix rm = new BlockRealMatrix(sum1.length, 2); rm.setColumn(0, sum1); rm.setColumn(1, sum2); SpearmansCorrelation sr = new SpearmansCorrelation(rm); PearsonsCorrelation p1 = sr.getRankCorrelation(); PearsonsCorrelation p2 = new PearsonsCorrelation(rm); StringBuilder sb = new StringBuilder(title); sb.append(sum1.length).append('\t'); sb.append(Utils.rounded(p1.getCorrelationMatrix().getEntry(0, 1))).append('\t'); sb.append(Utils.rounded(p1.getCorrelationPValues().getEntry(0, 1))).append('\t'); sb.append(Utils.rounded(p2.getCorrelationMatrix().getEntry(0, 1))).append('\t'); sb.append(Utils.rounded(p2.getCorrelationPValues().getEntry(0, 1))); twSummary.append(sb.toString()); }
/** * Returns a matrix of p-values associated with the (two-sided) null * hypothesis that the corresponding correlation coefficient is zero. * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability * that a random variable distributed as <code>t<sub>n-2</sub></code> takes * a value with absolute value greater than or equal to <br> * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p> * <p>The values in the matrix are sometimes referred to as the * <i>significance</i> of the corresponding correlation coefficients.</p> * * @return matrix of p-values * @throws org.apache.commons.math3.exception.MaxCountExceededException * if an error occurs estimating probabilities */ public RealMatrix getCorrelationPValues() { TDistribution tDistribution = new TDistribution(nObs - 2); int nVars = correlationMatrix.getColumnDimension(); double[][] out = new double[nVars][nVars]; for (int i = 0; i < nVars; i++) { for (int j = 0; j < nVars; j++) { if (i == j) { out[i][j] = 0d; } else { double r = correlationMatrix.getEntry(i, j); double t = FastMath.abs(r * FastMath.sqrt((nObs - 2)/(1 - r * r))); out[i][j] = 2 * tDistribution.cumulativeProbability(-t); } } } return new BlockRealMatrix(out); }
private static RealMatrix valueMatrixToRealMatrix(List<List<Double>> valueMatrix, List<FieldRecipe> fields){ RealMatrix matrix = new BlockRealMatrix(valueMatrix.size(), fields.size()); for(int i=0; i<valueMatrix.size(); i++){ // The i-th subject with non NaN values for all fields for (int j=0; j<fields.size(); j++){ // j-th field matrix.setEntry(i,j,valueMatrix.get(i).get(j)); } } return matrix; }
/** * Computes the Kendall's Tau rank correlation matrix for the columns of * the input matrix. * * @param matrix matrix with columns representing variables to correlate * @return correlation matrix */ public RealMatrix computeCorrelationMatrix(final RealMatrix matrix) { int nVars = matrix.getColumnDimension(); RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars); for (int i = 0; i < nVars; i++) { for (int j = 0; j < i; j++) { double corr = correlation(matrix.getColumn(i), matrix.getColumn(j)); outMatrix.setEntry(i, j, corr); outMatrix.setEntry(j, i, corr); } outMatrix.setEntry(i, i, 1d); } return outMatrix; }
/** * Computes the correlation matrix for the columns of the * input matrix, using {@link #correlation(double[], double[])}. * * Throws MathIllegalArgumentException if the matrix does not have at least * two columns and two rows. Pairwise correlations are set to NaN if one * of the correlates has zero variance. * * @param matrix matrix with columns representing variables to correlate * @return correlation matrix * @throws MathIllegalArgumentException if the matrix does not contain sufficient data * @see #correlation(double[], double[]) */ public RealMatrix computeCorrelationMatrix(RealMatrix matrix) { checkSufficientData(matrix); int nVars = matrix.getColumnDimension(); RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars); for (int i = 0; i < nVars; i++) { for (int j = 0; j < i; j++) { double corr = correlation(matrix.getColumn(i), matrix.getColumn(j)); outMatrix.setEntry(i, j, corr); outMatrix.setEntry(j, i, corr); } outMatrix.setEntry(i, i, 1d); } return outMatrix; }
protected RealMatrix createRealMatrix(double[] data, int nRows, int nCols) { double[][] matrixData = new double[nRows][nCols]; int ptr = 0; for (int i = 0; i < nRows; i++) { System.arraycopy(data, ptr, matrixData[i], 0, nCols); ptr += nCols; } return new BlockRealMatrix(matrixData); }
protected RealMatrix createLowerTriangularRealMatrix(double[] data, int dimension) { int ptr = 0; RealMatrix result = new BlockRealMatrix(dimension, dimension); for (int i = 1; i < dimension; i++) { for (int j = 0; j < i; j++) { result.setEntry(i, j, data[ptr]); ptr++; } } return result; }
public Matrix dense(RealMatrix matrix) { if (matrix == null) { throw new RuntimeException("matrix is null"); } else if (matrix instanceof BlockRealMatrix) { return dense((BlockRealMatrix) matrix); } else if (matrix instanceof Array2DRowRealMatrix) { return dense((Array2DRowRealMatrix) matrix); } else { throw new RuntimeException("implementation not available: " + matrix.getClass()); } }
/** * INDArray to Apache * * @param matrix rank-2 INDArray * @return Apache matrix */ public static RealMatrix convertINDArrayToApacheMatrix(@Nonnull final INDArray matrix) { Utils.validateArg(matrix.rank() == 2, "Input rank is not 2 (not matrix)"); final int[] shape = matrix.shape(); final INDArray concreteMatrix = matrix.isView() ? matrix.dup() : matrix; final double[] data = concreteMatrix.data().asDouble(); final char ordering = concreteMatrix.ordering(); if (ordering == 'c') { return new BlockRealMatrix(monoToBiDiArrayRowMajor(data, shape[0], shape[1])); } else { /* ordering == 'f' */ return new BlockRealMatrix(monoToBiDiArrayColumnMajor(data, shape[0], shape[1])); } }
@Test public void testApacheMatrixToINDArray() { /* row vector edge case */ assertApacheMatrixToINDArrayCorrectness( new BlockRealMatrix(new double[][] {{1.0, 2.0, 3.0}})); /* column vector edge case */ assertApacheMatrixToINDArrayCorrectness( new BlockRealMatrix(new double[][] {{1.0}, {2.0}, {3.0}})); /* general matrix */ assertApacheMatrixToINDArrayCorrectness( new BlockRealMatrix(new double[][] {{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}})); }
public void assertINDArrayToApacheMatrixCorrectness(final INDArray arr) { /* brute force result */ final RealMatrix expected = new BlockRealMatrix(arr.rows(), arr.columns()); for (int i = 0; i < arr.rows(); i++) { for (int j = 0; j < arr.columns(); j++) { expected.setEntry(i, j, arr.getDouble(i, j)); } } final RealMatrix result = Nd4jApacheAdapterUtils.convertINDArrayToApacheMatrix(arr); Assert.assertEquals(result.getColumnDimension(), expected.getColumnDimension()); Assert.assertEquals(result.getRowDimension(), expected.getRowDimension()); for (int i = 0; i < expected.getRowDimension(); i++) { ArrayAsserts.assertArrayEquals(result.getData()[i], expected.getData()[i], EPS); } }