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

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

Introduction

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

Prototype

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

Source Link

Document

Computes the Spearman's rank correlation coefficient between the two arrays.

Usage

From source file:edu.illinois.cs.cogcomp.saul.test.TestReal.java

public TestReal(Learner classifier, Classifier oracle, Parser parser) {
    int examples = 0;
    double totalDifference = 0;
    double[] actuals = {};
    double[] predictions = {};

    classifier.write(System.out);

    for (Object example = parser.next(); example != null; example = parser.next()) {
        double prediction = classifier.realValue(example);

        predictions = Arrays.copyOf(predictions, predictions.length + 1);
        predictions[predictions.length - 1] = prediction;

        double value = oracle.realValue(example);

        actuals = Arrays.copyOf(actuals, actuals.length + 1);
        actuals[actuals.length - 1] = value;

        double difference = Math.abs(prediction - value);
        totalDifference += difference;//  w  w  w  .  j  av  a2s . com
        classifier.classify(example);
        ++examples;

        System.out.println(
                "Example " + examples + " difference: " + difference + " (prediction: " + prediction + ")");
    }
    System.out.println("test examples number: " + examples);
    double avg = totalDifference / examples;
    System.out.println("Average difference: " + avg);
    double p = getPearsonCorrelation(predictions, actuals);
    System.out.println("Pearson correlation:" + p);
    SpearmansCorrelation e = new SpearmansCorrelation();
    double sp = e.correlation(predictions, actuals);
    System.out.println("Spearman correlation:" + sp);

}

From source file:eu.eexcess.diversityasurement.CorrelationTest.java

@Test
public void spearmansRho_givenAKartesianQuadrandIIandIVSymmetralePrallelLine() {
    CorrelationValues c = newCorrelationValue2();
    SpearmansCorrelation sCorrelation = new SpearmansCorrelation();
    System.out.println("spearman correlation r(x, y)=" + sCorrelation.correlation(c.x, c.y));
    assertEquals(sCorrelation.correlation(c.x, c.y), c.spearmanRho, c.epsilon);
}

From source file:eu.eexcess.diversityasurement.CorrelationTest.java

@Test
public void spearmansRho_givenAKartesianQuadrandIandIIISymmetraleParallelLine() {
    CorrelationValues c = newCorrelationValue3();
    SpearmansCorrelation sCorrelation = new SpearmansCorrelation();
    System.out.println("spearman correlation r(x, y)=" + sCorrelation.correlation(c.x, c.y));
    assertEquals(sCorrelation.correlation(c.x, c.y), c.spearmanRho, c.epsilon);
}

From source file:eu.eexcess.diversityasurement.CorrelationTest.java

@Test
public void spearmansRho_givenPosXAxis() {
    CorrelationValues c = newCorrelationValue4();
    SpearmansCorrelation sCorrelation = new SpearmansCorrelation();
    System.out.println("spearman correlation r(x, y)=" + sCorrelation.correlation(c.x, c.y));
    assertTrue(Double.isNaN(sCorrelation.correlation(c.x, c.y)));
}

From source file:eu.eexcess.diversityasurement.CorrelationTest.java

@Test
public void spearmansRho_givenDefaultValues_expectCorrectResult() {
    CorrelationValues c = newCorrelationValue1();
    SpearmansCorrelation sCorrelation = new SpearmansCorrelation();
    PearsonsCorrelation pCorrelatoin = new PearsonsCorrelation();

    System.out.println("spearman correlation r(x, y)=" + sCorrelation.correlation(c.x, c.y));
    assertEquals(sCorrelation.correlation(c.x, c.y), c.spearmanRho, c.epsilon);
    System.out.println("pearson correlation  r(x, y)=" + pCorrelatoin.correlation(c.x, c.y));
    assertEquals(sCorrelation.correlation(c.y, c.x), c.spearmanRho, c.epsilon);
}

From source file:com.thoughtworks.studios.journey.models.ActionCorrelationCalculation.java

