List of usage examples for org.apache.commons.math3.linear BlockRealMatrix BlockRealMatrix
public BlockRealMatrix(final double[][] rawData) throws DimensionMismatchException, NotStrictlyPositiveException
From source file:bigdataproject.PCA.java
public double[][] reduceDimensions() { BlockRealMatrix matrix = new BlockRealMatrix(dataSet); Covariance cov = new Covariance(matrix, false); RealMatrix covarianceMatrix = cov.getCovarianceMatrix(); EigenDecomposition dec = new EigenDecomposition(covarianceMatrix); RealVector principalEigenVector = dec.getEigenvector(0); RealVector secondEigenVector = dec.getEigenvector(1); BlockRealMatrix pca = new BlockRealMatrix(principalEigenVector.getDimension(), 2); pca.setColumnVector(0, principalEigenVector); pca.setColumnVector(1, secondEigenVector); BlockRealMatrix pcaTranspose = pca.transpose(); BlockRealMatrix columnVectorMatrix = matrix.transpose(); BlockRealMatrix matrix2D = pcaTranspose.multiply(columnVectorMatrix); return matrix2D.getData(); }
From source file:bigdataproject.KDistances.java
public void calculateDistances() { for (int i = 0; i < samples.length; i++) { double[] point = samples[i]; for (int j = i + 1; j < samples.length; j++) { double[] point2 = samples[j]; EuclideanDistance dist = new EuclideanDistance(); distanceMatrix[i][j] = dist.compute(point, point2); }// w w w . j a v a 2s. c o m } BlockRealMatrix dist = new BlockRealMatrix(distanceMatrix); BlockRealMatrix distTranspose = dist.transpose(); dist = dist.add(distTranspose); distanceMatrix = dist.getData(); }
From source file:gamlss.utilities.MatrixFunctions.java
/** * if the length of the return matrix is zero, * then the diff was not completed properly. * @param data - matrix/* ww w.j av a 2 s .co m*/ * @return - matrix */ private static BlockRealMatrix getDiff(final BlockRealMatrix data) { double[][] newdata = new double[data.getRowDimension() - 1][data.getColumnDimension()]; for (int i = 0; i < newdata.length; i++) { for (int j = 0; j < newdata[i].length; j++) { newdata[i][j] = data.getEntry(i + 1, j) - data.getEntry(i, j); } } return new BlockRealMatrix(newdata); }
From source file:edu.macalester.tagrelatedness.KendallsCorrelation.java
/** * 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./*from ww w . j ava 2 s.c o m*/ * * @param matrix matrix with columns representing variables to correlate * @return correlation matrix */ static public RealMatrix computeCorrelationMatrix(final double[][] matrix) { return computeCorrelationMatrix(new BlockRealMatrix(matrix)); }
From source file:edu.cmu.tetrad.search.EstimateRank.java
public double[] CanCor(double[][] A, double[][] B) { this.A = A;//from w w w . j av a 2 s . c o m this.B = B; RealMatrix Ua = new SingularValueDecomposition(new BlockRealMatrix(A)).getU(); RealMatrix UTa = Ua.transpose(); RealMatrix Ub = new SingularValueDecomposition(new BlockRealMatrix(B)).getU(); return new SingularValueDecomposition(UTa.multiply(Ub)).getSingularValues(); }
From source file:com.itemanalysis.psychometrics.mixture.MvNormalComponentDistributionTest.java
/** * This test uses the iris data from R. It is a three group mixture model. * Results confirmed with the following R code: * * library(mixtools)/*from w w w. java2 s. c om*/ * mvnormalmixEM(iris[,-5], arbvar=TRUE, k=3) * */ // @Test public void mixModel1Test() { System.out.println("Mixture model test with Iris Data"); MixtureModelTestData testData = new MixtureModelTestData(); BlockRealMatrix allData = new BlockRealMatrix(testData.getMixtureData3()); BlockRealMatrix group1Data = allData.getSubMatrix(0, 49, 0, 3); BlockRealMatrix group2Data = allData.getSubMatrix(50, 99, 0, 3); BlockRealMatrix group3Data = allData.getSubMatrix(100, 149, 0, 3); BlockRealMatrix data = allData.getSubMatrix(0, 149, 0, 3); // System.out.println(Arrays.toString(allData.getRow(0))); VectorialMean trueMean1 = new VectorialMean(4); VectorialMean trueMean2 = new VectorialMean(4); VectorialMean trueMean3 = new VectorialMean(4); Covariance trueCov1 = new Covariance(group1Data); Covariance trueCov2 = new Covariance(group2Data); Covariance trueCov3 = new Covariance(group3Data); for (int i = 0; i < group1Data.getRowDimension(); i++) { trueMean1.increment(group1Data.getRow(i)); } for (int i = 0; i < group2Data.getRowDimension(); i++) { trueMean2.increment(group2Data.getRow(i)); } for (int i = 0; i < group3Data.getRowDimension(); i++) { trueMean3.increment(group3Data.getRow(i)); } double[] trueMeanGroup1 = trueMean1.getResult(); double[] trueMeanGroup2 = trueMean2.getResult(); double[] trueMeanGroup3 = trueMean3.getResult(); int startGroup = 3; int endGroup = 3; int maxIter = 1000; double converge = 1e-8; int starts = 100; MvNormalMixtureModel model = null; model = new MvNormalMixtureModel(data, endGroup); model.setModelConstraints(false, false, false, false); model.setEmOptions(maxIter, converge, starts); model.call(); System.out.println(" Testing convergence..."); assertTrue("Convergence test", model.converged()); double[] pi = new double[3]; pi[0] = model.getMixingProportion(0); pi[1] = model.getMixingProportion(1); pi[2] = model.getMixingProportion(2); //check mixing proportions // System.out.println(" Testing proportions..."); // assertEquals("Proportion 1 test: ", 0.40, pi[index[0]], 5e-2); // assertEquals("Proportion 2 test: ", 0.60, pi[index[1]], 5e-2); double[] estMean1 = model.getMean(0); double[] estMean2 = model.getMean(1); double[] estMean3 = model.getMean(2); double[][] estCov1 = model.getCov(0); double[][] estCov2 = model.getCov(1); double[][] estCov3 = model.getCov(2); double[][] trueCov1Array = trueCov1.getCovarianceMatrix().getData(); double[][] trueCov2Array = trueCov2.getCovarianceMatrix().getData(); double[][] trueCov3Array = trueCov3.getCovarianceMatrix().getData(); System.out.println(model.printResults()); /** * Test means */ // System.out.println(" Testing means..."); // for(int i=0;i<3;i++){ // assertEquals("Group 1 mean test", trueMeanGroup1[i], estMean1[i], 5e-2); // assertEquals("Group 2 mean test", trueMeanGroup2[i], estMean2[i], 5e-2); // assertEquals("Group 3 mean test", trueMeanGroup3[i], estMean3[i], 5e-2); // } // // /** // * Test Covariance matrix // */ // System.out.println(" Testing covariances..."); // for(int i=0;i<3;i++){ // for(int j=0;j<3;j++){ // assertEquals("Cov[" +i + "," + j+"] test:", trueCov1Array[i][j], estCov1[i][j], 5e-2); // } // } // // for(int i=0;i<3;i++){ // for(int j=0;j<3;j++){ // assertEquals("Cov[" +i + "," + j+"] test:", trueCov2Array[i][j], estCov2[i][j], 5e-2); // } // } // // for(int i=0;i<3;i++){ // for(int j=0;j<3;j++){ // assertEquals("Cov[" +i + "," + j+"] test:", trueCov2Array[i][j], estCov2[i][j], 5e-2); // } // } }
From source file:gamlss.utilities.MatrixFunctions.java
/** * Calculate this: (x - t) ^ p * (x > t). * @param v1 vector 1/*from ww w . j a v a 2 s .co m*/ * @param v2 vector 2 * @param p - power * @return matrix C with Cij = v1i * v2j. */ //tpower <- function(x, t, p) public static BlockRealMatrix outertpowerPB(final ArrayRealVector v1, final ArrayRealVector v2, final int p) { double[][] newdata = new double[v1.getDimension()][v2.getDimension()]; for (int j = 0; j < v1.getDimension(); j++) { for (int j2 = 0; j2 < v2.getDimension(); j2++) { if (v1.getEntry(j) > v2.getEntry(j2)) { newdata[j][j2] = FastMath.pow((v1.getEntry(j) - v2.getEntry(j2)), p); } else { newdata[j][j2] = 0d; } } } return new BlockRealMatrix(newdata); }
From source file:edu.cmu.tetrad.util.ApacheTetradMatrix.java
private ApacheTetradMatrix(double[][] apacheData) { if (apacheData.length == 0) { this.apacheData = new Array2DRowRealMatrix(); } else {/*from w w w . j ava 2 s .com*/ this.apacheData = new BlockRealMatrix(apacheData); } }
From source file:edu.cmu.tetrad.search.EstimateRank.java
public double[] CanCor(int[] iA, int[] iB, double[][] cov) { this.iA = iA; this.iB = iB; this.cov = cov; RealMatrix covA = new BlockRealMatrix(cov).getSubMatrix(iA, iA); RealMatrix covB = new BlockRealMatrix(cov).getSubMatrix(iB, iB); RealMatrix covAB = new BlockRealMatrix(cov).getSubMatrix(iA, iB); RealMatrix covBA = new BlockRealMatrix(cov).getSubMatrix(iB, iA); RealMatrix S = getInverse(covA).multiply(covAB).multiply(getInverse(covB)).multiply(covBA); double[] rtCors = new EigenDecomposition(S).getRealEigenvalues(); Arrays.sort(rtCors);// w ww . j a v a 2 s . c om double[] Cors = new double[rtCors.length]; for (int i = rtCors.length; i > 0; i--) { Cors[rtCors.length - i] = Math.pow(rtCors[i - 1], .5); } return Cors; }
From source file:gamlss.utilities.MatrixFunctions.java
/** * Build identity matrix.//from w ww .j ava 2 s .c o m * @param size - size of the matrix * @return identity matrix */ public static BlockRealMatrix buildIdentityMatrix(final int size) { double[] tempArr = new double[size]; for (int i = 0; i < size; i++) { tempArr[i] = 1; } return new BlockRealMatrix(MatrixUtils.createRealDiagonalMatrix(tempArr).getData()); }