Example usage for org.apache.commons.math3.stat.correlation PearsonsCorrelation correlation

List of usage examples for org.apache.commons.math3.stat.correlation PearsonsCorrelation correlation

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.correlation PearsonsCorrelation correlation.

Prototype

public double correlation(final double[] xArray, final double[] yArray) 

Source Link

Document

Computes the Pearson's product-moment correlation coefficient between the two arrays.

Usage

From source file:PCC.java

/**
 * @param args the command line arguments
 * @throws java.io.IOException//  w w w.  j  a  va2 s.  c  o m
 */

public static void main(String[] args) throws IOException {
    // TODO code application logic here

    PearsonsCorrelation corel = new PearsonsCorrelation();
    PCC method = new PCC();
    ArrayList<String> name = new ArrayList<>();
    Multimap<String, String> genes = ArrayListMultimap.create();
    BufferedWriter bw = new BufferedWriter(new FileWriter(args[1]));
    BufferedReader br = new BufferedReader(new FileReader(args[0]));
    String str;
    while ((str = br.readLine()) != null) {
        String[] a = str.split("\t");
        name.add(a[0]);
        for (int i = 1; i < a.length; i++) {
            genes.put(a[0], a[i]);
        }
    }
    for (String key : genes.keySet()) {
        double[] first = new double[genes.get(key).size()];
        int element1 = 0;
        for (String value : genes.get(key)) {
            double d = Double.parseDouble(value);
            first[element1] = d;
            element1++;
        }
        for (String key1 : genes.keySet()) {
            if (!key.equals(key1)) {
                double[] second = new double[genes.get(key1).size()];
                int element2 = 0;
                for (String value : genes.get(key1)) {
                    double d = Double.parseDouble(value);
                    second[element2] = d;
                    element2++;

                }
                double corrlation = corel.correlation(first, second);
                if (corrlation > 0.5) {
                    bw.write(key + "\t" + key1 + "\t" + corrlation + "\t"
                            + method.pvalue(corrlation, second.length) + "\n");
                }
            }
        }
    }
    br.close();
    bw.close();
}

From source file:edu.indiana.soic.ts.streaming.dataflow.utils.DistanceFunction.java

public static double calculateDistance(VectorPoint vp1, VectorPoint vp2) {
    //FIXME This should be replaced by the actual distance calculation
    PearsonsCorrelation pearsonsCorrelation = new PearsonsCorrelation();
    return pearsonsCorrelation.correlation(vp1.getNumbers(), vp2.getNumbers());
}

From source file:dm_p2.KMEANS.java

public static void internalValidation(Map<Integer, List<Double>> linkedMap, Map<Integer, Integer> ourMap) {
    int size = linkedMap.size();
    int n = ((size - 1) * (size)) / 2;

    double incidenceMatrix[] = new double[n];
    double distanceMatrix[] = new double[n];
    int k = 0;//from   w w  w  . j a va2s  .com
    for (int i = 1; i <= size; i++) {
        for (int j = i + 1; j <= size; j++) {
            if (ourMap.get(i) == ourMap.get(j)) {
                incidenceMatrix[k] = 1;
            } else {
                incidenceMatrix[k] = 0;
            }

            distanceMatrix[k] = euclidianDistance(linkedMap.get(i), linkedMap.get(j));
            k++;
        }
    }
    PearsonsCorrelation pc = new PearsonsCorrelation();

    System.out.println(
            "Internal Index Validation : Correlation = " + pc.correlation(incidenceMatrix, distanceMatrix));

}

From source file:com.davidbracewell.ml.regression.Regression.java

/**
 * Pearson's correlation coefficient.//from w w  w.j a  v  a 2  s . com
 *
 * @param model the model
 * @param data  the data
 * @return the double
 */
