List of usage examples for org.apache.commons.math3.linear RealMatrix setEntry
void setEntry(int row, int column, double value) throws OutOfRangeException;
From source file:Rotationforest.Covariance.java
/** * Compute a covariance matrix from a matrix whose columns represent * covariates.//w w w . j a v a 2 s.c om * @param matrix input matrix (must have at least one column and two rows) * @param biasCorrected determines whether or not covariance estimates are bias-corrected * @return covariance matrix * @throws MathIllegalArgumentException if the matrix does not contain sufficient data */ protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected) throws MathIllegalArgumentException { int dimension = matrix.getColumnDimension(); Variance variance = new Variance(biasCorrected); RealMatrix outMatrix = new BlockRealMatrix(dimension, dimension); for (int i = 0; i < dimension; i++) { for (int j = 0; j < i; j++) { double cov = covariance(matrix.getColumn(i), matrix.getColumn(j), biasCorrected); outMatrix.setEntry(i, j, cov); outMatrix.setEntry(j, i, cov); } outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i))); outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i))); } //return outMatrix; return outMatrix; }
From source file:scorePairing.ScorePairingWithStaticMethods.java
public static ResultFromScorePairing computeCost(PairingAndNullSpaces currentNewPairingAndNewNullSpaces, Map<Integer, PointWithPropertiesIfc> queryShape, Map<Integer, PointWithPropertiesIfc> hitShape, AlgoParameters algoParameters) { int countOfPairedPoint = currentNewPairingAndNewNullSpaces.getPairing().size(); double[][] matrixPointsModelDouble = new double[3][countOfPairedPoint]; double[][] matrixPointsCandidateDouble = new double[3][countOfPairedPoint]; RealMatrix matrixPointsModel = new Array2DRowRealMatrix(matrixPointsModelDouble); RealMatrix matrixPointsCandidate = new Array2DRowRealMatrix(matrixPointsCandidateDouble); int currentPoint = 0; RealVector sumVector1 = new ArrayRealVector(new double[3]); RealVector sumVector2 = new ArrayRealVector(new double[3]); sumVector1.setEntry(0, 0.0);/*from w ww . j a v a 2 s. c om*/ sumVector1.setEntry(1, 0.0); sumVector1.setEntry(2, 0.0); sumVector2.setEntry(0, 0.0); sumVector2.setEntry(1, 0.0); sumVector2.setEntry(2, 0.0); for (Map.Entry<Integer, Integer> entry : currentNewPairingAndNewNullSpaces.getPairing().entrySet()) { Integer point1id = Integer.valueOf(entry.getKey().intValue()); Integer point2id = Integer.valueOf(entry.getValue().intValue()); // the 2 points could be defined as static be static PointWithPropertiesIfc point1 = queryShape.get(point1id); PointWithPropertiesIfc point2 = hitShape.get(point2id); // matrix for procrustes matrixPointsModel.setEntry(0, currentPoint, point1.getCoords().getCoords()[0]); matrixPointsModel.setEntry(1, currentPoint, point1.getCoords().getCoords()[1]); matrixPointsModel.setEntry(2, currentPoint, point1.getCoords().getCoords()[2]); matrixPointsCandidate.setEntry(0, currentPoint, point2.getCoords().getCoords()[0]); matrixPointsCandidate.setEntry(1, currentPoint, point2.getCoords().getCoords()[1]); matrixPointsCandidate.setEntry(2, currentPoint, point2.getCoords().getCoords()[2]); // barycenter computation sumVector1.addToEntry(0, point1.getCoords().getCoords()[0]); sumVector1.addToEntry(1, point1.getCoords().getCoords()[1]); sumVector1.addToEntry(2, point1.getCoords().getCoords()[2]); sumVector2.addToEntry(0, point2.getCoords().getCoords()[0]); sumVector2.addToEntry(1, point2.getCoords().getCoords()[1]); sumVector2.addToEntry(2, point2.getCoords().getCoords()[2]); currentPoint += 1; } RealVector barycenterVector1 = sumVector1.mapDivide((double) currentPoint); RealVector barycenterVector2 = sumVector2.mapDivide((double) currentPoint); RealVector translationVectorToTranslateShape2ToOrigin = barycenterVector2.mapMultiply(-1.0); RealVector translationVectorToTranslateShape2ToShape1 = barycenterVector1.subtract(barycenterVector2); translateBarycenterListOfPointToOrigin(matrixPointsModel, barycenterVector1); translateBarycenterListOfPointToOrigin(matrixPointsCandidate, barycenterVector2); ProcrustesAnalysisIfc procrustesAnalysis = null; try { procrustesAnalysis = algoParameters.procrustesAnalysisBuffer.get(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } procrustesAnalysis.run(matrixPointsModel, matrixPointsCandidate); RealMatrix rotationMatrixToRotateShape2ToShape1 = procrustesAnalysis.getRotationMatrix(); try { algoParameters.procrustesAnalysisBuffer.put(procrustesAnalysis); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // not used: we dont really care how accurate is the overlap of points in pairs as it is within expected range because of the // fact that I extend hits with a distance criteria double distanceResidual = procrustesAnalysis.getResidual(); // no need to use hit coverage as already used as a filter // no need to use the distance to outside as it is used as a filter // handling of probability to have a small contribution of if float toleranceElectronProbability = 0.2f; // use only properties for cost double costCharge = 0.0; double costHydrophobicity = 0.0; double costHbondDonnor = 0.0; double costHbondAcceptor = 0.0; double costDehydron = 0.0; double costAromaticRing = 0.0; double costProbabilityDiff = 0.0; for (Map.Entry<Integer, Integer> entry : currentNewPairingAndNewNullSpaces.getPairing().entrySet()) { Integer idFromMap1 = entry.getKey(); Integer idFromMap2 = entry.getValue(); PointWithPropertiesIfc point1 = queryShape.get(idFromMap1); PointWithPropertiesIfc point2 = hitShape.get(idFromMap2); float probabilityOfPoint1 = point1.getElectronProbability(); float probabilityOfPoint2 = point2.getElectronProbability(); float probabilityDiff = Math.abs(probabilityOfPoint1 - probabilityOfPoint2); if (probabilityDiff < toleranceElectronProbability) { probabilityDiff = 0.0f; } else { probabilityDiff -= toleranceElectronProbability; } costProbabilityDiff += probabilityDiff; // handle multi properties // Hydrophobe and Aromatic Float hydrophobicityOfPointFromMap1 = point1.get(PropertyName.Hydrophobicity); Float hydrophobicityOfPointFromMap2 = point2.get(PropertyName.Hydrophobicity); Float aromaticRingOfPointFromMap1 = point1.get(PropertyName.AromaticRing); Float aromaticRingOfPointFromMap2 = point2.get(PropertyName.AromaticRing); // if aromatic it is hydrophobe as well by definition if (aromaticRingOfPointFromMap1 != null && hydrophobicityOfPointFromMap1 == null) { System.out.println(aromaticRingOfPointFromMap1 != null && hydrophobicityOfPointFromMap1 == null); } if (aromaticRingOfPointFromMap2 != null && hydrophobicityOfPointFromMap2 == null) { System.out.println(aromaticRingOfPointFromMap2 != null && hydrophobicityOfPointFromMap2 == null); } // if one hydrophobe and not the other one // then cost hydrophobe if (hydrophobicityOfPointFromMap1 != null && hydrophobicityOfPointFromMap2 == null) { costHydrophobicity += returnCost(hydrophobicityOfPointFromMap1, hydrophobicityOfPointFromMap2); } if (hydrophobicityOfPointFromMap2 != null && hydrophobicityOfPointFromMap1 == null) { costHydrophobicity += returnCost(hydrophobicityOfPointFromMap1, hydrophobicityOfPointFromMap2); } // so a difference of hydrophobe to aromatic is neglected !!! Float chargeOfPointFromMap1 = point1.get(PropertyName.FormalCharge); Float chargeOfPointFromMap2 = point2.get(PropertyName.FormalCharge); costCharge += returnCost(chargeOfPointFromMap1, chargeOfPointFromMap2); Float hBondDonnorOfPointFromMap1 = point1.get(PropertyName.HbondDonnor); Float hBondDonnorOfPointFromMap2 = point2.get(PropertyName.HbondDonnor); Float hBondAcceptorOfPointFromMap1 = point1.get(PropertyName.HbondAcceptor); Float hBondAcceptorOfPointFromMap2 = point2.get(PropertyName.HbondAcceptor); costHbondDonnor += returnCost(hBondDonnorOfPointFromMap1, hBondDonnorOfPointFromMap2); costHbondAcceptor += returnCost(hBondAcceptorOfPointFromMap1, hBondAcceptorOfPointFromMap2); Float dehydronOfPointFromMap1 = point1.get(PropertyName.Dehydron); Float dehydronOfPointFromMap2 = point2.get(PropertyName.Dehydron); costDehydron += returnCost(dehydronOfPointFromMap1, dehydronOfPointFromMap2); } int size = currentNewPairingAndNewNullSpaces.getPairing().size(); costCharge = costCharge / size; costHbondDonnor = costHbondDonnor / size; costHbondAcceptor = costHbondAcceptor / size; costDehydron = costDehydron / size; costHydrophobicity = costHydrophobicity / size; costAromaticRing = costAromaticRing / size; costProbabilityDiff = costProbabilityDiff / size; double costOnPairs = algoParameters.getWEIGHT_DIFFERENCE_IN_CHARGES_BETWEEN_PAIRED_POINTS() * costCharge + algoParameters.getWEIGHT_DIFFERENCE_IN_HYDROPHOBICITY_BETWEEN_PAIRED_POINTS() * costHydrophobicity + algoParameters.getWEIGHT_HBOND_DONNOR() * costHbondDonnor + algoParameters.getWEIGHT_HBOND_ACCEPTOR() * costHbondAcceptor + algoParameters.getWEIGHT_DEHYDRON() * costDehydron + algoParameters.getWEIGHT_DIFFERENCE_AROMATICRING() * costAromaticRing + algoParameters.getWEIGHT_DIFFERENCE_IN_PROBABILITIES_IN_PAIRED_POINTS() * costProbabilityDiff; //double costOnUnpairedPoints = computeRatioFromUnpairedPointsHitToAllPointsHits(currentNewPairingAndNewNullSpaces, hitShape); //double newFinalCost = costOnPairs + algoParameters.getWEIGHT_UNPAIRED_POINT_IN_SMALLEST_MAP() * costOnUnpairedPoints; double sumWeight = algoParameters.getWEIGHT_DIFFERENCE_IN_CHARGES_BETWEEN_PAIRED_POINTS() + algoParameters.getWEIGHT_DIFFERENCE_IN_HYDROPHOBICITY_BETWEEN_PAIRED_POINTS() + algoParameters.getWEIGHT_HBOND_DONNOR() + algoParameters.getWEIGHT_HBOND_ACCEPTOR() + algoParameters.getWEIGHT_DEHYDRON() + algoParameters.getWEIGHT_DIFFERENCE_AROMATICRING() + algoParameters.getWEIGHT_DIFFERENCE_IN_PROBABILITIES_IN_PAIRED_POINTS(); double finalCost = costOnPairs / sumWeight; ResultFromScorePairing resultFromScorePairing = new ResultFromScorePairing(finalCost, rotationMatrixToRotateShape2ToShape1, translationVectorToTranslateShape2ToShape1, translationVectorToTranslateShape2ToOrigin, distanceResidual); return resultFromScorePairing; }
From source file:stats.KendallsCorrelation.java
/** * Computes the Kendall's Tau rank correlation matrix for the columns of the * input matrix.//from ww w . j a va 2 s.co m * * @param matrix matrix with columns representing variables to correlate * @return correlation matrix */ public RealMatrix computeCorrelationMatrix(final 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)).getFirst(); outMatrix.setEntry(i, j, corr); outMatrix.setEntry(j, i, corr); } outMatrix.setEntry(i, i, 1d); } return outMatrix; }
From source file:syncleus.gremlann.topology.adjacency.RealMatrixAdjacency.java
public static void fill(final RealMatrix m, final double value) { if (m instanceof Array2DRowRealMatrix) { Array2DRowRealMatrix a = (Array2DRowRealMatrix) m; for (double[] n : a.getDataRef()) Arrays.fill(n, value); } else {//ww w .j a v a 2 s . c o m for (int i = 0; i < m.getRowDimension(); i++) { for (int j = 0; j < m.getColumnDimension(); j++) { m.setEntry(i, j, value); } } } }
From source file:tinfour.gwr.SurfaceGwr.java
public void computeVarianceAndHat() { if (areVarianceAndHatPrepped) { return;//from w w w .j a v a 2s . c o m } areVarianceAndHatPrepped = true; if (sampleWeightsMatrix == null) { throw new NullPointerException("Null specification for sampleWeightsMatrix"); } else if (sampleWeightsMatrix.length != nSamples) { throw new IllegalArgumentException("Incorrectly specified sampleWeightsMatrix"); } double[][] bigS = new double[nSamples][nSamples]; double[][] bigW = sampleWeightsMatrix; double[][] input = computeDesignMatrix(model, xOffset, yOffset, nSamples, samples); RealMatrix mX = new BlockRealMatrix(input); RealMatrix mXT = mX.transpose(); // in the loop below, we compute // Tr(hat) and Tr(Hat' x Hat) // this second term is actually the square of the // Frobenius Norm. Internally, the Apache Commons classes // may provide a more numerically stable implementation of this operation. // This may be worth future investigation. double sTrace = 0; double sTrace2 = 0; for (int i = 0; i < nSamples; i++) { DiagonalMatrix mW = new DiagonalMatrix(bigW[i]); //NOPMD RealMatrix mXTW = mXT.multiply(mW); RealMatrix rx = mX.getRowMatrix(i); RealMatrix c = mXTW.multiply(mX); QRDecomposition cd = new QRDecomposition(c); // NOPMD DecompositionSolver cdSolver = cd.getSolver(); RealMatrix cInv = cdSolver.getInverse(); RealMatrix r = rx.multiply(cInv).multiply(mXTW); double[] row = r.getRow(0); sTrace += row[i]; System.arraycopy(row, 0, bigS[i], 0, nSamples); for (int j = 0; j < nSamples; j++) { sTrace2 += row[j] * row[j]; } } hat = new BlockRealMatrix(bigS); traceHat = sTrace; traceHat2 = sTrace2; double[][] zArray = new double[nSamples][1]; for (int i = 0; i < nSamples; i++) { zArray[i][0] = samples[i][2]; } RealMatrix mY = new BlockRealMatrix(zArray); RealMatrix mYH = hat.multiply(mY); double sse = 0; for (int i = 0; i < nSamples; i++) { double yHat = mYH.getEntry(i, 0); double e = zArray[i][0] - yHat; sse += e * e; } rss = sse; double d1 = nSamples - (2 * traceHat - sTrace2); sigma2 = rss / d1; mlSigma2 = rss / nSamples; RealMatrix mIL = hat.copy(); for (int i = 0; i < nSamples; i++) { double c = 1.0 - mIL.getEntry(i, i); mIL.setEntry(i, i, c); } RealMatrix mILT = mIL.transpose().multiply(mIL); delta1 = mILT.getTrace(); delta2 = (mILT.multiply(mILT)).getTrace(); }