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

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

Introduction

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

Prototype

public PearsonsCorrelation() 

Source Link

Document

Create a PearsonsCorrelation instance without data

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 w  w . j  a  va 2 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;/*w w w. ja  va  2 s. co  m*/

    return corrValue;
}

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  .ja va 2s.co m
}

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: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  w w. j  a  v  a 2 s . c  om*/
    }

    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:com.ibm.bluej.commonutil.PrecisionRecallThreshold.java

public double pearsonsR() {
    double[] x = new double[scoredPlusGold.size()];
    double[] y = new double[scoredPlusGold.size()];
    int ndx = 0;/*  ww w  . j  ava  2 s  .  co m*/
    for (Pair<Double, Boolean> sg : scoredPlusGold) {
        x[ndx] = sg.first;
        y[ndx] = sg.second ? 1.0 : -1.0;
        ++ndx;
    }
    double corrValue = new PearsonsCorrelation().correlation(x, y);
    if (Double.isNaN(corrValue))
        corrValue = 0.0;

    return corrValue;
}

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 w w .j  a v a 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());//  ww  w. j a v  a  2s.  com
    }

    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);
}