依赖 lib:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-math3</artifactId> <version>3.1.1</version> </dependency>
两个数组就计算其相关系数:
PearsonsCorrelation t=new PearsonsCorrelation();
RealMatrix matrix=new Array2DRowRealMatrix(3,2);
RealVector vector=new ArrayRealVector(2);
vector.setEntry(0,1.0);
vector.setEntry(1,2.0);
RealVector vector1=new ArrayRealVector(2);
vector1.setEntry(0,20);
vector1.setEntry(1,10);
RealVector vector2=new ArrayRealVector(2);
vector2.setEntry(0,100);
vector2.setEntry(1,200);
matrix.setRowVector(0, vector);
matrix.setRowVector(1, vector1);
matrix.setRowVector(2, vector2);
double p=t.correlation(vector.toArray(),vector1.toArray());
System.out.println(p);