List of usage examples for org.apache.commons.math3.linear EigenDecomposition getRealEigenvalue
public double getRealEigenvalue(final int i)
From source file:com.simiacryptus.mindseye.test.PCAUtil.java
/** * Pca features inv tensor [ ]./*from ww w. j a v a 2 s . co m*/ * * @param covariance the covariance * @param components the components * @param featureDimensions the feature dimensions * @param power the power * @return the tensor [ ] */ public static Tensor[] pcaFeatures(final RealMatrix covariance, final int components, final int[] featureDimensions, final double power) { @Nonnull final EigenDecomposition decomposition = new EigenDecomposition(covariance); final int[] orderedVectors = IntStream.range(0, components).mapToObj(x -> x) .sorted(Comparator.comparing(x -> -decomposition.getRealEigenvalue(x))).mapToInt(x -> x).toArray(); return IntStream.range(0, orderedVectors.length).mapToObj(i -> { @Nonnull final Tensor src = new Tensor(decomposition.getEigenvector(orderedVectors[i]).toArray(), featureDimensions).copy(); return src.scale(1.0 / src.rms()).scale((Math.pow(decomposition.getRealEigenvalue(orderedVectors[i]) / decomposition.getRealEigenvalue(orderedVectors[0]), power))); }).toArray(i -> new Tensor[i]); }
From source file:de.biomedical_imaging.traJ.features.Asymmetry2Feature.java
/** * @return Returns an double array with the following elements [0]=asymmetry */// ww w . j a v a 2 s.c om @Override public double[] evaluate() { Array2DRowRealMatrix gyr = RadiusGyrationTensor2D.getRadiusOfGyrationTensor(t); EigenDecomposition eigdec = new EigenDecomposition(gyr); double e1 = eigdec.getRealEigenvalue(0); double e2 = eigdec.getRealEigenvalue(1); double asym = e2 / e1; //-1*Math.log(1-Math.pow(e1-e2, 2)/(2*Math.pow(e1+e2, 2))); result = new double[] { asym }; return result; }
From source file:de.biomedical_imaging.traJ.features.Asymmetry3Feature.java
/** * @return Returns an double array with the following elements [0]=Asymmetry *//* w w w . j a v a 2 s . c o m*/ @Override public double[] evaluate() { Array2DRowRealMatrix gyr = RadiusGyrationTensor2D.getRadiusOfGyrationTensor(t); EigenDecomposition eigdec = new EigenDecomposition(gyr); double e1 = eigdec.getRealEigenvalue(0); double e2 = eigdec.getRealEigenvalue(1); double asym = -1 * Math.log(1 - Math.pow(e1 - e2, 2) / (2 * Math.pow(e1 + e2, 2))); result = new double[] { asym }; return result; }
From source file:de.biomedical_imaging.traJ.features.AsymmetryFeature.java
/** * @return Returns an double array with the following elements [0]=Asymmetry *//*from ww w . ja v a 2 s. c o m*/ @Override public double[] evaluate() { Array2DRowRealMatrix gyr = RadiusGyrationTensor2D.getRadiusOfGyrationTensor(t); EigenDecomposition eigdec = new EigenDecomposition(gyr); double e1 = eigdec.getRealEigenvalue(0); double e2 = eigdec.getRealEigenvalue(1); double asym = Math.pow(e1 * e1 - e2 * e2, 2) / (Math.pow(e1 * e1 + e2 * e2, 2)); //-1*Math.log(1-Math.pow(e1-e2, 2)/(2*Math.pow(e1+e2, 2))); result = new double[] { asym }; return result; }
From source file:edu.oregonstate.eecs.mcplan.ml.KernelPrincipalComponentsAnalysis.java
/** * TODO: Things to consider:// w w w .j a va 2 s . c o m * * Nystrom approximation to kernel matrix * * Iterative eigenvalue algorithm * * Online version of KPCA * @param data Training data * @param Nbases Number of eigenvectors to retain * @param k Kernel function * @param jitter We regularize by solving ((1 - jitter)*K + jitter*I). */ public KernelPrincipalComponentsAnalysis(final ArrayList<T> data, final KernelFunction<T> k, final double jitter) { this.data = data; this.k = k; this.Ndata = data.size(); // Compute kernel matrix System.out.println("[KPCA] Computing kernel matrix"); final RealMatrix K = new Array2DRowRealMatrix(Ndata, Ndata); for (int i = 0; i < Ndata; ++i) { final T xi = data.get(i); for (int j = i; j < Ndata; ++j) { final T xj = data.get(j); final double K_ij = (1.0 - jitter) * k.apply(xi, xj); final double jitter_if_diag = (i == j ? jitter : 0.0); K.setEntry(i, j, K_ij + jitter_if_diag); K.setEntry(j, i, K_ij + jitter_if_diag); } } // System.out.println( K ); System.out.println("[KPCA] Centering"); // Averages for centering row_avg = new double[Ndata]; final MeanVarianceAccumulator total_mv = new MeanVarianceAccumulator(); for (int i = 0; i < Ndata; ++i) { final MeanVarianceAccumulator row_mv = new MeanVarianceAccumulator(); for (int j = 0; j < Ndata; ++j) { final double K_ij = K.getEntry(i, j); row_mv.add(K_ij); total_mv.add(K_ij); } row_avg[i] = row_mv.mean(); } total_avg = total_mv.mean(); // Centered version of the kernel matrix: // K_c(i, j) = K_ij - sum_z K_zj / m - sum_z K_iz / m + sum_{z,y} K_zy / m^2 for (int i = 0; i < Ndata; ++i) { for (int j = 0; j < Ndata; ++j) { final double K_ij = K.getEntry(i, j); K.setEntry(i, j, K_ij - row_avg[i] - row_avg[j] + total_avg); } } System.out.println("[KPCA] Eigendecomposition"); eigenvectors = new ArrayList<RealVector>(); final EigenDecomposition evd = new EigenDecomposition(K); for (int j = 0; j < Ndata; ++j) { final double eigenvalue = evd.getRealEigenvalue(j); if (eigenvalue < eps) { break; } eigenvalues.add(eigenvalue); final double scale = 1.0 / Math.sqrt(eigenvalue); final RealVector eigenvector = evd.getEigenvector(j); eigenvectors.add(eigenvector.mapMultiply(scale)); } }
From source file:com.itemanalysis.psychometrics.factoranalysis.PrincipalComponentsMethod.java
public double estimateParameters() { EigenDecomposition eigen = new EigenDecomposition(R); RealMatrix eigenVectors = eigen.getV().getSubMatrix(0, nVariables - 1, 0, nFactors - 1); double[] ev = new double[nFactors]; for (int i = 0; i < nFactors; i++) { ev[i] = Math.sqrt(eigen.getRealEigenvalue(i)); }//www .java 2s.c om DiagonalMatrix evMatrix = new DiagonalMatrix(ev);//USE Apache version of Diagonal matrix when upgrade to version 3.2 RealMatrix LOAD = eigenVectors.multiply(evMatrix); //rotate factor loadings if (rotationMethod != RotationMethod.NONE) { GPArotation gpa = new GPArotation(); RotationResults results = gpa.rotate(LOAD, rotationMethod); LOAD = results.getFactorLoadings(); } Sum[] colSums = new Sum[nFactors]; Sum[] colSumsSquares = new Sum[nFactors]; for (int j = 0; j < nFactors; j++) { colSums[j] = new Sum(); colSumsSquares[j] = new Sum(); } factorLoading = new double[nVariables][nFactors]; communality = new double[nVariables]; uniqueness = new double[nVariables]; for (int i = 0; i < nVariables; i++) { for (int j = 0; j < nFactors; j++) { factorLoading[i][j] = LOAD.getEntry(i, j); colSums[j].increment(factorLoading[i][j]); colSumsSquares[j].increment(Math.pow(factorLoading[i][j], 2)); communality[i] += Math.pow(factorLoading[i][j], 2); } } //check sign of factor double sign = 1.0; for (int i = 0; i < nVariables; i++) { for (int j = 0; j < nFactors; j++) { if (colSums[j].getResult() < 0) { sign = -1.0; } else { sign = 1.0; } factorLoading[i][j] = factorLoading[i][j] * sign; } uniqueness[i] = 1.0 - communality[i]; } double totSumOfSquares = 0.0; sumsOfSquares = new double[nFactors]; proportionOfExplainedVariance = new double[nFactors]; proportionOfVariance = new double[nFactors]; for (int j = 0; j < nFactors; j++) { sumsOfSquares[j] = colSumsSquares[j].getResult(); totSumOfSquares += sumsOfSquares[j]; } for (int j = 0; j < nFactors; j++) { proportionOfExplainedVariance[j] = sumsOfSquares[j] / totSumOfSquares; proportionOfVariance[j] = sumsOfSquares[j] / nVariables; } return 0.0; }
From source file:com.itemanalysis.psychometrics.factoranalysis.FactorAnalysisTest.java
@Test public void eigenValues() { RealMatrix R = new Array2DRowRealMatrix(readHarman74Data()); int nFactors = 2; int nVariables = 24; double[] x = new double[nVariables]; for (int i = 0; i < nVariables; i++) { x[i] = .5;/*w ww . j av a 2 s .c o m*/ } for (int i = 0; i < nVariables; i++) { R.setEntry(i, i, 1.0 - x[i]); } EigenDecomposition eigen = new EigenDecomposition(R); RealMatrix eigenVectors = eigen.getV().getSubMatrix(0, nVariables - 1, 0, nFactors - 1); double[] ev = new double[nFactors]; for (int i = 0; i < nFactors; i++) { ev[i] = Math.sqrt(eigen.getRealEigenvalue(i)); } DiagonalMatrix evMatrix = new DiagonalMatrix(ev);// USE Apache version // of Diagonal // matrix when // upgrade to // version 3.2 RealMatrix LAMBDA = eigenVectors.multiply(evMatrix); RealMatrix SIGMA = (LAMBDA.multiply(LAMBDA.transpose())); RealMatrix RESID = R.subtract(SIGMA); double sum = 0.0; double squared = 0.0; for (int i = 0; i < RESID.getRowDimension(); i++) { for (int j = 0; j < RESID.getColumnDimension(); j++) { if (i != j) { sum += Math.pow(RESID.getEntry(i, j), 2); } } } System.out.println(sum); // RealMatrix SIGMA = (LAMBDA.multiply(LAMBDA.transpose())); // // System.out.println("SIGMA"); // for(int i=0;i<SIGMA.getRowDimension();i++){ // for(int j=0;j<SIGMA.getColumnDimension();j++){ // System.out.print(SIGMA.getEntry(i, j) + " "); // } // System.out.println(); // } }
From source file:edu.oregonstate.eecs.mcplan.ml.LinearDiscriminantAnalysis.java
/** * @param data The elements of 'data' will be modified. * @param label//from w w w . j a v a 2 s . co m * @param Nclasses * @param shrinkage_intensity */ public LinearDiscriminantAnalysis(final ArrayList<double[]> data, final int[] label, final int Nclasses, final double shrinkage) { assert (data.size() == label.length); final int Ndata = data.size(); final int Ndim = data.get(0).length; // Partition data by class final ArrayList<ArrayList<double[]>> classes = new ArrayList<ArrayList<double[]>>(Nclasses); for (int i = 0; i < Nclasses; ++i) { classes.add(new ArrayList<double[]>()); } for (int i = 0; i < data.size(); ++i) { classes.get(label[i]).add(data.get(i)); } // Mean center the data final VectorMeanVarianceAccumulator mv = new VectorMeanVarianceAccumulator(Ndim); for (int i = 0; i < Ndata; ++i) { mv.add(data.get(i)); } mean = mv.mean(); // Subtract global mean for (final double[] x : data) { Fn.vminus_inplace(x, mean); } // Calculate class means and covariances final double[][] class_mean = new double[Nclasses][Ndim]; final RealMatrix[] class_cov = new RealMatrix[Nclasses]; for (int i = 0; i < Nclasses; ++i) { final ArrayList<double[]> Xc = classes.get(i); final VectorMeanVarianceAccumulator mv_i = new VectorMeanVarianceAccumulator(Ndim); final StorelessCovariance cov = new StorelessCovariance(Ndim); for (int j = 0; j < Xc.size(); ++j) { final double[] x = Xc.get(j); mv_i.add(x); cov.increment(x); } class_mean[i] = mv_i.mean(); class_cov[i] = cov.getCovarianceMatrix(); } // Between-class scatter. // Note that 'data' is mean-centered, so the global mean is 0. RealMatrix Sb_builder = new Array2DRowRealMatrix(Ndim, Ndim); for (int i = 0; i < Nclasses; ++i) { final RealVector mu_i = new ArrayRealVector(class_mean[i]); final RealMatrix xxt = mu_i.outerProduct(mu_i); Sb_builder = Sb_builder.add(xxt.scalarMultiply(classes.get(i).size() / ((double) Ndata - 1))); } this.Sb = Sb_builder; Sb_builder = null; // Within-class scatter with shrinkage estimate: // Sw = (1.0 - shrinkage) * \sum Sigma_i + shrinkage * I RealMatrix Sw_builder = new Array2DRowRealMatrix(Ndim, Ndim); for (int i = 0; i < Nclasses; ++i) { final RealMatrix Sigma_i = class_cov[i]; final RealMatrix scaled = Sigma_i.scalarMultiply((1.0 - shrinkage) * (classes.get(i).size() - 1)); Sw_builder = Sw_builder.add(scaled); } for (int i = 0; i < Ndim; ++i) { Sw_builder.setEntry(i, i, Sw_builder.getEntry(i, i) + shrinkage); } this.Sw = Sw_builder; Sw_builder = null; // Invert Sw System.out.println("[LDA] Sw inverse"); final RealMatrix Sw_inv = new LUDecomposition(Sw).getSolver().getInverse(); final RealMatrix F = Sw_inv.multiply(Sb); System.out.println("[LDA] Eigendecomposition"); eigenvalues = new double[Nclasses - 1]; eigenvectors = new ArrayList<RealVector>(Nclasses - 1); final EigenDecomposition evd = new EigenDecomposition(F); for (int j = 0; j < Nclasses - 1; ++j) { final double eigenvalue = evd.getRealEigenvalue(j); eigenvalues[j] = eigenvalue; // final double scale = 1.0 / Math.sqrt( eigenvalue ); // eigenvectors.add( evd.getEigenvector( j ).mapMultiply( scale ) ); eigenvectors.add(evd.getEigenvector(j)); } }
From source file:com.itemanalysis.psychometrics.factoranalysis.GeneralizedLeastSquaresMethod.java
private void computeFactorLoadings(double[] x) { uniqueness = x;/* w ww . j a va 2s .c om*/ communality = new double[nVariables]; for (int i = 0; i < nVariables; i++) { R.setEntry(i, i, 1.0 - x[i]); } EigenDecomposition E = new EigenDecomposition(R); RealMatrix L = E.getV().getSubMatrix(0, nVariables - 1, 0, nFactors - 1); double[] ev = new double[nFactors]; for (int i = 0; i < nFactors; i++) { ev[i] = Math.sqrt(E.getRealEigenvalue(i)); } DiagonalMatrix M = new DiagonalMatrix(ev); RealMatrix LOAD = L.multiply(M); //rotate factor loadings if (rotationMethod != RotationMethod.NONE) { GPArotation gpa = new GPArotation(); RotationResults results = gpa.rotate(LOAD, rotationMethod); LOAD = results.getFactorLoadings(); } Sum[] colSums = new Sum[nFactors]; Sum[] colSumsSquares = new Sum[nFactors]; for (int j = 0; j < nFactors; j++) { colSums[j] = new Sum(); colSumsSquares[j] = new Sum(); } factorLoading = new double[nVariables][nFactors]; for (int i = 0; i < nVariables; i++) { for (int j = 0; j < nFactors; j++) { factorLoading[i][j] = LOAD.getEntry(i, j); colSums[j].increment(factorLoading[i][j]); colSumsSquares[j].increment(Math.pow(factorLoading[i][j], 2)); communality[i] += Math.pow(factorLoading[i][j], 2); } } //check sign of factor double sign = 1.0; for (int i = 0; i < nVariables; i++) { for (int j = 0; j < nFactors; j++) { if (colSums[j].getResult() < 0) { sign = -1.0; } else { sign = 1.0; } factorLoading[i][j] = factorLoading[i][j] * sign; } } double totSumOfSquares = 0.0; sumsOfSquares = new double[nFactors]; proportionOfExplainedVariance = new double[nFactors]; proportionOfVariance = new double[nFactors]; for (int j = 0; j < nFactors; j++) { sumsOfSquares[j] = colSumsSquares[j].getResult(); totSumOfSquares += sumsOfSquares[j]; } for (int j = 0; j < nFactors; j++) { proportionOfExplainedVariance[j] = sumsOfSquares[j] / totSumOfSquares; proportionOfVariance[j] = sumsOfSquares[j] / nVariables; } }
From source file:com.itemanalysis.psychometrics.factoranalysis.MaximumLikelihoodMethod.java
private void computeFactorLoadings(double[] x) { uniqueness = x;//from w w w . j av a 2 s.c o m communality = new double[nVariables]; double[] sqrtPsi = new double[nVariables]; double[] invSqrtPsi = new double[nVariables]; for (int i = 0; i < nVariables; i++) { sqrtPsi[i] = Math.sqrt(x[i]); invSqrtPsi[i] = 1.0 / Math.sqrt(x[i]); } DiagonalMatrix diagPsi = new DiagonalMatrix(x); DiagonalMatrix diagSqtPsi = new DiagonalMatrix(sqrtPsi); DiagonalMatrix diagInvSqrtPsi = new DiagonalMatrix(invSqrtPsi); RealMatrix Sstar = diagInvSqrtPsi.multiply(R).multiply(diagInvSqrtPsi); EigenDecomposition E = new EigenDecomposition(Sstar); RealMatrix L = E.getV().getSubMatrix(0, nVariables - 1, 0, nFactors - 1); double[] ev = new double[nFactors]; for (int i = 0; i < nFactors; i++) { ev[i] = Math.sqrt(Math.max(E.getRealEigenvalue(i) - 1, 0)); } DiagonalMatrix M = new DiagonalMatrix(ev); RealMatrix LOAD2 = L.multiply(M); RealMatrix LOAD = diagSqtPsi.multiply(LOAD2); //rotate factor loadings if (rotationMethod != RotationMethod.NONE) { GPArotation gpa = new GPArotation(); RotationResults results = gpa.rotate(LOAD, rotationMethod); LOAD = results.getFactorLoadings(); } Sum[] colSums = new Sum[nFactors]; Sum[] colSumsSquares = new Sum[nFactors]; for (int j = 0; j < nFactors; j++) { colSums[j] = new Sum(); colSumsSquares[j] = new Sum(); } factorLoading = new double[nVariables][nFactors]; for (int i = 0; i < nVariables; i++) { for (int j = 0; j < nFactors; j++) { factorLoading[i][j] = LOAD.getEntry(i, j); colSums[j].increment(factorLoading[i][j]); colSumsSquares[j].increment(Math.pow(factorLoading[i][j], 2)); communality[i] += Math.pow(factorLoading[i][j], 2); } } //check sign of factor double sign = 1.0; for (int i = 0; i < nVariables; i++) { for (int j = 0; j < nFactors; j++) { if (colSums[j].getResult() < 0) { sign = -1.0; } else { sign = 1.0; } factorLoading[i][j] = factorLoading[i][j] * sign; } } double totSumOfSquares = 0.0; sumsOfSquares = new double[nFactors]; proportionOfExplainedVariance = new double[nFactors]; proportionOfVariance = new double[nFactors]; for (int j = 0; j < nFactors; j++) { sumsOfSquares[j] = colSumsSquares[j].getResult(); totSumOfSquares += sumsOfSquares[j]; } for (int j = 0; j < nFactors; j++) { proportionOfExplainedVariance[j] = sumsOfSquares[j] / totSumOfSquares; proportionOfVariance[j] = sumsOfSquares[j] / nVariables; } }