List of usage examples for org.apache.commons.math3.linear RealMatrix getRowDimension
int getRowDimension();
From source file:edu.cudenver.bios.matrix.test.TestOrthogonalPolynomials.java
/** * Write the matrix to std out// w w w. j a v a 2 s . c o m * @param m */ private void printMatrix(String title, RealMatrix m) { System.out.println(title); DecimalFormat Number = new DecimalFormat("#0.000"); for (int row = 0; row < m.getRowDimension(); row++) { for (int col = 0; col < m.getColumnDimension(); col++) { System.out.print(Number.format(m.getEntry(row, col)) + "\t"); } System.out.print("\n"); } }
From source file:edu.cudenver.bios.power.glmm.GLMMTestPillaiBartlett.java
/** * Compute a Pillai Bartlett Trace statistic * //w w w . j a v a 2 s . c o m * @param H hypothesis sum of squares matrix * @param E error sum of squares matrix * @returns F statistic */ private double getPillaiBartlettTrace(RealMatrix H, RealMatrix E) { if (!H.isSquare() || !E.isSquare() || H.getColumnDimension() != E.getRowDimension()) throw new IllegalArgumentException( "Failed to compute Pillai-Bartlett Trace: hypothesis and error matrices must be square and same dimensions"); double a = C.getRowDimension(); double b = U.getColumnDimension(); double s = (a < b) ? a : b; double p = beta.getColumnDimension(); RealMatrix adjustedH = H; if ((s == 1 && p > 1) || fMethod == FApproximation.PILLAI_ONE_MOMENT_OMEGA_MULT || fMethod == FApproximation.MULLER_TWO_MOMENT_OMEGA_MULT) { adjustedH = H.scalarMultiply(((double) (totalN - rank) / (double) totalN)); } RealMatrix T = adjustedH.add(E); RealMatrix inverseT = new LUDecomposition(T).getSolver().getInverse(); RealMatrix HinverseT = adjustedH.multiply(inverseT); return HinverseT.getTrace(); }
From source file:com.itemanalysis.psychometrics.cfa.AbstractConfirmatoryFactorAnalysisEstimator.java
public double sumMatrix(RealMatrix matrix) { double sum = 0.0; for (int i = 0; i < matrix.getRowDimension(); i++) { for (int j = 0; j < matrix.getColumnDimension(); j++) { sum += matrix.getEntry(i, j); }// w ww . j a v a 2 s . c om } return sum; }
From source file:jurls.core.utils.MatrixImage.java
public void draw(RealMatrix M, double minValue, double maxValue) { draw(M::getEntry, M.getColumnDimension(), M.getRowDimension(), minValue, maxValue); }
From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotationTest.java
@Test public void testM255Varimax() { System.out.println("Varimax rotation test: m255 data"); /**//w w w .java2s. c om * True result obtained form R using GPArotation package */ double[][] true_Varimax = { { 0.2806153, 0.1303408, 0.7332729 }, { 0.2620153, 0.1714751, 0.7434001 }, { 0.3129422, 0.2735077, 0.6396424 }, { 0.2277365, 0.2707234, 0.5740148 }, { 0.3650465, 0.4339803, 0.5203103 }, { 0.3193267, 0.7036671, 0.2683278 }, { 0.2056908, 0.7280409, 0.1543441 }, { 0.2398168, 0.4488439, 0.2356959 }, { 0.4424600, 0.4637861, 0.3346261 }, { 0.3748683, 0.4973271, 0.1762550 }, { 0.7710955, 0.2932897, 0.3783441 }, { 0.6437897, 0.2491322, 0.2953552 }, }; RealMatrix L = new Array2DRowRealMatrix(m255MINRESLoadings); GPArotation gpa = new GPArotation(); RotationResults R = gpa.rotate(L, RotationMethod.VARIMAX, false, 1000, 1e-5); RealMatrix Lr = R.getFactorLoadings(); // System.out.println(R.toString()); for (int i = 0; i < Lr.getRowDimension(); i++) { for (int j = 0; j < Lr.getColumnDimension(); j++) { assertEquals(" loading: ", Precision.round(true_Varimax[i][j], 4), Precision.round(Lr.getEntry(i, j), 5), 1e-4); } } }
From source file:com.joptimizer.algebra.CholeskyFactorizationTest.java
/** * This test shows that the correct check of the inversion accuracy must be done with * the scaled residual, not with the simple norm ||A.x-b|| *//*from w w w . jav a 2 s.co m*/ public void testScaledResidual() throws Exception { log.debug("testScaledResidual"); String matrixId = "1"; double[][] A = Utils .loadDoubleMatrixFromFile("factorization" + File.separator + "matrix" + matrixId + ".csv"); RealMatrix Q = MatrixUtils.createRealMatrix(A); int dim = Q.getRowDimension(); RealVector b = new ArrayRealVector(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); CholeskyFactorization cs = new CholeskyFactorization(DoubleFactory2D.dense.make(Q.getData())); cs.factorize(); RealVector x = new ArrayRealVector(cs.solve(DoubleFactory1D.dense.make(b.toArray())).toArray()); //scaledResidual = ||Ax-b||_oo/( ||A||_oo . ||x||_oo + ||b||_oo ) // with ||x||_oo = max(x[i]) double scaledResidual = Utils.calculateScaledResidual(Q.getData(), x.toArray(), b.toArray()); log.debug("scaledResidual: " + scaledResidual); assertTrue(scaledResidual < Utils.getDoubleMachineEpsilon()); //b - A.x //checking the simple norm, this will fail double n1 = b.subtract(Q.operate(x)).getNorm(); log.debug("||b - A.x||: " + n1); //assertTrue(n1 < 1.E-8); }
From source file:eagle.security.userprofile.impl.UserProfileAnomalyKDEEvaluator.java
@Override public List<MLCallbackResult> detect(final String user, final String algorithm, UserActivityAggModel userActivity, UserProfileKDEModel aModel) { List<MLCallbackResult> mlPredictionOutputList = new ArrayList<MLCallbackResult>(); RealMatrix inputData = userActivity.matrix(); double[] probabilityEstimation = new double[inputData.getRowDimension()]; for (int i = 0; i < probabilityEstimation.length; i++) probabilityEstimation[i] = 1.0;/*w w w. jav a2 s . co m*/ boolean[][] anomalyFeature = new boolean[inputData.getRowDimension()][inputData.getColumnDimension()]; for (int i = 0; i < anomalyFeature.length; i++) { for (int j = 0; j < anomalyFeature[i].length; j++) { anomalyFeature[i][j] = false; } } if (aModel == null) { LOG.info("No model available for this uer, returning"); return null; } Map<String, String> context = new HashMap<String, String>() { { put(UserProfileConstants.USER_TAG, user); put(UserProfileConstants.ALGORITHM_TAG, algorithm); } }; for (int i = 0; i < inputData.getRowDimension(); i++) { List<String> cmds = JavaConversions.seqAsJavaList(userActivity.cmdTypes()); if (inputData.getColumnDimension() != cmds.size()) { LOG.error("Test data is not with same dimension as training, aborting..."); return null; } else { UserCommandStatistics[] listStats = aModel.statistics(); for (int j = 0; j < inputData.getColumnDimension(); j++) { // LOG.info("mean for j=" + j + " is:" + listStats[j].getMean()); // LOG.info("stddev for j=" + j + " is:" + listStats[j].getStddev()); if (listStats[j].isLowVariant()) { // LOG.info(listStats[j].getCommandName() + " is low variant for user: " + user); if (inputData.getEntry(i, j) > listStats[j].getMean()) { probabilityEstimation[i] *= Double.NEGATIVE_INFINITY; anomalyFeature[i][j] = true; } } else { double stddev = listStats[j].getStddev(); //LOG.info("stddev: " + stddev); double mean = listStats[j].getMean(); //LOG.info("mean: " + mean); double sqrt2PI = Math.sqrt(2.0 * Math.PI); //LOG.info("sqrt2PI: " + sqrt2PI); double denominatorFirstPart = sqrt2PI * stddev; //LOG.info("denominatorFirstPart: " + denominatorFirstPart); double squareMeanNormal = Math.pow((inputData.getEntry(i, j) - mean), 2); //LOG.info("squareMeanNormal: " + squareMeanNormal); double twoPowStandardDev = Math.pow(stddev, 2); //LOG.info("twoPowStandardDev: " + twoPowStandardDev); double twoTimesTwoPowStandardDev = 2.0 * twoPowStandardDev; //LOG.info("twoTimesTwoPowStandardDev: " + twoTimesTwoPowStandardDev); double tempVal = ((1.00 / denominatorFirstPart) * (Math.exp(-(squareMeanNormal / twoTimesTwoPowStandardDev)))); probabilityEstimation[i] *= tempVal; //LOG.info("probabilityEstimation: " + probabilityEstimation[i]); if ((inputData.getEntry(i, j) - mean) > 2 * stddev) anomalyFeature[i][j] = true; } } } } for (int i = 0; i < probabilityEstimation.length; i++) { MLCallbackResult callBackResult = new MLCallbackResult(); callBackResult.setContext(context); //LOG.info("probability estimation for data @" + i + " is: " + probabilityEstimation[i]); if (probabilityEstimation[i] < aModel.maxProbabilityEstimate()) { callBackResult.setAnomaly(true); for (int col = 0; col < anomalyFeature[i].length; col++) { //LOG.info("feature anomaly? " + (featureVals[col] == true)); if (anomalyFeature[i][col] == true) { callBackResult.setFeature(aModel.statistics()[col].getCommandName()); } } } else { callBackResult.setAnomaly(false); } callBackResult.setTimestamp(userActivity.timestamp()); List<String> datapoints = new ArrayList<String>(); double[] rowVals = userActivity.matrix().getRow(i); for (double rowVal : rowVals) datapoints.add(rowVal + ""); callBackResult.setDatapoints(datapoints); callBackResult.setId(user); callBackResult.setAlgorithm(UserProfileConstants.KDE_ALGORITHM); mlPredictionOutputList.add(callBackResult); } return mlPredictionOutputList; }
From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotationTest.java
@Test public void testM255Olimin() { System.out.println("Oblimin rotation test: m255 data"); /**//from www .j a v a2s. c o m * True result obtained form R using GPArotation package */ double[][] true_Oblimin = { { 0.818982935, -0.086728237, -0.03014828 }, { 0.836948168, -0.028356336, 0.02072749 }, { 0.650083626, 0.102202784, -0.06494440 }, { 0.609805424, 0.145551832, 0.03455997 }, { 0.432053641, 0.310356386, -0.13539510 }, { 0.071824049, 0.723712703, -0.07186957 }, { -0.022431888, 0.822701356, 0.05997316 }, { 0.113162767, 0.434661061, -0.07100987 }, { 0.129672215, 0.353260863, -0.33144976 }, { -0.052966793, 0.454048208, -0.28827799 }, { 0.028011398, -0.006402917, -0.89108626 }, { -0.005289128, 0.004084979, -0.75198003 } }; RealMatrix L = new Array2DRowRealMatrix(m255MINRESLoadings); GPArotation gpa = new GPArotation(); RotationResults R = gpa.rotate(L, RotationMethod.OBLIMIN, false, 60, 1e-5); RealMatrix Lr = R.getFactorLoadings(); // System.out.println(R.toString()); for (int i = 0; i < Lr.getRowDimension(); i++) { for (int j = 0; j < Lr.getColumnDimension(); j++) { assertEquals(" loading: ", Precision.round(true_Oblimin[i][j], 4), Precision.round(Lr.getEntry(i, j), 5), 1e-4); } } }
From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotationTest.java
@Test public void testM255Quartimin() { System.out.println("Quartimin rotation test: m255 data"); /**// w w w. java 2s . c o m * True result obtained form R using GPArotation package */ double[][] true_Quartimin = { { 0.818982935, -0.086728237, -0.03014828 }, { 0.836948168, -0.028356336, 0.02072749 }, { 0.650083626, 0.102202784, -0.06494440 }, { 0.609805424, 0.145551832, 0.03455997 }, { 0.432053641, 0.310356386, -0.13539510 }, { 0.071824049, 0.723712703, -0.07186957 }, { -0.022431888, 0.822701356, 0.05997316 }, { 0.113162767, 0.434661061, -0.07100987 }, { 0.129672215, 0.353260863, -0.33144976 }, { -0.052966793, 0.454048208, -0.28827799 }, { 0.028011398, -0.006402917, -0.89108626 }, { -0.005289128, 0.004084979, -0.75198003 } }; RealMatrix L = new Array2DRowRealMatrix(m255MINRESLoadings); GPArotation gpa = new GPArotation(); RotationResults R = gpa.rotate(L, RotationMethod.QUARTIMIN, false, 500, 1e-5); RealMatrix Lr = R.getFactorLoadings(); // System.out.println(R.toString()); for (int i = 0; i < Lr.getRowDimension(); i++) { for (int j = 0; j < Lr.getColumnDimension(); j++) { assertEquals(" loading: ", Precision.round(true_Quartimin[i][j], 4), Precision.round(Lr.getEntry(i, j), 5), 1e-4); } } }
From source file:com.itemanalysis.psychometrics.cfa.TauEquivalentModel.java
public double[] getGradient(RealMatrix factorLoadingFirstDerivative, RealMatrix errorVarianceFirstDerivative) { double[] gradient = new double[getNumberOfParameters()]; double sum = 0.0; for (int i = 0; i < factorLoadingFirstDerivative.getRowDimension(); i++) { sum += factorLoadingFirstDerivative.getEntry(i, 0); }/*from w ww .java 2 s . c o m*/ gradient[0] = sum; for (int i = 0; i < errorVariance.length; i++) { gradient[i + 1] = errorVarianceFirstDerivative.getEntry(i, i); } return gradient; }