Example usage for org.apache.commons.math3.stat.correlation PearsonsCorrelation computeCorrelationMatrix

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

Introduction

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

Prototype

public RealMatrix computeCorrelationMatrix(double[][] data) 

Source Link

Document

Computes the correlation matrix for the columns of the input rectangular array.

Usage

From source file:com.mothsoft.alexis.engine.numeric.CorrelationCalculatorImpl.java

@Override
public double[][] correlate(List<DataSet> dataSets, Timestamp startDate, Timestamp endDate,
        TimeUnits granularity) {/*  ww w  .jav  a 2  s  .c  o m*/
    if (dataSets == null || dataSets.size() < 2) {
        throw new IllegalArgumentException("At least 2 data sets are required for correlation");
    }

    final Map<Long, List<Double>> orderedPoints = new TreeMap<Long, List<Double>>();

    for (int i = 0; i < dataSets.size(); i++) {
        final DataSet dataSet = dataSets.get(i);
        final List<DataSetPoint> points = this.dao.findAndAggregatePointsGroupedByUnit(dataSet, startDate,
                endDate, granularity);
        for (final DataSetPoint point : points) {
            final Long millis = point.getX().getTime();
            if (!orderedPoints.containsKey(millis)) {
                orderedPoints.put(millis, newDoubleList(dataSets.size()));
            }
            orderedPoints.get(millis).set(i, point.getY());
        }
    }

    if (orderedPoints.size() <= 1) {
        throw new IllegalArgumentException("Needed at least 2 points, found: " + orderedPoints.size());
    }

    final double[][] points = new double[orderedPoints.size()][dataSets.size()];

    int i = 0;
    for (final Map.Entry<Long, List<Double>> entry : orderedPoints.entrySet()) {
        final List<Double> values = entry.getValue();
        for (int j = 0; j < values.size(); j++) {
            points[i][j] = values.get(j);
        }
        i++;
    }

    final PearsonsCorrelation correlation = new PearsonsCorrelation();
    final RealMatrix matrix = correlation.computeCorrelationMatrix(points);
    return matrix.getData();
}

From source file:com.mothsoft.alexis.engine.numeric.CorrelationCalculatorTest.java

@Test
public void testCommonsMathCorrelationIdentityMatrix() {
    final PearsonsCorrelation correlation = new PearsonsCorrelation();

    final double[][] xy = new double[4][2];
    xy[0][0] = 0.0;/*www . j  ava  2  s  .  co m*/
    xy[1][0] = 1.0;
    xy[2][0] = 2.3;
    xy[3][0] = 3.4;

    xy[0][1] = 0.0;
    xy[1][1] = 1.0;
    xy[2][1] = 2.3;
    xy[3][1] = 3.4;

    final RealMatrix matrix = correlation.computeCorrelationMatrix(xy);
    assertEquals(2, matrix.getColumnDimension());
    assertEquals(2, matrix.getRowDimension());
    assertEquals(1.0, matrix.getEntry(0, 1), 0.000001);
    assertEquals(1.0, matrix.getEntry(1, 0), 0.000001);
}