public List<CorrelationResult> calculate() {
    Map<String, List<List<Integer>>> data = rawData();
    List<CorrelationResult> results = new ArrayList<>(data.size());

    for (String action : data.keySet()) {
        SpearmansCorrelation correlation = new SpearmansCorrelation();

        List<List<Integer>> variables = data.get(action);
        double[] x = toDoubleArray(variables.get(0));
        double[] y = toDoubleArray(variables.get(1));

        double r = correlation.correlation(x, y);
        TDistribution tDistribution = new TDistribution(x.length - 2);
        double t = FastMath.abs(r * FastMath.sqrt((x.length - 2) / (1 - r * r)));
        double pValue = 2 * tDistribution.cumulativeProbability(-t);

        SummaryStatistics successSt = new SummaryStatistics();
        SummaryStatistics failSt = new SummaryStatistics();
        for (int i = 0; i < x.length; i++) {
            if (y[i] == 1) {
                successSt.addValue(x[i]);
            } else {
                failSt.addValue(x[i]);/*from  w ww .  j a  v a 2  s  . c o  m*/
            }
        }

        results.add(new CorrelationResult(action, r, pValue, successSt, failSt));
    }

    Collections.sort(results, new Comparator<CorrelationResult>() {
        @Override
        public int compare(CorrelationResult r1, CorrelationResult r2) {
            Double abs1 = Math.abs(r2.getCorrelation());
            Double abs2 = Math.abs(r1.getCorrelation());
            return abs1.compareTo(abs2);
        }
    });
    return results;
}

From source file:hms.hwestra.interactionrebuttal.InteractionRebuttal.java

public void spearmanSE(String normal1, String robust1) throws IOException {
    HashMap<String, Double> eQTLToSE = loadSE(normal1);
    HashMap<String, Double> eQTLToSERobust = loadSE(robust1);

    Set<String> eqtls = eQTLToSE.keySet();
    ArrayList<Double> x = new ArrayList<Double>();
    ArrayList<Double> y = new ArrayList<Double>();
    for (String s : eqtls) {

        x.add(eQTLToSE.get(s));//from w w w. ja va2  s  .  c  o  m
        y.add(eQTLToSERobust.get(s));

    }

    SpearmansCorrelation corr = new SpearmansCorrelation();
    double c = corr.correlation(Primitives.toPrimitiveArr(x.toArray(new Double[0])),
            Primitives.toPrimitiveArr(y.toArray(new Double[0])));
    System.out.println(c);
}

From source file:com.medlog.webservice.vo.DiaryAnalysisSummaryVO.java

