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

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

Introduction

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

Prototype

public KendallsCorrelation() 

Source Link

Document

Create a KendallsCorrelation instance without data.

Usage

From source file:edu.umd.umiacs.clip.tools.math.CorrelationUtils.java

public static void main(String[] args) {
    double[] x = { 0.9, 0.6, 0.4, 0.3, 0.2, 0.1 };
    double[] y = { 0.5, 0.6, 0.4, 0.3, 0.2, 0.1 };
    System.out.println(new PearsonsCorrelation().correlation(x, y));
    System.out.println(new KendallsCorrelation().correlation(x, y));
    System.out.println(tauAP(x, y));
    System.out.println(tauGAP(x, y));
    System.out.println(pr(x, y));
}

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

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

From source file:Evaluator.StatCalculator.java

public double kendalTauCalc(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  w  w . ja  v a 2  s.co  m*/

    KendallsCorrelation kc = new KendallsCorrelation();
    return kc.correlation(xArray, yArray);

}

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

@Test
public void kendallsTao_givenAKartesianQuadrandIandIIISymmetraleParallelLine() {
    CorrelationValues c = newCorrelationValue3();
    KendallsCorrelation kCorrelation = new KendallsCorrelation();
    System.out.println("kendall's tao correlation t(x, y)=" + kCorrelation.correlation(c.x, c.y));
    assertEquals(c.kendallTaoB, kCorrelation.correlation(c.x, c.y), c.epsilon);
}

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

@Test
public void kendallsTao_givenAKartesianQuadrandIIandIVSymmetralePrallelLine() {
    CorrelationValues c = newCorrelationValue2();
    KendallsCorrelation kCorrelation = new KendallsCorrelation();
    System.out.println("kendall's tao correlation t(x, y)=" + kCorrelation.correlation(c.x, c.y));
    assertEquals(c.kendallTaoB, kCorrelation.correlation(c.x, c.y), c.epsilon);
}

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

@Test
public void kendallsTao_givenDefaultValues_expectCorrectResult() {
    CorrelationValues c = newCorrelationValue1();
    KendallsCorrelation kCorrelation = new KendallsCorrelation();
    System.out.println("kendall's tao correlation t(x, y)=" + kCorrelation.correlation(c.x, c.y));
    assertEquals(c.kendallTaoB, kCorrelation.correlation(c.x, c.y), c.epsilon);
}

From source file:org.apache.solr.client.solrj.io.eval.CorrelationEvaluator.java

