/** * Computes the correlation matrix for the columns of the input matrix. * * @param matrix matrix with columns representing variables to correlate * @return correlation matrix */ public RealMatrix computeCorrelationMatrix(RealMatrix matrix) { int nVars = matrix.getColumnDimension(); RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars); for (int i = 0; i < nVars; i++) { for (int j = 0; j < i; j++) { double corr = correlation(matrix.getColumn(i), matrix.getColumn(j)); outMatrix.setEntry(i, j, corr); outMatrix.setEntry(j, i, corr); } outMatrix.setEntry(i, i, 1d); } return outMatrix; }
/** * Derives a correlation matrix from a covariance matrix. * * <p>Uses the formula <br> * <code>r(X,Y) = cov(X,Y)/s(X)s(Y)</code> where <code>r(·,·)</code> is the * correlation coefficient and <code>s(·)</code> means standard deviation. * * @param covarianceMatrix the covariance matrix * @return correlation matrix */ public RealMatrix covarianceToCorrelation(RealMatrix covarianceMatrix) { int nVars = covarianceMatrix.getColumnDimension(); RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars); for (int i = 0; i < nVars; i++) { double sigma = Math.sqrt(covarianceMatrix.getEntry(i, i)); outMatrix.setEntry(i, i, 1d); for (int j = 0; j < i; j++) { double entry = covarianceMatrix.getEntry(i, j) / (sigma * Math.sqrt(covarianceMatrix.getEntry(j, j))); outMatrix.setEntry(i, j, entry); outMatrix.setEntry(j, i, entry); } } return outMatrix; }
public static void main(String[] args) { RealMatrix coefficients2 = new Array2DRowRealMatrix( new double[][] { {0.0D, 1.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D}, {0.0D, 0.0D, 0.857D, 0.0D, 0.054D, 0.018D, 0.0D, 0.071D, 0.0D, 0.0D, 0.0D}, {0.0D, 0.0D, 0.0D, 1.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D}, {0.0D, 0.0D, 0.857D, 0.0D, 0.054D, 0.018D, 0.0D, 0.071D, 0.0D, 0.0D, 0.0D}, {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 1.0D, 0.0D, 0.0D, 0.0D, 0.0D}, {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 1.0D, 0.0D, 0.0D, 0.0D, 0.0D}, {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 1.0D, 0.0D, 0.0D}, {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.6D, 0.4D}, {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 1.0D}, {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 1.0D, 0.0D, 0.0D, 1.0D}, {0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D, 0.0D} }, false); for (int i = 0; i < 11; i++) { coefficients2.setEntry(i, i, -1d); } coefficients2 = coefficients2.transpose(); DecompositionSolver solver = new LUDecompositionImpl(coefficients2).getSolver(); System.out.println("1 method my Value :"); RealVector constants = new ArrayRealVector(new double[] {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false); RealVector solution = solver.solve(constants); double[] data = solution.getData(); DecimalFormat df = new DecimalFormat(); df.setRoundingMode(RoundingMode.DOWN); System.out.println("Корни уравнения:"); for (double dd : data) { System.out.print(df.format(dd) + " "); } System.out.println(); System.out.println( "Среднее число процессорных операций, выполняемых при одном прогоне алгоритма: " + operationsByProcess(data, arr)); System.out.println("Среднее число обращений к файлам:"); for (int i = 1; i < 4; i++) { System.out.println(" Файл " + i + " : " + fileMiddleRequest(data, arr, i)); } System.out.println("Среднее количество информации передаваемой при одном обращении к файлам:"); for (int i = 1; i < 4; i++) { System.out.println(" Файл " + i + " : " + bitsPerFileTransfer(data, arr, i)); } System.out.println( "Сумма среднего числа обращений к основным операторам: " + operatorExecute(data, arr)); System.out.println("Средняя трудоемкость этапа: " + middleWork(data, arr)); }
/** * Solve an estimation problem using a least squares criterion. * * <p>This method set the unbound parameters of the given problem starting from their current * values through several iterations. At each step, the unbound parameters are changed in order to * minimize a weighted least square criterion based on the measurements of the problem. * * <p>The iterations are stopped either when the criterion goes below a physical threshold under * which improvement are considered useless or when the algorithm is unable to improve it (even if * it is still high). The first condition that is met stops the iterations. If the convergence it * not reached before the maximum number of iterations, an {@link EstimationException} is thrown. * * @param problem estimation problem to solve * @exception EstimationException if the problem cannot be solved * @see EstimationProblem */ @Override public void estimate(EstimationProblem problem) throws EstimationException { initializeEstimate(problem); // work matrices double[] grad = new double[parameters.length]; ArrayRealVector bDecrement = new ArrayRealVector(parameters.length); double[] bDecrementData = bDecrement.getDataRef(); RealMatrix wGradGradT = MatrixUtils.createRealMatrix(parameters.length, parameters.length); // iterate until convergence is reached double previous = Double.POSITIVE_INFINITY; do { // build the linear problem incrementJacobianEvaluationsCounter(); RealVector b = new ArrayRealVector(parameters.length); RealMatrix a = MatrixUtils.createRealMatrix(parameters.length, parameters.length); for (int i = 0; i < measurements.length; ++i) { if (!measurements[i].isIgnored()) { double weight = measurements[i].getWeight(); double residual = measurements[i].getResidual(); // compute the normal equation for (int j = 0; j < parameters.length; ++j) { grad[j] = measurements[i].getPartial(parameters[j]); bDecrementData[j] = weight * residual * grad[j]; } // build the contribution matrix for measurement i for (int k = 0; k < parameters.length; ++k) { double gk = grad[k]; for (int l = 0; l < parameters.length; ++l) { wGradGradT.setEntry(k, l, weight * gk * grad[l]); } } // update the matrices a = a.add(wGradGradT); b = b.add(bDecrement); } } try { // solve the linearized least squares problem RealVector dX = new LUDecompositionImpl(a).getSolver().solve(b); // update the estimated parameters for (int i = 0; i < parameters.length; ++i) { parameters[i].setEstimate(parameters[i].getEstimate() + dX.getEntry(i)); } } catch (InvalidMatrixException e) { throw new EstimationException("unable to solve: singular problem"); } previous = cost; updateResidualsAndCost(); } while ((getCostEvaluations() < 2) || (Math.abs(previous - cost) > (cost * steadyStateThreshold) && (Math.abs(cost) > convergence))); }