private void populateCorrelation() {
    SpearmansCorrelation c = new SpearmansCorrelation();

    getCorr()[IDX_MOOD] = c.correlation(getMood(), getMood());
    getCorr()[IDX_AGREEABLENESS_BIG5] = c.correlation(getMood(), getAgreeablenessBig5());
    getCorr()[IDX_MOOD] = c.correlation(getMood(), getMood());
    getCorr()[IDX_AGREEABLENESS_BIG5] = c.correlation(getMood(), getAgreeablenessBig5());
    getCorr()[IDX_ANALYTICAL] = c.correlation(getMood(), getAnalytical());
    getCorr()[IDX_ANGER] = c.correlation(getMood(), getAnger());
    getCorr()[IDX_CONFIDENT] = c.correlation(getMood(), getConfident());
    getCorr()[IDX_CONSCIENTIOUSNESS_BIG5] = c.correlation(getMood(), getConscientiousnessBig5());
    getCorr()[IDX_DISGUST] = c.correlation(getMood(), getDisgust());
    getCorr()[IDX_EMOTIONALRANGE_BIG5] = c.correlation(getMood(), getEmotionalRangeBig5());
    getCorr()[IDX_EXTRAVERSION_BIG5] = c.correlation(getMood(), getExtraversionBig5());
    getCorr()[IDX_FEAR] = c.correlation(getMood(), getFear());
    getCorr()[IDX_JOY] = c.correlation(getMood(), getJoy());
    getCorr()[IDX_OPENNESS_BIG5] = c.correlation(getMood(), getOpennessBig5());
    getCorr()[IDX_SADNESS] = c.correlation(getMood(), getSadness());
    getCorr()[IDX_TENTATIVE] = c.correlation(getMood(), getTentative());
    double corrTemp;
    for (int k = 0; k < getCorr().length; k++) {
        corrTemp = Math.pow(getCorr()[k], 2);
        if (StrUtl.matchOR(k, IDX_ANGER, IDX_DISGUST, IDX_FEAR, IDX_SADNESS)) {
            corrTemp = .99 - corrTemp;//w w  w  .j a v a  2 s .  c  om
        }
        getrSquared()[k] = corrTemp;
    }
    double q3 = org.apache.commons.math3.stat.StatUtils.percentile(getrSquared(), .75);

    double q1 = org.apache.commons.math3.stat.StatUtils.percentile(getrSquared(), .25);
    double var = StatUtils.variance(getrSquared());
    System.out.println("q1" + q1);
    System.out.println("q3" + q3);
    System.out.println("var" + var);
    double max = StatUtils.max(getrSquared());

    double min = StatUtils.max(getrSquared());

    setSum(StatUtils.sum(getrSquared()) - 1.0);
    System.out.println("sum" + getSum());
    for (int j = 1; j < 14; j++) {
        getCorrRanked()[j] = getrSquared()[j] / getSum();
    }
    System.out.println("pmf sum" + StatUtils.sum(getCorrRanked()));
    double[] cRCopy = ArrayUtils.clone(getCorrRanked());
    Arrays.sort(cRCopy);
    ArrayUtils.reverse(cRCopy);
    setToneList(new ArrayList<ToneKeyValuePair>());
    for (int j = 1; j < 14; j++) {
        ArrayUtils.indexOf(cRCopy, getCorrRanked()[j]);
        //ToneKeyValuePair t = ToneKeyValuePair.builder().key(CORR_STR[j]).value(getrSquared()[j]).weightedValue(getCorrRanked()[j]).rank(ArrayUtils.indexOf(cRCopy, getCorrRanked()[j])).build();
        getToneList().add(ToneKeyValuePair.builder().key(CORR_STR[j]).value(getrSquared()[j])
                .weightedValue(getCorrRanked()[j]).rank(ArrayUtils.indexOf(cRCopy, getCorrRanked()[j]) + 1)
                .historicalRawAvg(getAverageScores()[j]).build());
        //            corrRanked[j] = rSquared[j] / sum;
    }

    double guess = 0;

    for (ToneKeyValuePair t : getToneList()) {
        guess += (t.getWeightedValue() * 10 * getValFromKeyPct(t.getKey()));//t.getWeightedValue());

        System.out.println(t.toString());
        System.out.println("com.medlog.webservice.vo.DiaryAnalysisSummaryVO.populateCorrelation() Guess with "
                + t.getRank() + "== " + guess);
    }
    SimpleRegression sg = new SimpleRegression(false);
    populateLineGuessPoints(sg);

    getGuesses()[1] = sg.predict(getToneCurrentAvgX());
    System.out
            .println("\n\n\ncom.medlog.webservice.vo.DiaryAnalysisSummaryVO.populateCorrelation() GUESS === >");

    System.out.printf("Weighted (history) Guess ------> %.3f%n", (guess));
    System.out.printf("Best fit line Guess -----------> %.3f%n", sg.predict(getToneCurrentAvgX()));
    guess /= 10;
    guess = Math.round(guess);
    System.out.printf("Weighted (history) Guess adj.-----> %.3f%n", (guess));
    System.out.println("-------------------------------------------\n");
    getGuesses()[0] = guess;
    getLineEq()[SLOPE] = sg.getSlope();
    getLineEq()[YINT] = sg.getIntercept();
    getLineEq()[R] = sg.getRSquare();
    double[] norm = StatUtils.normalize(getCorrRanked());

    if (DEBUG) {
        System.out.println("com.medlog.webservice.vo.DiaryAnalysisSummaryVO.populateCorrelation()"
                + Arrays.toString(norm));
    }
    getFiveSummary()[0] = StatUtils.min(norm);
    getFiveSummary()[1] = StatUtils.percentile(norm, 25);

    getFiveSummary()[2] = StatUtils.percentile(norm, 50);

    getFiveSummary()[3] = StatUtils.percentile(norm, 75);

    getFiveSummary()[4] = StatUtils.max(norm);

}

From source file:mase.generic.WeightedClusterSCPostEval.java

