Calculating covariance of two sets of data points

Unbiased covariances are given by the formula cov(X, Y) = sum [(xi - E(X))(yi - E(Y))] / (n - 1), where E(X) is the mean of X and E(Y) is the mean of the Y values. Non-bias-corrected estimates use n in place of n - 1. To determine if the covariance is bias corrected or not, we need to set an additional, optional parameter called biasCorrected which is set to true by default.

How to do it...

  1. Create a method that takes two one-dimensional double arrays. Each array represents a set of data points:
            public void calculateCov(double[] x, double[] y){ 
    
  2. Calculate the covariance of the two sets of data points as follows:
            double covariance = new Covariance().covariance(x, y, false); 
    

    Note

    For this recipe, we have ...

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.