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

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:com.test.zmisc.App.java

public static void main(String[] args) {
    double[] a = new double[32768];
    double[] b = new double[32768];

    ThreadLocalRandom rand = ThreadLocalRandom.current();

    for (int i = 0; i < a.length; i++) {
        a[i] = rand.nextDouble();/*from  w ww .  j  a va2  s  .c om*/
        b[i] = a[i];
    }

    long ts = System.currentTimeMillis();
    PearsonsCorrelation corr = new PearsonsCorrelation();
    corr.correlation(a, b);
    ts = System.currentTimeMillis() - ts;
    System.out.println("Time elapsed:" + ts + "ms");

    // standard deviation is the sqrt(mean((X-avg)^2))

    ts = System.currentTimeMillis();
    double amean = average(a);
    double aStdDev = standardDeviation(a, amean);

    StandardDeviation sd = new StandardDeviation();
    sd.setBiasCorrected(false);

    double bmean = average(b);
    double bStdDev = standardDeviation(b, bmean);

    double cor = (covariance(a, aStdDev, b, bmean)) / (aStdDev * bStdDev);
    ts = System.currentTimeMillis() - ts;
    System.out.println("Time elapsed:" + ts + "ms");
}

From source file:com.ibm.bluej.commonutil.DenseVectors.java

public static double pearsonsR(double[] x, double[] y) {
    PearsonsCorrelation pc = new PearsonsCorrelation();
    double corrValue = pc.correlation(x, y);

    if (new Double(corrValue).isNaN())
        corrValue = 0.0;/*from  w w w .j ava 2 s .  c om*/

    return corrValue;
}

From source file:com.srotya.sidewinder.core.analytics.TestMathUtils.java

@Test
public void testPPMCC() {
    double[] a = new double[] { 2, 3, 4, 5, 6 };
    double[] b = new double[] { 2.2, 33.2, 44.4, 55.5, 66.6 };
    PearsonsCorrelation corr = new PearsonsCorrelation();
    double ppmcc = MathUtils.ppmcc(a, b);
    double correlation = corr.correlation(a, b);
    assertEquals(correlation, ppmcc, 0.001);
}

From source file:com.srotya.sidewinder.core.analytics.TestMathUtils.java

@Test
public void testPPMCCMulti() {
    double[][] data = new double[][] { new double[] { 2, 3, 4, 5, 6 },
            new double[] { 2.2, 33.2, 44.4, 55.5, 66.6 }, new double[] { 2, 3, 4, 5, 6 } };
    PearsonsCorrelation corr = new PearsonsCorrelation();
    double[] ppmcc = MathUtils.ppmcc(data, 0);
    for (int i = 0; i < data.length; i++) {
        double correlation = corr.correlation(data[0], data[i]);
        assertEquals(ppmcc[i], correlation, 0.001);
    }//from  w w  w.j  av  a 2  s  .c o m
}

From source file:dkpro.similarity.algorithms.structure.TokenPairDistanceMeasure.java

@Override
public double getSimilarity(Collection<String> stringList1, Collection<String> stringList2)
        throws SimilarityException {
    // Transform input lists into lowercase string lists
    List<String> sl1 = new ArrayList<String>();
    for (String s : stringList1) {
        sl1.add(s.toLowerCase());/* w  ww .  j a  va  2 s. c o  m*/
    }

    List<String> sl2 = new ArrayList<String>();
    for (String s : stringList2) {
        sl2.add(s.toLowerCase());
    }

    // Get word sets
    Set<String> strings1 = new HashSet<String>(sl1);
    Set<String> strings2 = new HashSet<String>(sl2);

    // Get a common word list
    List<String> commonStrings = new ArrayList<String>(strings1);
    commonStrings.retainAll(strings2);

    // Build up pairs (ignoring order, i.e. a-b or b-a)
    Set<Pair> pairs = new HashSet<Pair>();
    for (String s1 : commonStrings) {
        for (String s2 : commonStrings) {
            if (!s1.equals(s2)) {
                Pair p = new Pair(s1, s2);
                pairs.add(p);
            }
        }
    }

    if (pairs.size() > 1) {
        double[] v1 = new double[pairs.size()];
        double[] v2 = new double[pairs.size()];

        List<Pair> pairList = new ArrayList<Pair>(pairs);

        for (int i = 0; i < pairList.size(); i++) {
            Pair p = pairList.get(i);

            int idx1a = sl1.indexOf(p.getString1());
            int idx1b = sl1.indexOf(p.getString2());
            int idx1diff = transform(idx1a - idx1b);

            int idx2a = sl2.indexOf(p.getString1());
            int idx2b = sl2.indexOf(p.getString2());
            int idx2diff = transform(idx2a - idx2b);

            v1[i] = idx1diff;
            v2[i] = idx2diff;
        }

        PearsonsCorrelation pearson = new PearsonsCorrelation();
        return pearson.correlation(v1, v2);
    }

    return 0.0;
}