private HashMap<Integer, Double> stateCorrelations(EvolutionState state) {
    SpearmansCorrelation spearman = new SpearmansCorrelation();
    HashMap<Integer, Double> pointWeight = new HashMap<Integer, Double>(buffer.size() * 2);
    double[] fitScores = new double[super.currentPop.size()];
    int index = 0;
    for (Subpopulation sub : state.population.subpops) {
        for (Individual ind : sub.individuals) {
            NoveltyFitness nf = (NoveltyFitness) ind.fitness;
            fitScores[index++] = nf.getFitnessScore();
        }/*from   www.  j  a va 2 s. c om*/
    }
    for (Integer k : buffer.keySet()) {
        double[] cs = new double[super.currentPop.size()];
        for (int i = 0; i < super.currentPop.size(); i++) {
            Float count = currentPop.get(i).getCounts().get(k);
            cs[i] = count == null ? 0 : count;
        }
        double c = Math.abs(spearman.correlation(fitScores, cs));
        pointWeight.put(k, c);
    }
    return pointWeight;
}

From source file:hms.hwestra.interactionrebuttal2.InteractionRebuttal2.java

private void determineInteractionPvalueAndMerge(String normal1, String robust1, String out) throws IOException {

    InteractionRebuttal a = new InteractionRebuttal();
    HashMap<String, Double> normalSE = a.loadSE(normal1);
    HashMap<String, Double> robustSE = a.loadSE(robust1);

    HashMap<String, Double> normalZ = loadZ(normal1);
    HashMap<String, Double> robustZ = loadZ(robust1);

    Set<String> eqtls = normalZ.keySet();
    TextFile tf = new TextFile(out, TextFile.W);
    SpearmansCorrelation corr = new SpearmansCorrelation();
    tf.writeln("SNP-Probe\tZNorm\tZRobust\tPNorm\tPRobust\tSENorm\tSERobust");
    ArrayList<Double> x = new ArrayList<Double>();
    ArrayList<Double> y = new ArrayList<Double>();
    ArrayList<Double> x2 = new ArrayList<Double>();
    ArrayList<Double> y2 = new ArrayList<Double>();

    ArrayList<Double> x3 = new ArrayList<Double>();
    ArrayList<Double> y3 = new ArrayList<Double>();
    for (String s : eqtls) {
        if (robustZ.containsKey(s)) {
            if (!Double.isNaN(normalZ.get(s))) {
                x.add(normalZ.get(s));/*  w  w  w.  j ava2s  .c o  m*/
                y.add(robustZ.get(s));
                x2.add(-Math.log10(ZScores.zToP(normalZ.get(s))));
                y2.add(-Math.log10(ZScores.zToP(robustZ.get(s))));
                x3.add(normalSE.get(s));
                y3.add(robustSE.get(s));
            }
            String ln = s + "\t" + normalZ.get(s) + "\t" + robustZ.get(s) + "\t"
                    + -Math.log10(ZScores.zToP(normalZ.get(s))) + "\t"
                    + -Math.log10(ZScores.zToP(robustZ.get(s))) + "\t" + normalSE.get(s) + "\t"
                    + robustSE.get(s);
            tf.writeln(ln);
        }
    }
    tf.close();
    System.out.println(x.size());
    double c = corr.correlation(Primitives.toPrimitiveArr(x.toArray(new Double[0])),
            Primitives.toPrimitiveArr(y.toArray(new Double[0])));
    double c2 = corr.correlation(Primitives.toPrimitiveArr(x2.toArray(new Double[0])),
            Primitives.toPrimitiveArr(y2.toArray(new Double[0])));

    double c3 = Correlation.correlate(Primitives.toPrimitiveArr(x.toArray(new Double[0])),
            Primitives.toPrimitiveArr(y.toArray(new Double[0])));
    double c4 = Correlation.correlate(Primitives.toPrimitiveArr(x2.toArray(new Double[0])),
            Primitives.toPrimitiveArr(y2.toArray(new Double[0])));

    double c5 = corr.correlation(Primitives.toPrimitiveArr(x3.toArray(new Double[0])),
            Primitives.toPrimitiveArr(y3.toArray(new Double[0])));
    double c6 = Correlation.correlate(Primitives.toPrimitiveArr(x3.toArray(new Double[0])),
            Primitives.toPrimitiveArr(y3.toArray(new Double[0])));

    System.out.println("Z Spearman: " + c + " Pearson: " + c3);
    System.out.println("P Spearman: " + c2 + " Pearson: " + c4);
    System.out.println("SE Spearman: " + c5 + " Pearson: " + c6);
}