List of usage examples for org.apache.commons.math3.stat.correlation KendallsCorrelation getCorrelationMatrix
public RealMatrix getCorrelationMatrix()
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 w w . j av a2s . 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."); } }