From source file:cal.binBased.Calculate_BinSpectrum_Similarity.java

/**
 * This method calculates correlation coefficients between two MSnSpectrum
 * object while taking into account of intensities of the mutual peaks. A
 * perfect score is 1 (or -1). And value range is [-1:1]
 *
 *//*from  w ww  .  j  ava 2 s  . c  o m*/
public void calculateCorrelation() {
    // TODO: Make sure if any of binSpectra is empty, score i NaN!       
    if (sim_method.equals(SimilarityMethods.SPEARMANS_CORRELATION)) {
        SpearmansCorrelation sc = new SpearmansCorrelation();
        score = sc.correlation(bin_specA.getBin_spectrum(), bin_specB.getBin_spectrum());
    } else if (sim_method.equals(SimilarityMethods.PEARSONS_CORRELATION)) {
        PearsonsCorrelation pc = new PearsonsCorrelation();
        score = pc.correlation(bin_specA.getBin_spectrum(), bin_specB.getBin_spectrum());
    }
}

From source file:org.dkpro.similarity.algorithms.structure.TokenPairDistanceMeasure.java

@Override
public double getSimilarity(Collection<String> stringList1, Collection<String> stringList2)
        throws SimilarityException {

    // Transform input lists into lowercase string lists
    List<String> sl1 = new LinkedList<String>();
    for (String s : stringList1) {
        sl1.add(s.toLowerCase());//  w w  w  . j a va  2s  .  co  m
    }

    List<String> sl2 = new LinkedList<String>();
    for (String s : stringList2) {
        sl2.add(s.toLowerCase());
    }

    // Get word sets
    Set<String> strings1 = new LinkedHashSet<String>(sl1);
    Set<String> strings2 = new LinkedHashSet<String>(sl2);

    // Get a common word list
    List<String> commonStrings = new LinkedList<String>(strings1);
    commonStrings.retainAll(strings2);

    // Build up pairs (ignoring order, i.e. a-b or b-a)
    Set<Pair> pairs = new LinkedHashSet<Pair>();
    for (String s1 : commonStrings) {
        for (String s2 : commonStrings) {
            if (!s1.equals(s2)) {
                Pair p = new Pair(s1, s2);
                pairs.add(p);
            }
        }
    }

    if (pairs.size() > 1) {
        double[] v1 = new double[pairs.size()];
        double[] v2 = new double[pairs.size()];

        List<Pair> pairList = new LinkedList<Pair>(pairs);

        for (int i = 0; i < pairList.size(); i++) {
            Pair p = pairList.get(i);

            int idx1a = sl1.indexOf(p.getString1());
            int idx1b = sl1.indexOf(p.getString2());
            int idx1diff = transform(idx1a - idx1b);

            int idx2a = sl2.indexOf(p.getString1());
            int idx2b = sl2.indexOf(p.getString2());
            int idx2diff = transform(idx2a - idx2b);

            v1[i] = idx1diff;
            v2[i] = idx2diff;
        }

        PearsonsCorrelation pearson = new PearsonsCorrelation();
        return pearson.correlation(v1, v2);
    }

    return 0.0;
}

From source file:org.dkpro.similarity.algorithms.style.FunctionWordFrequenciesMeasure.java

@Override
public double getSimilarity(JCas jcas1, JCas jcas2) throws SimilarityException {
    double[] v1 = getDocumentVector(jcas1);
    double[] v2 = getDocumentVector(jcas2);

    PearsonsCorrelation p = new PearsonsCorrelation();
    return p.correlation(v1, v2);
}

From source file:org.dkpro.similarity.algorithms.style.FunctionWordFrequenciesMeasure.java

