List of usage examples for org.apache.commons.math3.linear RealMatrix getColumnDimension
int getColumnDimension();
From source file:eagle.security.userprofile.model.kde.UserProfileKDEModeler.java
private void computeStats(RealMatrix m) { if (m.getColumnDimension() != this.cmdTypes.length) { LOG.error("Please fix the commands list in config file"); }// ww w .ja v a2 s . c om statistics = new UserCommandStatistics[m.getColumnDimension()]; for (int i = 0; i < m.getColumnDimension(); i++) { UserCommandStatistics stats = new UserCommandStatistics(); stats.setCommandName(this.cmdTypes[i]); RealVector colData = m.getColumnVector(i); StandardDeviation deviation = new StandardDeviation(); double stddev = deviation.evaluate(colData.toArray()); if (LOG.isDebugEnabled()) LOG.debug("Stddev is NAN ? " + (Double.isNaN(stddev) ? "yes" : "no")); if (stddev <= lowVarianceVal) stats.setLowVariant(true); else stats.setLowVariant(false); stats.setStddev(stddev); Mean mean = new Mean(); double mu = mean.evaluate(colData.toArray()); if (LOG.isDebugEnabled()) LOG.debug("mu is NAN ? " + (Double.isNaN(mu) ? "yes" : "no")); stats.setMean(mu); statistics[i] = stats; } }
From source file:com.itemanalysis.psychometrics.factoranalysis.PrincipalComponentsMethod.java
public PrincipalComponentsMethod(RealMatrix R, int nFactors, RotationMethod rotationMethod) { this.R = R;/*from w w w.j a v a2 s .c om*/ this.nVariables = R.getColumnDimension(); this.nFactors = nFactors; this.rotationMethod = rotationMethod; }
From source file:com.itemanalysis.psychometrics.cfa.ConfirmatoryFactorAnalysis.java
/** * Computes the first centroid factor loadings to be used * as initial values for the confirmatory factor anlaysis. * * This only works for a one-factor model. Need to * generalize to multiple factors.//from w w w . j a v a2 s. c o m * * @param varcov observed covariance matrix * @return */ public double[] computeInitialFactorLoadings(RealMatrix varcov) { int r = varcov.getRowDimension(); int c = varcov.getColumnDimension(); double totalVariance = 0.0; double[] rowSum = new double[c]; double[] inits = new double[c]; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { rowSum[i] += varcov.getEntry(i, j); totalVariance += varcov.getEntry(i, j); } } double denom = Math.sqrt(totalVariance); for (int i = 0; i < r; i++) { inits[i] = rowSum[i] / denom; } return inits; }
From source file:edu.cudenver.bios.matrix.test.TestMatrixUtils.java
/** * Determines if dimensions and all cells are equal between the * two matrices//from w ww . j av a2s . c om * * @param A matrix A * @param B matrix B * @return true if equal, false if not equal */ private boolean compareMatrices(RealMatrix A, RealMatrix B) { if (A.getRowDimension() == B.getRowDimension() && A.getColumnDimension() == B.getColumnDimension()) { for (int r = 0; r < A.getRowDimension(); r++) { for (int c = 0; c < A.getColumnDimension(); c++) { if (Double.compare(A.getEntry(r, c), B.getEntry(r, c)) != 0) { System.err.println("Value mismatch at row=" + r + ", column=" + c); return false; } } } } else { System.err.println("Dimensions do not match: A (" + A.getRowDimension() + " x " + A.getColumnDimension() + "), B (" + B.getRowDimension() + " x " + B.getColumnDimension() + ")"); return false; } return true; }
From source file:com.clust4j.algo.preprocess.WeightTransformer.java
/** * Inverse transform the incoming data. If the corresponding weight is 0.0, * will coerce the column to positive infinity rather than NaN. *//* w ww . ja v a 2s .com*/ @Override public RealMatrix inverseTransform(RealMatrix data) { checkFit(); final int m = data.getRowDimension(); if (data.getColumnDimension() != n) throw new DimensionMismatchException(n, data.getColumnDimension()); double[][] X = data.getData(); double weight, val; for (int j = 0; j < n; j++) { weight = weights[j]; for (int i = 0; i < m; i++) { // sometimes, weight can be 0.0 if the user is masochistic... val = X[i][j] / weight; X[i][j] = Double.isNaN(val) ? Inf : val; } } // assign -- already copied in getData() return new Array2DRowRealMatrix(X, false); }
From source file:de.andreasschoknecht.LS3.LSSMCalculator.java
/** * Calculate the LSSM matrix containing the final similarity values between the documents, i.e., the models. * For calculating the similarity values the Vtk matrix is scaled with the singular value matrix. Afterwards, the cosine similarity * transformed on the interval [0,1] is calculated, which represents the similarity value of two documents. *///w ww. jav a 2 s. c o m void calculateLSSMMatrix() { // scale Vtk with singular value matrix Sk RealMatrix scaledVtk = Sk.multiply(Vtk); int docsNumber = scaledVtk.getColumnDimension(); double[][] tmpArray = new double[docsNumber][docsNumber]; for (int i = 0; i < docsNumber; i++) { RealVector documentVector1 = scaledVtk.getColumnVector(i); for (int j = i; j < docsNumber; j++) { double lssmValue = (documentVector1.cosine(scaledVtk.getColumnVector(j)) + 1) / 2; tmpArray[i][j] = round(lssmValue, 2); tmpArray[j][i] = round(lssmValue, 2); } } lssmMatrix = new Array2DRowRealMatrix(tmpArray); }
From source file:com.itemanalysis.psychometrics.cfa.ConfirmatoryFactorAnalysis.java
public ConfirmatoryFactorAnalysis(RealMatrix varcov, double numberOfExaminees, int modelType, int estimationMethod) { int nItems = varcov.getColumnDimension(); setModel(nItems, modelType, computeInitialFactorLoadings(varcov)); setEstimator(model, varcov, numberOfExaminees, estimationMethod); }
From source file:com.clust4j.algo.preprocess.YeoJohnsonTransformer.java
/** * Inverse transform your matrix./* w w w.j ava 2s . c o m*/ */ @Override public RealMatrix inverseTransform(RealMatrix X) { checkFit(); final int m = X.getRowDimension(); final int n = X.getColumnDimension(); if (n != shift.length) throw new DimensionMismatchException(n, shift.length); double[][] x = X.getData(); for (int j = 0; j < n; j++) { for (int i = 0; i < m; i++) { x[i][j] = yjInvTransSingle(x[i][j], this.lambdas[j]); } } // Implicit copy in the getData() return new Array2DRowRealMatrix(x, false); }
From source file:com.itemanalysis.psychometrics.factoranalysis.VarimaxCriteria.java
/** * Computes the function value for varimax rotation. * * @param L matrix of factor loadings./* w w w . j a v a 2 s . c o m*/ */ public void computeValues(RealMatrix L) { //initialize dimensions and column mean array int nrow = L.getRowDimension(); int ncol = L.getColumnDimension(); Mean[] colMean = new Mean[ncol]; for (int i = 0; i < ncol; i++) { colMean[i] = new Mean(); } //square each element in matrix RealMatrix L2 = L.copy(); double value = 0.0; for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { value = L.getEntry(i, j); value *= value; L2.setEntry(i, j, value); colMean[j].increment(value); } } double dif = 0.0; RealMatrix QL = new Array2DRowRealMatrix(nrow, ncol); for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { dif = L2.getEntry(i, j) - colMean[j].getResult(); QL.setEntry(i, j, dif); } } //compute gradientAt gradient = new Array2DRowRealMatrix(nrow, ncol); for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { value = -L.getEntry(i, j) * QL.getEntry(i, j); gradient.setEntry(i, j, value); } } //compute function value RealMatrix B = QL.transpose().multiply(QL); double sum = B.getTrace(); functionValue = -sum / 4.0; }
From source file:com.itemanalysis.psychometrics.cfa.ConfirmatoryFactorAnalysis.java
public ConfirmatoryFactorAnalysis(RealMatrix varcov, double[] inits, double numberOfExaminees, int modelType, int estimationMethod) { int nItems = varcov.getColumnDimension(); setModel(nItems, modelType, inits);// w w w .j a v a2 s . c o m setEstimator(model, varcov, numberOfExaminees, estimationMethod); }