@Override
public Object doWork(Object... values) throws IOException {

    if (values.length == 2) {
        Object first = values[0];
        Object second = values[1];

        if (null == first) {
            throw new IOException(
                    String.format(Locale.ROOT, "Invalid expression %s - null found for the first value",
                            toExpression(constructingFactory)));
        }// w ww . jav  a  2s  . c  o m
        if (null == second) {
            throw new IOException(
                    String.format(Locale.ROOT, "Invalid expression %s - null found for the second value",
                            toExpression(constructingFactory)));
        }
        if (!(first instanceof List<?>)) {
            throw new IOException(String.format(Locale.ROOT,
                    "Invalid expression %s - found type %s for the first value, expecting a list of numbers",
                    toExpression(constructingFactory), first.getClass().getSimpleName()));
        }
        if (!(second instanceof List<?>)) {
            throw new IOException(String.format(Locale.ROOT,
                    "Invalid expression %s - found type %s for the second value, expecting a list of numbers",
                    toExpression(constructingFactory), first.getClass().getSimpleName()));
        }

        if (type.equals(CorrelationType.pearsons)) {
            PearsonsCorrelation pearsonsCorrelation = new PearsonsCorrelation();
            return pearsonsCorrelation.correlation(
                    ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),
                    ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue())
                            .toArray());
        } else if (type.equals(CorrelationType.kendalls)) {
            KendallsCorrelation kendallsCorrelation = new KendallsCorrelation();
            return kendallsCorrelation.correlation(
                    ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),
                    ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue())
                            .toArray());

        } else if (type.equals(CorrelationType.spearmans)) {
            SpearmansCorrelation spearmansCorrelation = new SpearmansCorrelation();
            return spearmansCorrelation.correlation(
                    ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),
                    ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue())
                            .toArray());
        } else {
            return null;
        }
    } else if (values.length == 1) {
        if (values[0] instanceof Matrix) {
            Matrix matrix = (Matrix) values[0];
            double[][] data = matrix.getData();
            if (type.equals(CorrelationType.pearsons)) {
                PearsonsCorrelation pearsonsCorrelation = new PearsonsCorrelation(data);
                RealMatrix corrMatrix = pearsonsCorrelation.getCorrelationMatrix();
                double[][] corrMatrixData = corrMatrix.getData();
                Matrix realMatrix = new Matrix(corrMatrixData);
                realMatrix.addToContext("corr", pearsonsCorrelation);
                return realMatrix;
            } else if (type.equals(CorrelationType.kendalls)) {
                KendallsCorrelation kendallsCorrelation = new KendallsCorrelation(data);
                RealMatrix corrMatrix = kendallsCorrelation.getCorrelationMatrix();
                double[][] corrMatrixData = corrMatrix.getData();
                Matrix realMatrix = new Matrix(corrMatrixData);
                realMatrix.addToContext("corr", kendallsCorrelation);
                return realMatrix;
            } else if (type.equals(CorrelationType.spearmans)) {
                SpearmansCorrelation spearmansCorrelation = new SpearmansCorrelation(
                        new Array2DRowRealMatrix(data));
                RealMatrix corrMatrix = spearmansCorrelation.getCorrelationMatrix();
                double[][] corrMatrixData = corrMatrix.getData();
                Matrix realMatrix = new Matrix(corrMatrixData);
                realMatrix.addToContext("corr", spearmansCorrelation.getRankCorrelation());
                return realMatrix;
            } else {
                return null;
            }
        } else {
            throw new IOException(
                    "corr function operates on either two numeric arrays or a single matrix as parameters.");
        }
    } else {
        throw new IOException(
                "corr function operates on either two numeric arrays or a single matrix as parameters.");
    }
}

From source file:org.apache.solr.client.solrj.io.eval.KendallsCorrelationEvaluator.java

@Override
public Object doWork(Object first, Object second) throws IOException {
    if (null == first) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));
    }//from w w  w  .jav a 2  s .  c om
    if (null == second) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the second value", toExpression(constructingFactory)));
    }
    if (!(first instanceof List<?>)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the first value, expecting a list of numbers",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }
    if (!(second instanceof List<?>)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the second value, expecting a list of numbers",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }

    KendallsCorrelation kendallsCorrelation = new KendallsCorrelation();

    return kendallsCorrelation.correlation(
            ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),
            ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray());
}

From source file:org.meteoinfo.math.stats.StatsUtil.java

/**
 * Calculates Kendall's tau, a correlation measure for ordinal data.
 *
 * @param x X data/*w ww .j av a2s  .c o m*/
 * @param y Y data
 * @return Kendall's tau correlation.
 */
public static double kendalltau(Array x, Array y) {
    double[] xd = (double[]) ArrayUtil.copyToNDJavaArray(x);
    double[] yd = (double[]) ArrayUtil.copyToNDJavaArray(y);
    KendallsCorrelation kc = new KendallsCorrelation();
    double r = kc.correlation(xd, yd);
    return r;
}

From source file:tech.tablesaw.api.NumberColumnTest.java

@Test
public void testCorrelation() {
    double[] x = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    double[] y = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    DoubleColumn xCol = DoubleColumn.create("x", x);
    DoubleColumn yCol = DoubleColumn.create("y", y);

    double resultP = xCol.pearsons(yCol);
    double resultS = xCol.spearmans(yCol);
    double resultK = xCol.kendalls(yCol);
    assertEquals(new PearsonsCorrelation().correlation(x, y), resultP, 0.0001);
    assertEquals(new SpearmansCorrelation().correlation(x, y), resultS, 0.0001);
    assertEquals(new KendallsCorrelation().correlation(x, y), resultK, 0.0001);
}