/** {@inheritDoc} */ @Override public BlockRealMatrix createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { return new BlockRealMatrix(rowDimension, columnDimension); }
/** {@inheritDoc} */ @Override public BlockRealMatrix scalarAdd(final double d) { final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k] + d; } } return out; }
/** {@inheritDoc} */ @Override public RealMatrix scalarMultiply(final double d) { final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k] * d; } } return out; }
/** * Create a PearsonsCorrelation from a rectangular array * whose columns represent values of variables to be correlated. * * Throws MathIllegalArgumentException if the input array 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 data rectangular array with columns representing variables * @throws MathIllegalArgumentException if the input data array is not * rectangular with at least two rows and two columns. * @see #correlation(double[], double[]) */ public PearsonsCorrelation(double[][] data) { this(new BlockRealMatrix(data)); }
/** * Create a Covariance matrix from a rectangular array * whose columns represent covariates. * * <p>The <code>biasCorrected</code> parameter determines whether or not * covariance estimates are bias-corrected.</p> * * <p>The input array must be rectangular with at least one column * and two rows.</p> * * @param data rectangular array with columns representing covariates * @param biasCorrected true means covariances are bias-corrected * @throws MathIllegalArgumentException if the input data array is not * rectangular with at least two rows and one column. * @throws NotStrictlyPositiveException if the input data array is not * rectangular with at least one row and one column. */ public Covariance(double[][] data, boolean biasCorrected) throws MathIllegalArgumentException, NotStrictlyPositiveException { this(new BlockRealMatrix(data), biasCorrected); }
/** {@inheritDoc} */ @Override public BlockRealMatrix copy() { // create an empty matrix BlockRealMatrix copied = new BlockRealMatrix(rows, columns); // copy the blocks for (int i = 0; i < blocks.length; ++i) { System.arraycopy(blocks[i], 0, copied.blocks[i], 0, blocks[i].length); } return copied; }
/** * Computes the Kendall's Tau rank correlation matrix for the columns of * the input rectangular array. The columns of the array represent values * of variables to be correlated. * * @param matrix matrix with columns representing variables to correlate * @return correlation matrix */ public RealMatrix computeCorrelationMatrix(final double[][] matrix) { return computeCorrelationMatrix(new BlockRealMatrix(matrix)); }
/** * Computes the Spearman's rank correlation matrix for the columns of the * input rectangular array. The columns of the array represent values * of variables to be correlated. * * @param matrix matrix with columns representing variables to correlate * @return correlation matrix */ public RealMatrix computeCorrelationMatrix(final double[][] matrix) { return computeCorrelationMatrix(new BlockRealMatrix(matrix)); }
/** * Compute a covariance matrix from a rectangular array whose columns represent * covariates. * @param data input array (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 data array does not contain sufficient * data * @throws NotStrictlyPositiveException if the input data array is not * rectangular with at least one row and one column. */ protected RealMatrix computeCovarianceMatrix(double[][] data, boolean biasCorrected) throws MathIllegalArgumentException, NotStrictlyPositiveException { return computeCovarianceMatrix(new BlockRealMatrix(data), biasCorrected); }
/** * Computes the correlation matrix for the columns of the * input rectangular array. The columns of the array represent values * of variables to be correlated. * * Throws MathIllegalArgumentException if the matrix does not have at least * two columns and two rows or if the array is not rectangular. Pairwise * correlations are set to NaN if one of the correlates has zero variance. * * @param data matrix with columns representing variables to correlate * @return correlation matrix * @throws MathIllegalArgumentException if the array does not contain sufficient data * @see #correlation(double[], double[]) */ public RealMatrix computeCorrelationMatrix(double[][] data) { return computeCorrelationMatrix(new BlockRealMatrix(data)); }
/** * Returns a {@link RealMatrix} with specified dimensions. * <p>The type of matrix returned depends on the dimension. Below * 2<sup>12</sup> elements (i.e. 4096 elements or 64×64 for a * square matrix) which can be stored in a 32kB array, a {@link * Array2DRowRealMatrix} instance is built. Above this threshold a {@link * BlockRealMatrix} instance is built.</p> * <p>The matrix elements are all set to 0.0.</p> * @param rows number of rows of the matrix * @param columns number of columns of the matrix * @return RealMatrix with specified dimensions * @see #createRealMatrix(double[][]) */ public static RealMatrix createRealMatrix(final int rows, final int columns) { return (rows * columns <= 4096) ? new Array2DRowRealMatrix(rows, columns) : new BlockRealMatrix(rows, columns); }
/** * Subtract {@code m} from this matrix. * * @param m Matrix to be subtracted. * @return {@code this} - m. * @throws MatrixDimensionMismatchException if {@code m} is not the * same size as this matrix. */ public BlockRealMatrix subtract(final BlockRealMatrix m) throws MatrixDimensionMismatchException { // safety check MatrixUtils.checkSubtractionCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; final double[] mBlock = m.blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k] - mBlock[k]; } } return out; }
/** * Compute the sum of this matrix and {@code m}. * * @param m Matrix to be added. * @return {@code this} + m. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as this matrix. */ public BlockRealMatrix add(final BlockRealMatrix m) throws MatrixDimensionMismatchException { // safety check MatrixUtils.checkAdditionCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform addition block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; final double[] mBlock = m.blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k] + mBlock[k]; } } return out; }
/** {@inheritDoc} */ @Override public BlockRealMatrix getColumnMatrix(final int column) throws OutOfRangeException { MatrixUtils.checkColumnIndex(this, column); final BlockRealMatrix out = new BlockRealMatrix(rows, 1); // perform copy block-wise, to ensure good cache behavior final int jBlock = column / BLOCK_SIZE; final int jColumn = column - jBlock * BLOCK_SIZE; final int jWidth = blockWidth(jBlock); int outBlockIndex = 0; int outIndex = 0; double[] outBlock = out.blocks[outBlockIndex]; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { if (outIndex >= outBlock.length) { outBlock = out.blocks[++outBlockIndex]; outIndex = 0; } outBlock[outIndex++] = block[i * jWidth + jColumn]; } } return out; }
/** * Returns a matrix of standard errors associated with the estimates * in the correlation matrix.<br/> * <code>getCorrelationStandardErrors().getEntry(i,j)</code> is the standard * error associated with <code>getCorrelationMatrix.getEntry(i,j)</code> * * <p>The formula used to compute the standard error is <br/> * <code>SE<sub>r</sub> = ((1 - r<sup>2</sup>) / (n - 2))<sup>1/2</sup></code> * where <code>r</code> is the estimated correlation coefficient and * <code>n</code> is the number of observations in the source dataset.</p> * * <p>To use this method, one of the constructors that supply an input * matrix must have been used to create this instance.</p> * * @return matrix of correlation standard errors * @throws NullPointerException if this instance was created with no data */ public RealMatrix getCorrelationStandardErrors() { int nVars = correlationMatrix.getColumnDimension(); double[][] out = new double[nVars][nVars]; for (int i = 0; i < nVars; i++) { for (int j = 0; j < nVars; j++) { double r = correlationMatrix.getEntry(i, j); out[i][j] = FastMath.sqrt((1 - r * r) /(nObs - 2)); } } return new BlockRealMatrix(out); }
public static RealMatrix convertToApacheMatrix(INDArray matrix) { if (matrix.rank() != 2) throw new IllegalArgumentException("Input rank is not 2 (not matrix)"); long[] shape = matrix.shape(); if (matrix.columns() > Integer.MAX_VALUE || matrix.rows() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); BlockRealMatrix out = new BlockRealMatrix((int) shape[0], (int) shape[1]); for (int i = 0; i < shape[0]; i++) { for (int j = 0; j < shape[1]; j++) { double value = matrix.getDouble(i, j); out.setEntry(i, j, value); } } return out; }
/** * 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; }
/** * 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; }
/** * 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; }