@Override
public double getSimilarity(JCas jcas1, JCas jcas2, Annotation coveringAnnotation1,
        Annotation coveringAnnotation2) throws SimilarityException {
    double[] v1 = getDocumentVector(jcas1, coveringAnnotation1);
    double[] v2 = getDocumentVector(jcas2, coveringAnnotation2);

    PearsonsCorrelation p = new PearsonsCorrelation();
    return p.correlation(v1, v2);
}

From source file:org.dkpro.similarity.experiments.sts2013.util.Evaluator.java

@SuppressWarnings("unchecked")
public static void runEvaluationMetric(Mode mode, EvaluationMetric metric, Dataset... datasets)
        throws IOException {
    StringBuilder sb = new StringBuilder();

    // Compute Pearson correlation for the specified datasets
    for (Dataset dataset : datasets) {
        computePearsonCorrelation(mode, dataset);
    }/*w  ww  . ja va 2  s.co  m*/

    if (metric == PearsonAll) {
        List<Double> concatExp = new ArrayList<Double>();
        List<Double> concatGS = new ArrayList<Double>();

        // Concat the scores
        for (Dataset dataset : datasets) {
            File expScoresFile = new File(
                    OUTPUT_DIR + "/" + mode.toString().toLowerCase() + "/" + dataset.toString() + ".csv");

            List<String> lines = FileUtils.readLines(expScoresFile);

            for (String line : lines) {
                concatExp.add(Double.parseDouble(line));
            }
        }

        // Concat the gold standard
        for (Dataset dataset : datasets) {
            String gsScoresFilePath = GOLDSTANDARD_DIR + "/" + mode.toString().toLowerCase() + "/" + "STS.gs."
                    + dataset.toString() + ".txt";

            PathMatchingResourcePatternResolver r = new PathMatchingResourcePatternResolver();
            Resource res = r.getResource(gsScoresFilePath);
            File gsScoresFile = res.getFile();

            List<String> lines = FileUtils.readLines(gsScoresFile);

            for (String line : lines) {
                concatGS.add(Double.parseDouble(line));
            }
        }

        double[] concatExpArray = ArrayUtils.toPrimitive(concatExp.toArray(new Double[concatExp.size()]));
        double[] concatGSArray = ArrayUtils.toPrimitive(concatGS.toArray(new Double[concatGS.size()]));

        PearsonsCorrelation pearson = new PearsonsCorrelation();
        Double correl = pearson.correlation(concatExpArray, concatGSArray);

        sb.append(correl.toString());
    } else if (metric == PearsonMean) {
        List<Double> scores = new ArrayList<Double>();

        for (Dataset dataset : datasets) {
            File resultFile = new File(
                    OUTPUT_DIR + "/" + mode.toString().toLowerCase() + "/" + dataset.toString() + ".txt");
            double score = Double.parseDouble(FileUtils.readFileToString(resultFile));

            scores.add(score);
        }

        double mean = 0.0;
        for (Double score : scores) {
            mean += score;
        }
        mean = mean / scores.size();

        sb.append(mean);
    } else if (metric == PearsonWeightedMean) {
        List<Double> scores = new ArrayList<Double>();
        List<Integer> weights = new ArrayList<Integer>();

        for (Dataset dataset : datasets) {
            File resultFile = new File(
                    OUTPUT_DIR + "/" + mode.toString().toLowerCase() + "/" + dataset.toString() + ".txt");
            double score = Double.parseDouble(FileUtils.readFileToString(resultFile));

            File scoresFile = new File(
                    OUTPUT_DIR + "/" + mode.toString().toLowerCase() + "/" + dataset.toString() + ".csv");
            int weight = FileUtils.readLines(scoresFile).size();

            scores.add(score);
            weights.add(weight);
        }

        double mean = 0.0;
        int weightsum = 0;
        for (int i = 0; i < scores.size(); i++) {
            Double score = scores.get(i);
            Integer weight = weights.get(i);

            mean += weight * score;
            weightsum += weight;
        }
        mean = mean / weightsum;

        sb.append(mean);
    }

    FileUtils.writeStringToFile(
            new File(OUTPUT_DIR + "/" + mode.toString().toLowerCase() + "/" + metric.toString() + ".txt"),
            sb.toString());
}