org.apache.commons.math.stat.correlation.PearsonsCorrelation Java Examples
The following examples show how to use
org.apache.commons.math.stat.correlation.PearsonsCorrelation.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RowVsRowScoreGC.java From mzmine3 with GNU General Public License v2.0 | 5 votes |
public static double computeSimilarityScore(double[] vec1, double[] vec2, SimilarityMethodType simMethodType) throws IllegalArgumentException { double simScore = 0.0; try { if (simMethodType == SimilarityMethodType.DOT) { double[] vec1_norm = new double[vec1.length]; double[] vec2_norm = new double[vec2.length]; double div1 = 0.0, div2 = 0.0; for (int i = 0; i < vec1.length; ++i) { div1 += vec1[i] * vec1[i]; div2 += vec2[i] * vec2[i]; } for (int i = 0; i < vec1.length; ++i) { vec1_norm[i] = vec1[i] / Math.sqrt(div1); vec2_norm[i] = vec2[i] / Math.sqrt(div2); } simScore = (new ArrayRealVector(vec1_norm)).dotProduct(vec2_norm); } else if (simMethodType == SimilarityMethodType.PEARSON) { simScore = new PearsonsCorrelation().correlation(vec1, vec2); } } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Failed to compute similarity score for vec1.length=" + vec1.length + " and vec2.length=" + vec2.length); } return simScore; }
Example #2
Source File: Step10bUpperBoundStatistics.java From argument-reasoning-comprehension-task with Apache License 2.0 | 5 votes |
private static double[] computeCorrelation(List<Double> allX, List<Double> allY) { if (allX.size() < 2) { return new double[] { Double.NaN, Double.NaN }; } double[][] matrix = new double[allX.size()][]; for (int i = 0; i < allX.size(); i++) { matrix[i] = new double[2]; matrix[i][0] = allX.get(i); matrix[i][1] = allY.get(i); } PearsonsCorrelation pearsonsCorrelation = new PearsonsCorrelation(matrix); try { double pValue = pearsonsCorrelation.getCorrelationPValues().getEntry(0, 1); double correlation = pearsonsCorrelation.getCorrelationMatrix().getEntry(0, 1); SpearmansCorrelation sc = new SpearmansCorrelation(new Array2DRowRealMatrix(matrix)); double[] result = new double[2]; double pValSC = sc.getRankCorrelation().getCorrelationPValues().getEntry(0, 1); double corrSC = sc.getCorrelationMatrix().getEntry(0, 1); result[0] = corrSC; result[1] = pValSC; return result; } catch (MathException e) { throw new RuntimeException(e); } }
Example #3
Source File: RowVsRowScoreGC.java From mzmine2 with GNU General Public License v2.0 | 5 votes |
public static double computeSimilarityScore(double[] vec1, double[] vec2, SimilarityMethodType simMethodType) throws IllegalArgumentException { double simScore = 0.0; try { if (simMethodType == SimilarityMethodType.DOT) { double[] vec1_norm = new double[vec1.length]; double[] vec2_norm = new double[vec2.length]; double div1 = 0.0, div2 = 0.0; for (int i = 0; i < vec1.length; ++i) { div1 += vec1[i] * vec1[i]; div2 += vec2[i] * vec2[i]; } for (int i = 0; i < vec1.length; ++i) { vec1_norm[i] = vec1[i] / Math.sqrt(div1); vec2_norm[i] = vec2[i] / Math.sqrt(div2); } simScore = (new ArrayRealVector(vec1_norm)).dotProduct(vec2_norm); } else if (simMethodType == SimilarityMethodType.PEARSON) { simScore = new PearsonsCorrelation().correlation(vec1, vec2); } } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Failed to compute similarity score for vec1.length=" + vec1.length + " and vec2.length=" + vec2.length); } return simScore; }
Example #4
Source File: MillerUpdatingRegressionTest.java From astor with GNU General Public License v2.0 | 4 votes |
@Test public void testPCorr() { MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false); double[][] x = new double[airdata[0].length][]; double[] y = new double[airdata[0].length]; double[] cp = new double[10]; double[] yxcorr = new double[4]; double[] diag = new double[4]; double sumysq = 0.0; int off = 0; for (int i = 0; i < airdata[0].length; i++) { x[i] = new double[4]; x[i][0] = 1.0; x[i][1] = Math.log(airdata[3][i]); x[i][2] = Math.log(airdata[4][i]); x[i][3] = airdata[5][i]; y[i] = Math.log(airdata[2][i]); off = 0; for (int j = 0; j < 4; j++) { double tmp = x[i][j]; for (int k = 0; k <= j; k++, off++) { cp[off] += tmp * x[i][k]; } yxcorr[j] += tmp * y[i]; } sumysq += y[i] * y[i]; } PearsonsCorrelation pearson = new PearsonsCorrelation(x); RealMatrix corr = pearson.getCorrelationMatrix(); off = 0; for (int i = 0; i < 4; i++, off += (i + 1)) { diag[i] = FastMath.sqrt(cp[off]); } instance.addObservations(x, y); double[] pc = instance.getPartialCorrelations(0); int idx = 0; off = 0; int off2 = 6; for (int i = 0; i < 4; i++) { for (int j = 0; j < i; j++) { if (Math.abs(pc[idx] - cp[off] / (diag[i] * diag[j])) > 1.0e-8) { fail("Failed cross products... i = " + i + " j = " + j); } ++idx; ++off; } ++off; if (Math.abs(pc[i+off2] - yxcorr[ i] / (FastMath.sqrt(sumysq) * diag[i])) > 1.0e-8) { fail("failed cross product i = " + i + " y"); } } double[] pc2 = instance.getPartialCorrelations(1); idx = 0; for (int i = 1; i < 4; i++) { for (int j = 1; j < i; j++) { if (Math.abs(pc2[idx] - corr.getEntry(j, i)) > 1.0e-8) { fail("Failed cross products... i = " + i + " j = " + j); } ++idx; } } double[] pc3 = instance.getPartialCorrelations(2); if (pc3 == null) { fail("Should not be null"); } return; }
Example #5
Source File: MillerUpdatingRegressionTest.java From astor with GNU General Public License v2.0 | 4 votes |
@Test public void testPCorr() { MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false); double[][] x = new double[airdata[0].length][]; double[] y = new double[airdata[0].length]; double[] cp = new double[10]; double[] yxcorr = new double[4]; double[] diag = new double[4]; double sumysq = 0.0; int off = 0; for (int i = 0; i < airdata[0].length; i++) { x[i] = new double[4]; x[i][0] = 1.0; x[i][1] = Math.log(airdata[3][i]); x[i][2] = Math.log(airdata[4][i]); x[i][3] = airdata[5][i]; y[i] = Math.log(airdata[2][i]); off = 0; for (int j = 0; j < 4; j++) { double tmp = x[i][j]; for (int k = 0; k <= j; k++, off++) { cp[off] += tmp * x[i][k]; } yxcorr[j] += tmp * y[i]; } sumysq += y[i] * y[i]; } PearsonsCorrelation pearson = new PearsonsCorrelation(x); RealMatrix corr = pearson.getCorrelationMatrix(); off = 0; for (int i = 0; i < 4; i++, off += (i + 1)) { diag[i] = FastMath.sqrt(cp[off]); } instance.addObservations(x, y); double[] pc = instance.getPartialCorrelations(0); int idx = 0; off = 0; int off2 = 6; for (int i = 0; i < 4; i++) { for (int j = 0; j < i; j++) { if (Math.abs(pc[idx] - cp[off] / (diag[i] * diag[j])) > 1.0e-8) { fail("Failed cross products... i = " + i + " j = " + j); } ++idx; ++off; } ++off; if (Math.abs(pc[i+off2] - yxcorr[ i] / (FastMath.sqrt(sumysq) * diag[i])) > 1.0e-8) { fail("failed cross product i = " + i + " y"); } } double[] pc2 = instance.getPartialCorrelations(1); idx = 0; for (int i = 1; i < 4; i++) { for (int j = 1; j < i; j++) { if (Math.abs(pc2[idx] - corr.getEntry(j, i)) > 1.0e-8) { fail("Failed cross products... i = " + i + " j = " + j); } ++idx; } } double[] pc3 = instance.getPartialCorrelations(2); if (pc3 == null) { fail("Should not be null"); } return; }
Example #6
Source File: DoubleSeries.java From incubator-pinot with Apache License 2.0 | 4 votes |
public static double corr(Series a, Series b) { if(a.hasNull() || b.hasNull()) return NULL; return new PearsonsCorrelation().correlation(a.getDoubles().values(), b.getDoubles().values()); }