Calculate the covariance between two features or variables in a dataset.
Covariance measures how two variables change together: cov(X,Y) = Σ(x − mean_x)(y − mean_y) / (n−1) for the sample covariance. A positive covariance means the variables tend to increase together; a negative covariance means one tends to increase as the other decreases; a covariance near zero suggests no linear relationship. Unlike correlation, covariance's magnitude depends on the scale of the input variables, which is why it's often normalized into the unitless correlation coefficient for interpretability across different feature scales.
cov(X, Y) = sum((x_i - mean_x)(y_i - mean_y)) / (n - 1)
Covariance's magnitude depends on the units and scale of both variables — a covariance of 1000 could indicate a strong or weak relationship depending on whether the variables are measured in single units or thousands, which is why correlation (a normalized version) is usually preferred for interpretation.
It means there is no linear relationship between the two variables on average — but note this doesn't rule out a strong nonlinear relationship, since covariance only captures linear co-movement.
The covariance matrix (pairwise covariances across all features) underlies techniques like PCA (Principal Component Analysis), Gaussian distributions in probabilistic models, and portfolio-style risk analysis of feature interactions.
Covariance is computed from paired observations — each x_i must correspond to the same observation as y_i — so mismatched lengths mean the pairing is undefined and the calculation cannot proceed.