Covariance Calculator
Calculate sample or population covariance and Pearson correlation from two paired data sets.
Shows means, standard deviations, and a scatter plot.
Covariance measures how two variables move together. Positive covariance means they tend to rise and fall together. Negative means one rises as the other falls. Zero means no linear relationship — though zero covariance does not imply independence (Y = X² has zero covariance around X = 0, but the two are completely tied).
Sample covariance (the form you almost always want for real data):
Cov(X, Y) = Σ (xᵢ − x̄)(yᵢ − ȳ) / (n − 1)
The n − 1 divisor (Bessel’s correction) makes the estimator unbiased when X and Y are drawn from a larger population. If your data IS the entire population, divide by n instead. NumPy’s numpy.cov() defaults to the sample form (ddof=1); pandas’ .cov() does the same; R’s cov() also samples. So when in doubt, use the sample form.
Why magnitude alone tells you nothing. Covariance is in the product of the units of X and Y. Height-and-weight covariance in (cm·kg) versus (m·kg) differ by a factor of 100, even though the actual relationship is the same. To get something scale-free, divide by both standard deviations: that’s the Pearson correlation coefficient r = Cov(X, Y) / (σₓ × σᵧ), which always sits in [−1, 1]. This calculator returns both.
Worked example. X = (1, 2, 3) and Y = (2, 4, 5). Means: x̄ = 2, ȳ ≈ 3.667. Sum of products: (−1)(−1.667) + (0)(0.333) + (1)(1.333) = 3.0. Divide by n−1 = 2: Cov = 1.5. Both std devs: σₓ = 1.0, σᵧ ≈ 1.528. Pearson r = 1.5 / (1.0 × 1.528) ≈ 0.982. Very strong positive linear relationship.
Real uses.
- Portfolio risk. The variance of a 2-asset portfolio is w₁²σ₁² + w₂²σ₂² + 2w₁w₂Cov(R₁, R₂). Diversification works because adding a low-covariance asset reduces total variance without proportionally reducing return.
- Linear regression. The slope of the best-fit line of Y on X equals Cov(X, Y) / Var(X). Regression is built directly on top of covariance.
- Principal Component Analysis (PCA). PCA finds the eigenvectors of the covariance matrix of your features. The directions of largest variance are the principal components.
- Kalman filters. The state covariance matrix is updated every step. The whole machinery rests on covariance and its propagation through linear operations.
Caveats.
- Outliers move covariance dramatically. One bad data point in 20 can flip the sign. Inspect a scatter plot before trusting the number.
- Covariance only catches linear association. If X and Y are tied nonlinearly (Y = sin(X), Y = X², a U-shape), covariance can be near zero while the relationship is strong. Plot the data; don’t just trust the number.
- Sample size matters. With n = 3, even a single coincidental outlier produces a misleading covariance. Sample sizes under 30 give noisy estimates; over 100 starts to converge to the true value.
Quick check. Enter the same series in both X and Y. Cov(X, X) equals Var(X), Pearson r equals 1.0. That’s the diagonal of any covariance matrix — variances on the diagonal, covariances off-diagonal.