public static double correlationCoefficient(RegressionModel model, List<Instance> data) {
    PearsonsCorrelation correlation = new PearsonsCorrelation();
    List<Double> gold = Lists.newArrayList();
    List<Double> pred = Lists.newArrayList();
    for (Instance instance : data) {
        if (instance.hasTargetValue()) {
            gold.add(instance.getTargetValue());
            pred.add(model.estimate(instance));
        }
    }
    return correlation.correlation(Doubles.toArray(gold), Doubles.toArray(pred));
}

From source file:dm_p2.DBSCAN.java

public static void internalValidation(LinkedHashMap<Integer, List<Double>> linkedMap,
        Map<Integer, Integer> ourMap) {
    int size = linkedMap.size();
    int n = ((size - 1) * (size)) / 2;

    double incidenceMatrix[] = new double[n];
    double distanceMatrix[] = new double[n];
    int k = 0;//from  ww  w.  j  a v a 2 s  . c  om
    for (int i = 1; i <= size; i++) {
        for (int j = i + 1; j <= size; j++) {
            if (ourMap.get(i) == ourMap.get(j)) {
                incidenceMatrix[k] = 1;
            } else {
                incidenceMatrix[k] = 0;
            }

            distanceMatrix[k] = euclidianDistance(linkedMap.get(i), linkedMap.get(j));
            k++;
        }
    }
    PearsonsCorrelation pc = new PearsonsCorrelation();

    System.out.println(
            "Internal Index Validation : Correlation = " + pc.correlation(incidenceMatrix, distanceMatrix));

}

From source file:elaborate.tag_analysis.feature.PearsonCorrelationDistanceCalculatorImpl.java

@Override
public double getDistance(double[] feature1, double[] feature2) {
    PearsonsCorrelation p = new PearsonsCorrelation();
    double cor = p.correlation(feature1, feature2);
    return 1.0 / (cor + 2);//prevent negative value
}

From source file:com.facebook.presto.operator.aggregation.TestFloatCorrelationAggregation.java

@Override
public Object getExpectedValue(int start, int length) {
    if (length <= 1) {
        return null;
    }//  www . j  a  v  a  2  s.  c  o m
    PearsonsCorrelation corr = new PearsonsCorrelation();
    return (float) corr.correlation(constructDoublePrimitiveArray(start + 2, length),
            constructDoublePrimitiveArray(start, length));
}

From source file:com.facebook.presto.operator.aggregation.TestCorrelationAggregation.java

@Override
public Object getExpectedValue(int start, int length) {
    if (length <= 1) {
        return null;
    }//from www  . j a  v a2 s  .  co  m
    PearsonsCorrelation corr = new PearsonsCorrelation();
    return corr.correlation(constructDoublePrimitiveArray(start + 2, length),
            constructDoublePrimitiveArray(start, length));
}

From source file:com.facebook.presto.operator.aggregation.TestCorrelationAggregation.java

private void testNonTrivialAggregation(double[] y, double[] x) {
    PearsonsCorrelation corr = new PearsonsCorrelation();
    double expected = corr.correlation(x, y);
    checkArgument(Double.isFinite(expected) && expected != 0.0 && expected != 1.0,
            "Expected result is trivial");
    testAggregation(expected, createDoublesBlock(box(y)), createDoublesBlock(box(x)));
}

From source file:Evaluator.StatCalculator.java

public double correlationCalc(HashMap<String, Double> kderunQidMap, HashMap<String, Double> aprunQidMap) {
    double xArray[], yArray[];
    int count = 0;

    xArray = new double[kderunQidMap.size()];
    yArray = new double[kderunQidMap.size()];

    Iterator it = kderunQidMap.keySet().iterator();
    while (it.hasNext()) {
        String run = (String) it.next();
        xArray[count] = kderunQidMap.get(run);
        yArray[count++] = aprunQidMap.get(run);
    }/*from   w ww .j av  a 2s .  co m*/

    PearsonsCorrelation prc = new PearsonsCorrelation();
    return prc.correlation(xArray, yArray);

}