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

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

Introduction

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

Prototype

public RealMatrix getCorrelationMatrix() 

Source Link

Document

Calculate the Spearman Rank Correlation Matrix.

Usage

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)));
        }//from  ww  w . j a va  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()));
        }

        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.meteoinfo.math.stats.StatsUtil.java

/**
 * Computes Spearman's rank correlation for pairs of arrays or columns of a matrix.
 *
 * @param x X data//from w w  w  .jav  a2 s .co m
 * @param y Y data
 * @return Spearman's rank correlation
 */
public static Array spearmanr(Array x, Array y) {
    int m = x.getShape()[0];
    int n = 1;
    if (x.getRank() == 2)
        n = x.getShape()[1];
    double[][] aa = new double[m][n * 2];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n * 2; j++) {
            if (j < n) {
                aa[i][j] = x.getDouble(i * n + j);
            } else {
                aa[i][j] = y.getDouble(i * n + j - n);
            }
        }
    }
    RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
    SpearmansCorrelation cov = new SpearmansCorrelation(matrix);
    RealMatrix mcov = cov.getCorrelationMatrix();
    m = mcov.getColumnDimension();
    n = mcov.getRowDimension();
    Array r = Array.factory(DataType.DOUBLE, new int[] { m, n });
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            r.setDouble(i * n + j, mcov.getEntry(i, j));
        }
    }

    return r;
}