* @throws MathIllegalArgumentException if the arrays lengths do not match or
* there is insufficient data
*/
public double covariance(final double[] xArray, final double[] yArray, boolean biasCorrected)
throws MathIllegalArgumentException {
Mean mean = new Mean();
double result = 0d;
int length = xArray.length;
if (length != yArray.length) {
throw new MathIllegalArgumentException(
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, length, yArray.length);
} else if (length < 2) {
throw new MathIllegalArgumentException(
LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, length, 2);
} else {
double xMean = mean.evaluate(xArray);
double yMean = mean.evaluate(yArray);
for (int i = 0; i < length; i++) {
double xDev = xArray[i] - xMean;
double yDev = yArray[i] - yMean;
result += (xDev * yDev - result) / (i + 1);
}