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...
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){
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 used non-bias-corrected covariance, and therefore, we have used three parameters in the
covariace()
method. To use unbiased covariance between twodouble
arrays, remove the third parameter,double covariance = new Covariance...