Calculating Pearson's correlation of two sets of data points
PearsonsCorrelation
computes correlations defined by the formula cor(X, Y) = sum[(xi - E(X))(yi - E(Y))] / [(n - 1)s(X)s(Y)]
, where E(X)
and E(Y)
are means of X
and Y
, and s(X)
and s(Y)
are their respective standard deviations.
How to do it...
Create a method that takes two
double
arrays that represent two sets of data points:public void calculatePearson(double[] x, double[] y){
Create a
PearsonsCorrelation
object:PearsonsCorrelation pCorrelation = new PearsonsCorrelation();
Compute correlation of the two sets of data points:
double cor = pCorrelation.correlation(x, y);
Use the correlation as per your requirements, and close the method:
System.out.println(cor); }
The complete code for the recipe is as follows:
import org.apache.commons.math3.stat.correlation.PearsonsCorrelation; public class PearsonTest { public static void main(String[] args...