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...

  1. Create a method that takes two double arrays that represent two sets of data points:
            public void calculatePearson(double[] x, double[] y){ 
    
  2. Create a PearsonsCorrelation object:
            PearsonsCorrelation pCorrelation = new PearsonsCorrelation(); 
    
  3. Compute correlation of the two sets of data points:
            double cor = pCorrelation.correlation(x, y); 
    
  4. Use the correlation as per your requirements, and close the method:
        System.out.println(cor); 
        } 

The complete code for ...

Get Java Data Science Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.