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

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

Introduction

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

Prototype

public SpearmansCorrelation() 

Source Link

Document

Create a SpearmansCorrelation without data.

Usage

From source file:com.medlog.webservice.util.MathUtl.java

public static void main(String[] args) {
    BigDecimal d;// ww w. ja  v a  2 s.c o  m
    d = new BigDecimal(0);
    double[] xs = { 30, 80, 90, 70, 70, 80, 50, 50, 50, 100, 10, 40, 80, 100 };

    double ys[] = { 52.9676423, 28.14739166, 45.22978518, 60.54520645, 58.43671379, 15.94587142, 58.869,
            57.8465, 58.686, 40.38026666, 55.8178, 60.3812, 88.1389, 61.1895 };

    double r = new PearsonsCorrelation().correlation(xs, ys);
    double rb = new SpearmansCorrelation().correlation(xs, ys);
    System.out.println("com.medlog.webservice.util.MathUtl.main()" + r * r);

    System.out.println("com.medlog.webservice.util.MathUtl.main()" + rb * rb);

}

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();
        }/* w  w w  .  j  av a2 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:com.insightml.math.statistics.Correlation.java

public Correlation(final double[] x, final double[] y) {
    arrays = new double[2][x.length];
    for (int i = 0; i < x.length; ++i) {
        arrays[0][i] = x[i];//from   w  ww  . java  2s  .co m
        arrays[1][i] = y[i];
    }
    final DoubleLinkedList x2 = new DoubleLinkedList();
    final DoubleLinkedList y2 = new DoubleLinkedList();
    for (int i = 0; i < x.length; ++i) {
        if (!Double.isNaN(x[i])) {
            x2.add(x[i]);
            y2.add(y[i]);
        }
    }
    final double[] x2arr = x2.toArray();
    final double[] y2arr = y2.toArray();
    covariance = new Covariance().covariance(x2arr, y2arr);
    pearson = new PearsonsCorrelation().correlation(x2arr, y2arr);
    spearman = new SpearmansCorrelation().correlation(x2arr, y2arr);
    mean = (Math.abs(pearson) + Math.abs(spearman)) / 2;
}

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;//from w ww  . j  a  v  a2s .  co  m
        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_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: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: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 ww w.ja  va 2 s.  com
            }
        }

        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:MSUmpire.MathPackage.PearsonCorr.java

public double CalcCorrV2(XYPointCollection CollectionA, XYPointCollection CollectionB, int NoPointPerInterval) {
    SpearmansCorrelation pearsonsCorrelation = new SpearmansCorrelation();

    int num = Math.max(CollectionA.PointCount(), CollectionB.PointCount()) / 2;
    float timeinterval = 2f / (float) NoPointPerInterval;
    if (num < 6) {
        return 0f;
    }/*from   ww w.j  ava  2s . co m*/

    double[] arrayA = new double[num];
    double[] arrayB = new double[num];

    float start = Math.max(CollectionA.Data.get(0).getX(), CollectionB.Data.get(0).getX());

    int i = 0;
    float low = start;
    float up = start + timeinterval;

    for (int j = 0; j < CollectionA.PointCount(); j++) {
        while (CollectionA.Data.get(j).getX() > up) {
            i++;
            low = up;
            up = low + timeinterval;
        }
        if (i >= num) {
            break;
        }
        if (CollectionA.Data.get(j).getX() >= low && CollectionA.Data.get(j).getX() < up) {
            if (CollectionA.Data.get(j).getY() > arrayA[i]) {
                arrayA[i] = CollectionA.Data.get(j).getY();
            }
        }
    }
    i = 0;
    low = start;
    up = start + timeinterval;
    for (int j = 0; j < CollectionB.PointCount(); j++) {
        while (CollectionB.Data.get(j).getX() > up) {
            i++;
            low = up;
            up = low + timeinterval;
        }
        if (i >= num) {
            break;
        }
        if (CollectionB.Data.get(j).getX() >= low && CollectionB.Data.get(j).getX() < up) {
            if (CollectionB.Data.get(j).getY() > arrayB[i]) {
                arrayB[i] = CollectionB.Data.get(j).getY();
            }
        }
    }

    if (arrayA[0] == 0f) {
        arrayA[0] = arrayA[1];
    }
    if (arrayB[0] == 0f) {
        arrayB[0] = arrayB[1];
    }
    for (int idx = 1; idx < num - 1; idx++) {
        if (arrayA[idx] == 0f) {
            arrayA[idx] = (arrayA[idx - 1] + arrayA[idx + 1]) / 2;
        }
        if (arrayB[idx] == 0f) {
            arrayB[idx] = (arrayB[idx - 1] + arrayB[idx + 1]) / 2;
        }
    }

    if (arrayA[num - 1] == 0f) {
        arrayA[num - 1] = arrayA[num - 2];
    }
    if (arrayB[num - 1] == 0f) {
        arrayB[num - 1] = arrayB[num - 2];
    }
    double R2 = pearsonsCorrelation.correlation(arrayA, arrayB);
    return R2;
}