/** * truncated SVD as taken from http://stackoverflow.com/questions/19957076/best-way-to-compute-a-truncated-singular-value-decomposition-in-java */ static double[][] getTruncatedSVD(double[][] matrix, final int k) { SingularValueDecomposition svd = new SingularValueDecomposition(MatrixUtils.createRealMatrix(matrix)); double[][] truncatedU = new double[svd.getU().getRowDimension()][k]; svd.getU().copySubMatrix(0, truncatedU.length - 1, 0, k - 1, truncatedU); double[][] truncatedS = new double[k][k]; svd.getS().copySubMatrix(0, k - 1, 0, k - 1, truncatedS); double[][] truncatedVT = new double[k][svd.getVT().getColumnDimension()]; svd.getVT().copySubMatrix(0, k - 1, 0, truncatedVT[0].length - 1, truncatedVT); RealMatrix approximatedSvdMatrix = (MatrixUtils.createRealMatrix(truncatedU)).multiply( MatrixUtils.createRealMatrix(truncatedS)).multiply(MatrixUtils.createRealMatrix(truncatedVT)); return approximatedSvdMatrix.getData(); }
public void apacheCommonsExample() { double[][] A = { {0.1950, 0.0311}, {0.3588, 0.2203}, {0.1716, 0.5931}, {0.2105, 0.3242}}; double[][] B = { {0.0502, 0.9823, 0.9472}, {0.5732, 0.2694, 0.916}}; RealMatrix aRealMatrix = new Array2DRowRealMatrix(A); RealMatrix bRealMatrix = new Array2DRowRealMatrix(B); RealMatrix cRealMatrix = aRealMatrix.multiply(bRealMatrix); System.out.println(); for (int i = 0; i < cRealMatrix.getRowDimension(); i++) { System.out.println(cRealMatrix.getRowVector(i)); } }
@Test() public void testRealMatrixUpdates() { final DataFrame<String,String> frame = TestDataFrames.random(double.class, 100, 100); final RealMatrix matrix = frame.export().asApacheMatrix(); Assert.assertEquals(frame.rowCount(), matrix.getRowDimension(), "Row count matches"); Assert.assertEquals(frame.colCount(), matrix.getColumnDimension(), "Column count matches"); for (int i=0; i<frame.rowCount(); ++i) { for (int j = 0; j<frame.colCount(); ++j) { matrix.setEntry(i, j, Math.random()); } } for (int i=0; i<frame.rowCount(); ++i) { for (int j = 0; j<frame.colCount(); ++j) { final double v1 = frame.data().getDouble(i, j); final double v2 = matrix.getEntry(i, j); Assert.assertEquals(v1, v2, "Values match at " + i + "," + j); } } }
/** * Creates the X design matrix for this regression model * @return the X design matrix */ RealMatrix createX() { final int n = frame.rows().count(); final int offset = hasIntercept() ? 1 : 0; final int p = hasIntercept() ? regressors.size() + 1 : regressors.size(); final int[] colIndexes = regressors.stream().mapToInt(k -> frame.cols().ordinalOf(k)).toArray(); final RealMatrix x = new Array2DRowRealMatrix(n, p); for (int i = 0; i < n; ++i) { x.setEntry(i, 0, 1d); for (int j = offset; j < p; ++j) { final double value = frame.data().getDouble(i, colIndexes[j - offset]); x.setEntry(i, j, value); } } return x; }
@Test(dataProvider = "styles") public void testPseudoInverse(DataFrameAlgebra.Lib lib, boolean parallel) { DataFrameAlgebra.LIBRARY.set(lib); final DataFrame<Integer,String> source = DataFrame.read().csv("./src/test/resources/pca/svd/poppet-svd-eigenvectors.csv"); Array.of(20, 77, 95, 135, 233, 245).forEach(count -> { final DataFrame<Integer,String> frame = source.cols().select(col -> col.ordinal() < count); final DataFrame<Integer,Integer> inverse = frame.inverse(); final RealMatrix matrix = new QRDecomposition(toMatrix(frame)).getSolver().getInverse(); assertEquals(inverse, matrix); }); }
public static RealMatrix multiplyElementWise(final RealMatrix matrix1, final RealMatrix matrix2) { if (matrix1.getRowDimension() != matrix2.getRowDimension() || matrix1 .getColumnDimension() != matrix2.getColumnDimension()) { throw new IllegalArgumentException( "The matrices must be of the same dimensions!"); } final RealMatrix result = matrix1.createMatrix( matrix1.getRowDimension(), matrix1.getColumnDimension()); for (int r = 0; r < matrix1.getRowDimension(); r++) { for (int c = 0; c < matrix1.getColumnDimension(); c++) { result.setEntry(r, c, matrix1.getEntry(r, c) * matrix2.getEntry(r, c)); } } return result; }
public static double[] calculateRowVectorDistances(final RealMatrix matrix) { final double[] distances = new double[matrix.getRowDimension() * (matrix.getRowDimension() - 1) / 2]; int count = 1; int iterator = 0; for (int r1 = 0; r1 < matrix.getRowDimension(); r1++) { for (int r2 = count; r2 < matrix.getRowDimension(); r2++) { distances[iterator++] = matrix.getRowVector(r1) .getDistance(matrix.getRowVector(r2)); } count++; } return distances; }
private static RealMatrix centerKernelMatrix(final RealMatrix kernelMatrix) { // get size of kernelMatrix final int n = kernelMatrix.getRowDimension(); // get mean values for each row/column final RealVector columnMeans = MatrixFunctions.columnMeans(kernelMatrix); final double matrixMean = MatrixFunctions.mean(kernelMatrix); RealMatrix centeredKernelMatrix = kernelMatrix.copy(); for (int k = 0; k < n; k++) { centeredKernelMatrix.setRowVector(k, centeredKernelMatrix.getRowVector(k).subtract(columnMeans)); centeredKernelMatrix.setColumnVector(k, centeredKernelMatrix .getColumnVector(k).subtract(columnMeans)); } centeredKernelMatrix = centeredKernelMatrix.scalarAdd(matrixMean); return centeredKernelMatrix; }
private RealMatrix makeDataMatrix(List<double[]> X, double[] meanX) { if (meanX == null) { return makeDataMatrix(X); } final int m = X.size(); final int n = X.get(0).length; RealMatrix M = MatrixUtils.createRealMatrix(n, m); RealVector mean = MatrixUtils.createRealVector(meanX); int i = 0; for (double[] x : X) { RealVector xi = MatrixUtils.createRealVector(x).subtract(mean); M.setColumnVector(i, xi); i++; } return M; }
public static void main(String[] args) { // test wishart double[][] s = {{2.0,1.0,0.0},{1.0,2.0,1.0},{0.0,1.0,2.0}}; RealMatrix S = new Array2DRowRealMatrix(s); Cholesky C = new Cholesky(S); double df = 2.4; RealMatrix sum = new Array2DRowRealMatrix(3,3); for (int i=0; i<100000; i++) { RealMatrix sample = generator.nextWishart(df, C); sum = sum.add(sample); } sum = sum.scalarMultiply(1.0/100000.0); System.out.println(sum.getRowVector(0)); System.out.println(sum.getRowVector(1)); System.out.println(sum.getRowVector(2)); }
public MixModel(MethyModel tumor, MethyModel normal, RealVector thetas, int nBetas, int MYTHREADS) throws InterruptedException { int nFeatures=tumor.getNaRatio().getDimension(); this.nBetas = nBetas; RealVector betas = new ArrayRealVector(nBetas); for (int i=0; i<nBetas; i++) { betas.setEntry(i,i/(nBetas-1.0)); } mixDens = new RealMatrix[nFeatures]; ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS); for(int i = 0; i < nFeatures; i++) { double tumorAlpha = tumor.getAlpha().getEntry(i); double tumorBeta = tumor.getBeta().getEntry(i); BetaDistribution tumorDist = new BetaDistribution(tumorAlpha,tumorBeta); double normalAlpha = normal.getAlpha().getEntry(i); double normalBeta = normal.getBeta().getEntry(i); BetaDistribution normalDist = new BetaDistribution(normalAlpha,normalBeta); Runnable worker = new CalMixDens(tumorDist,normalDist,thetas,betas,nPoints,i,mixDens); executor.execute(worker); } executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(10000); } }
public MixModel selectFeature(boolean[] selectedFeatures) { MixModel newModel = new MixModel(); int nSelectedFeature = 0; for (boolean select:selectedFeatures) { if (select) {nSelectedFeature++;} } RealMatrix[] newMixDens = new RealMatrix[nSelectedFeature]; int j = 0; for(int i=0; i<selectedFeatures.length; i++) { if (selectedFeatures[i]) { newMixDens[j] = this.mixDens[i]; j++; } } newModel.setMixDens(newMixDens); return newModel; }
public static RealMatrix correlation2Distance(RealMatrix rMat) { // Copy to retain Dimensions RealMatrix dMat = rMat.copy(); for (int row = 0; row < rMat.getRowDimension(); row++) { for (int col = 0; col < rMat.getColumnDimension(); col++) { double r = rMat.getEntry(row, col); //Apply cosine theorem: //https://stats.stackexchange.com/questions/165194/using-correlation-as-distance-metric-for-hierarchical-clustering double d = Math.sqrt(2*(1-r)); dMat.setEntry(row, col, d); } } return dMat; }
@Test public void testCorrelation() throws Exception { TreeParser parser = new TreeParser(); parser.parseTree("./res/nodes.dmp", "./res/names.dmp"); TaxonId2CountCSVParser csvParser = new TaxonId2CountCSVParser(parser.getTaxonTree()); ArrayList<Sample> samples = new ArrayList<>(); samples.addAll(csvParser.parse("./res/testFiles/megan_examples/core1_activelayer_day2-ID2Count.txt")); samples.addAll(csvParser.parse("./res/testFiles/megan_examples/core1_activelayer_day7-ID2Count.txt")); samples.addAll(csvParser.parse("./res/testFiles/megan_examples/core1_activelayer_frozen-ID2Count.txt")); RealMatrix correlationMatrix = SampleComparison.getCorrelationMatrixOfSamples(); System.out.println("Correlation Matrix:"); printMatrix(correlationMatrix); System.out.println(); RealMatrix correlationPValues = SampleComparison.getCorrelationPValuesOfSamples(); System.out.println("P-Value matrix:"); printMatrix(correlationPValues); }
/** * Returns the 'root' (U) of the inverse covariance matrix S^{-1}, * such that S^{-1} = U^T . U * This matrix can be used to pre-transform the original sample * vectors X (by X → U . X) to a space where distance measurement (in the Mahalanobis * sense) can be calculated with the usual Euclidean norm. * The matrix U is invertible in case the reverse mapping is required. * * @return The matrix for pre-transforming the original sample vectors. */ public double[][] getWhiteningTransformation() { IJ.log("in whitening"); double relativeSymmetryThreshold = 1.0E-6; // CholeskyDecomposition.DEFAULT_RELATIVE_SYMMETRY_THRESHOLD == 1.0E-15; too small! double absolutePositivityThreshold = 1.0E-10; // CholeskyDecomposition.DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD == 1.0E-10; CholeskyDecomposition cd = new CholeskyDecomposition(MatrixUtils.createRealMatrix(iCov), relativeSymmetryThreshold, absolutePositivityThreshold); RealMatrix U = cd.getLT(); return U.getData(); }
public static double[] solve(final double[][] A, double[] b) { RealMatrix AA = MatrixUtils.createRealMatrix(A); RealVector bb = MatrixUtils.createRealVector(b); DecompositionSolver solver = new LUDecomposition(AA).getSolver(); double[] x = null; try { x = solver.solve(bb).toArray(); } catch (SingularMatrixException e) {} return x; }
/** * * @param matrix * @return */ public static double[][] invertMatrix(double[][] matrix){ Array2DRowRealMatrix rMatrix=new Array2DRowRealMatrix(matrix); RealMatrix inv=MatrixUtils.inverse(rMatrix); double[][]invHermite=inv.getData(); return invHermite; }
@Override public RealMatrix getTransformationMatrix() { RealMatrix cR = R.scalarMultiply(c); RealMatrix M = MatrixUtils.createRealMatrix(n, n + 1); M.setSubMatrix(cR.getData(), 0, 0); M.setColumnVector(n, t); return M; }
/** * esta funcion devuelve en una lista de valores, la correlación entre las variables accel-x accel - y | accel - x accel - z | accel - y accel - z * Es importante que en el dataframe este ordenado, ya que el acceso se realiza de manera manual * 2ºAccels (x, y, z) * 1ºGyros (a , b , g) * * @param df * @return */ //PASAR SOLO EL DF DE LOS ACCEL private List giveMeCorrelation(DataFrame df){ List retList = new ArrayList(); double[][] miMatrix = (double[][]) df.toArray(double[][].class); RealMatrix rm = new PearsonsCorrelation(miMatrix).getCorrelationMatrix(); double [][] matrixDebug = rm.getData(); retList.add(matrixDebug[3][4]); retList.add(matrixDebug[3][5]); retList.add(matrixDebug[4][5]); return retList; }
@Override public SlopeCoefficients estimateCoefficients(final DerivationEquation eq) throws EstimationException { final double[][] sourceTriangleMatrix = eq.getCovarianceLowerTriangularMatrix(); // Copy matrix and enhance it to a full matrix as expected by CholeskyDecomposition // FIXME: Avoid copy job to speed-up the solving process e.g. by extending the CholeskyDecomposition constructor final int length = sourceTriangleMatrix.length; final double[][] matrix = new double[length][]; for (int i = 0; i < length; i++) { matrix[i] = new double[length]; final double[] s = sourceTriangleMatrix[i]; final double[] t = matrix[i]; for (int j = 0; j <= i; j++) { t[j] = s[j]; } for (int j = i + 1; j < length; j++) { t[j] = sourceTriangleMatrix[j][i]; } } final RealMatrix coefficients = new Array2DRowRealMatrix(matrix, false); try { final DecompositionSolver solver = new CholeskyDecomposition(coefficients).getSolver(); final RealVector constants = new ArrayRealVector(eq.getConstraints(), true); final RealVector solution = solver.solve(constants); return new DefaultSlopeCoefficients(solution.toArray()); } catch (final NonPositiveDefiniteMatrixException e) { throw new EstimationException("Matrix inversion error due to data is linearly dependent", e); } }
private void solveLinearEquationSystem(double targetDelay, int iterations) { driveStrengthMatrix_x = MatrixUtils.createRealMatrix(this.cellInstances.size(), 1); for (int i = 0; i < iterations; i++) { RealMatrix effortLoadMatrix = this.effortMatrix_T.multiply(driveStrengthMatrix_x); driveStrengthMatrix_x = effortLoadMatrix.add(this.staticLoadMatrix_b).scalarMultiply(1 / targetDelay); } }
@SuppressWarnings("unused") private void printX(RealMatrix x, int iteration) { for (int i = 0; i < x.getRowDimension(); i++) { double value = x.getEntry(i, 0); System.out.print(value); if ( i < x.getRowDimension() - 1 ){ System.out.print(','); } } System.out.print('\n'); }
@Test public void testMatrixEquality() { final RealMatrix m1 = MatrixUtils .createRealMatrix(new double[][] { { 1, 2 }, { 1, 2 } }); final RealMatrix m2 = m1.copy(); assertEquals(m1, m2); }
@Test public void testNullspace() { final double expected = 0; final RealMatrix nullspace = MatrixFunctions.nullspace(matrixA); final RealMatrix result = matrixA.multiply(nullspace); final double[][] resultData = result.getData(); for (final double[] row : resultData) { for (final double cell : row) { assertEquals(expected, cell, 1e-12); } } assertNull(MatrixFunctions.nullspace(matrixI)); }
@Test public void testMultiplyElementWise() { final RealMatrix expected = MatrixUtils.createRealMatrix( new double[][] { { 1, 0, 0 }, { 0, 2, 0 }, { 0, 0, 3 } }); final RealMatrix result = MatrixFunctions.multiplyElementWise(matrixI, matrixA); assertEquals(expected, result); }
@Test public void testPow() { final RealMatrix result = MatrixFunctions.pow(matrixA, 2); for (int r = 0; r < result.getRowDimension(); r++) { for (int c = 0; c < result.getColumnDimension(); c++) { assertEquals(Math.pow(matrixA.getEntry(r, c), 2), result.getEntry(r, c), 0); } } }
/** * @param A a square matrix. * @return the inverse of A or null if A is non-square or singular. */ public static double[][] inverse(final double[][] A) { RealMatrix M = MatrixUtils.createRealMatrix(A); if (!M.isSquare()) return null; else { double[][] Ai = null; try { RealMatrix Mi = MatrixUtils.inverse(M); //new LUDecomposition(M).getSolver().getInverse(); Ai = Mi.getData(); } catch (SingularMatrixException e) {} return Ai; } }
private RealMatrix makeDataMatrix(List<double[]> X) { final int m = X.size(); final int n = X.get(0).length; RealMatrix M = MatrixUtils.createRealMatrix(n, m); int i = 0; for (double[] x : X) { RealVector xi = MatrixUtils.createRealVector(x); M.setColumnVector(i, xi); i++; } return M; }
@Test() public void testRealMatrixRead() { final DataFrame<String,String> frame = TestDataFrames.random(double.class, 100, 100); final RealMatrix matrix = frame.export().asApacheMatrix(); Assert.assertEquals(frame.rowCount(), matrix.getRowDimension(), "Row count matches"); Assert.assertEquals(frame.colCount(), matrix.getColumnDimension(), "Column count matches"); for (int i=0; i<frame.rowCount(); ++i) { for (int j = 0; j<frame.colCount(); ++j) { final double v1 = frame.data().getDouble(i, j); final double v2 = matrix.getEntry(i, j); Assert.assertEquals(v1, v2, "Values match at " + i + "," + j); } } }
public ThreadController(final ExecutionContext exec, final RealMatrix globalKernelMatrix, final RealMatrix trainingKernelMatrix, final String[] labels, final int numNeighbors, final boolean normalize) { m_exec = exec; m_globalKernelMatrix = globalKernelMatrix; m_trainingKernelMatrix = trainingKernelMatrix; m_labels = labels; m_numNeighbors = numNeighbors; m_normalize = normalize; m_noveltyScores = new double[globalKernelMatrix.getColumnDimension()]; }
public NoveltyScoreCalculationCallable(final int index, final Semaphore semaphore, final int numNeighbors, final RealMatrix trainingKernelMatrix, final RealMatrix globalKernelMatrix, final String[] labels, final boolean normalize) { m_index = index; m_semaphore = semaphore; m_numNeighbors = numNeighbors; m_trainingKernelMatrix = trainingKernelMatrix; m_globalKernelMatrix = globalKernelMatrix; m_labels = labels; m_normalize = normalize; }
public LocalNoveltyScorer(final ExecutionMonitor executionMonitor, final RealMatrix m_globalKernelMatrix, final RealMatrix m_trainingKernelMatrix, final String[] m_labels, final int m_numNeighbors, final boolean m_normalize) { super(); m_exec = executionMonitor; this.m_globalKernelMatrix = m_globalKernelMatrix; this.m_trainingKernelMatrix = m_trainingKernelMatrix; this.m_labels = m_labels; this.m_numNeighbors = m_numNeighbors; this.m_normalize = m_normalize; }
public static RealMatrixWrapper of(final RealMatrix delegate) { if (delegate instanceof Array2DRowRealMatrix) { return new Array2DRowWrapper((Array2DRowRealMatrix) delegate); } else if (delegate instanceof DiagonalMatrix) { return new DiagonalWrapper((DiagonalMatrix) delegate); } else { return new DefaultWrapper(delegate); } }
@Override public void fit(List<double[]> X, List<double[]> Y) { // fits n-dimensional data sets with affine model if (X.size() != Y.size()) throw new IllegalArgumentException("point sequences X, Y must have same length"); this.m = X.size(); this.n = X.get(0).length; RealMatrix M = MatrixUtils.createRealMatrix(2 * m, 2 * (n + 1)); RealVector b = new ArrayRealVector(2 * m); // mount matrix M: int row = 0; for (double[] x : X) { for (int j = 0; j < n; j++) { M.setEntry(row, j, x[j]); M.setEntry(row, n, 1); row++; } for (int j = 0; j < n; j++) { M.setEntry(row, j + n + 1, x[j]); M.setEntry(row, 2 * n + 1, 1); row++; } } // mount vector b row = 0; for (double[] y : Y) { for (int j = 0; j < n; j++) { b.setEntry(row, y[j]); row++; } } SingularValueDecomposition svd = new SingularValueDecomposition(M); DecompositionSolver solver = svd.getSolver(); RealVector a = solver.solve(b); A = makeTransformationMatrix(a); }
private RealMatrix squared_euclidean_distances(final RealMatrix x, final RealMatrix y) { final RealMatrix distmat = MatrixUtils .createRealMatrix(x.getRowDimension(), y.getRowDimension()); for (int i = 0; i < x.getRowDimension(); i++) { for (int j = 0; j < y.getRowDimension(); j++) { final RealVector buff = x.getRowVector(i).subtract(y.getRowVector(j)); distmat.setEntry(i, j, buff.dotProduct(buff)); } } return distmat; }
public OneClassKNFST(final RealMatrix kernelMatrix) throws KNFSTException { final int n = kernelMatrix.getRowDimension(); // include dot products of training samples and the origin in feature // space (these dot products are always zero!) final RealMatrix k = MatrixFunctions.concatVertically( MatrixFunctions.concatHorizontally(kernelMatrix, MatrixUtils.createRealMatrix( kernelMatrix.getRowDimension(), 1)), MatrixUtils.createRealMatrix(1, kernelMatrix.getColumnDimension() + 1)); // create one-class labels + a different label for the origin final String[] labels = new String[n + 1]; for (int l = 0; l <= n; l++) { labels[l] = (l == n) ? "0" : "1"; } // get model parameters final RealMatrix projection = projection(k, labels); final int[] indices = new int[n]; for (int i = 0; i < n; i++) { indices[i] = i; } m_targetPoints = MatrixUtils .createRowRealMatrix( MatrixFunctions .columnMeans(k .getSubMatrix(0, n - 1, 0, k.getColumnDimension() - 1) .multiply(projection)).toArray()); m_projection = projection.getSubMatrix(0, n - 1, 0, projection.getColumnDimension() - 1); m_betweenClassDistances = new double[] { Math.abs(m_targetPoints.getEntry(0, 0)) }; }
public static RealMatrix sqrt(final RealMatrix matrix) { final double[][] data = matrix.getData(); for (int r = 0; r < matrix.getRowDimension(); r++) { for (int c = 0; c < matrix.getColumnDimension(); c++) { data[r][c] = Math.sqrt(data[r][c]); } } return MatrixUtils.createRealMatrix(data); }
public static RealMatrix pow(final RealMatrix matrix, final double power) { final RealMatrix result = matrix.createMatrix(matrix.getRowDimension(), matrix.getColumnDimension()); for (int r = 0; r < result.getRowDimension(); r++) { for (int c = 0; c < result.getColumnDimension(); c++) { result.setEntry(r, c, Math.pow(matrix.getEntry(r, c), power)); } } return result; }
/** * Returns a newly created DataFrame from the Apache matrix * @param matrix the matrix input * @return the newly created DataFrame */ private DataFrame<Integer,Integer> toDataFrame(RealMatrix matrix) { final Range<Integer> rowKeys = Range.of(0, matrix.getRowDimension()); final Range<Integer> colKeys = Range.of(0, matrix.getColumnDimension()); return DataFrame.ofDoubles(rowKeys, colKeys, v -> { final int i = v.rowOrdinal(); final int j = v.colOrdinal(); return matrix.getEntry(i, j); }); }