covariance function
Computes the covariance between two series.
Implementation
double covariance(List<double> x, List<double> y) {
if (x.length != y.length) {
throw ArgumentError('x and y must have the same length');
}
if (x.length < 2) {
throw ArgumentError('At least 2 points are required');
}
final n = x.length;
double sumX = 0, sumY = 0;
for (int i = 0; i < n; i++) {
sumX += x[i];
sumY += y[i];
}
final meanX = sumX / n;
final meanY = sumY / n;
double cov = 0;
for (int i = 0; i < n; i++) {
cov += (x[i] - meanX) * (y[i] - meanY);
}
return cov / (n - 1); // Sample covariance
}