how to compute covariance and correlation between two datasets: formulas, steps and examples
Covariance and correlation measure the relationship between two numerical variables. Covariance indicates direction (positive or negative co-movement) and scale, while correlation standardizes that relationship to a dimensionless value between -1 and 1. Use these metrics to understand associations in finance, science, engineering and everyday data analysis.
what are covariance and correlation and when to use each
Covariance: shows how two variables vary together. If larger values of X tend to occur with larger values of Y, covariance is positive; if larger X occur with smaller Y, covariance is negative. Its magnitude depends on the units of X and Y, so it is not directly comparable across datasets.
Correlation (Pearson correlation): a standardized measure of linear association. It rescales covariance by the product of the standard deviations of both variables, producing a value between -1 (perfect negative linear relationship) and +1 (perfect positive linear relationship). Use correlation when you need a unitless measure of linear association, and covariance when you want the raw co-variation (for example in portfolio variance calculations).
formulas: covariance and Pearson correlation
Sample covariance (between two samples X and Y of size n):
cov(X,Y) = (1 / (n - 1)) * Σ (xi - x̄)(yi - ȳ)
Population covariance (when you have the entire population):
Cov(X,Y) = (1 / n) * Σ (xi - μx)(yi - μy)
Pearson correlation coefficient (r):
r = cov(X,Y) / (sx * sy)
where sx and sy are the sample standard deviations of X and Y respectively (using denominator n-1). Using the sample covariance in the numerator and sample standard deviations in the denominator yields the sample Pearson correlation.
step-by-step: how to compute covariance manually
- Collect paired observations (xi, yi) for i = 1..n. Both lists must have the same length and correspond element-wise.
- Compute the sample means x̄ = (Σ xi)/n and ȳ = (Σ yi)/n.
- For each pair, compute the product of deviations: (xi - x̄)(yi - ȳ).
- Sum those products: Σ (xi - x̄)(yi - ȳ).
- Divide by (n - 1) to get the sample covariance: cov(X,Y) = [Σ (xi - x̄)(yi - ȳ)]/(n - 1).
worked numerical example — covariance
Data (n = 5):
- X: 2, 4, 6, 8, 10
- Y: 1, 3, 4, 7, 9
Step 1 — means: x̄ = (2+4+6+8+10)/5 = 6. ȳ = (1+3+4+7+9)/5 = 4.8.
Step 2 — deviations and products:
| i | xi | yi | (xi-x̄) | (yi-ȳ) | product |
|---|---|---|---|---|---|
| 1 | 2 | 1 | -4 | -3.8 | 15.2 |
| 2 | 4 | 3 | -2 | -1.8 | 3.6 |
| 3 | 6 | 4 | 0 | -0.8 | 0 |
| 4 | 8 | 7 | 2 | 2.2 | 4.4 |
| 5 | 10 | 9 | 4 | 4.2 | 16.8 |
Sum of products = 15.2 + 3.6 + 0 + 4.4 + 16.8 = 40. Dividing by (n-1)=4 gives sample covariance cov(X,Y) = 10.
step-by-step: how to compute Pearson correlation manually
- Compute sample covariance cov(X,Y) as above.
- Compute sample standard deviation for X: sx = sqrt(Σ (xi - x̄)^2 / (n - 1)).
- Compute sample standard deviation for Y: sy similarly.
- Compute r = cov(X,Y) / (sx * sy).
continued example — correlation
From the table, compute squared deviations:
| i | (xi-x̄)^2 | (yi-ȳ)^2 |
|---|---|---|
| 1 | 16 | 14.44 |
| 2 | 4 | 3.24 |
| 3 | 0 | 0.64 |
| 4 | 4 | 4.84 |
| 5 | 16 | 17.64 |
Σ(xi-x̄)^2 = 40 → sx = sqrt(40/4) = sqrt(10) ≈ 3.1623. Σ(yi-ȳ)^2 = 40.8 → sy = sqrt(40.8/4) = sqrt(10.2) ≈ 3.1937.
r = cov / (sx * sy) = 10 / (3.1623 * 3.1937) ≈ 10 / 10.099 ≈ 0.9902.
Interpretation: very strong positive linear relationship; as X increases, Y tends to increase almost proportionally in this sample.
how to compute covariance and correlation using a calculator or spreadsheet
Online calculators and spreadsheets automate the steps above. In most spreadsheet programs:
- Use =COVARIANCE.S(rangeX, rangeY) for sample covariance, or =COVARIANCE.P for population covariance.
- Use =CORREL(rangeX, rangeY) for Pearson correlation.
On calculatorr.com you can use the covariance and correlation calculators to paste paired lists, choose sample vs population mode, and get covariance, correlation, and a scatter plot. After computing, compare the numeric result to the sign and magnitude to interpret the relationship.
how to interpret results
- Covariance > 0: variables tend to increase together. Covariance < 0: one tends to increase while the other decreases. Magnitude depends on units—large value may or may not indicate a strong relationship.
- Correlation near +1: strong positive linear relationship. Near -1: strong negative linear relationship. Near 0: little or no linear relationship (nonlinear association may still exist).
- Correlation does not imply causation. A high correlation could be driven by a third variable or by outliers.
common mistakes and pitfalls
- Mixing population and sample formulas: be consistent. Use population formulas only when you truly have the entire population.
- Interpreting correlation beyond linear relationships: Pearson correlation detects linear association. For monotonic but nonlinear relationships, consider Spearman rank correlation.
- Ignoring outliers: a single outlier can dramatically change covariance and correlation. Visualize data with a scatter plot first.
- Using mismatched pairs: ensure observations are paired correctly (xi with yi). Misalignment produces meaningless results.
- Reporting covariance without units: remember covariance depends on units; reporting correlation is usually clearer for general audiences.
practical examples and applications
- Finance: covariance is used to compute portfolio variance (covariances between asset returns matter). Correlation helps to select assets with low correlation to diversify risk.
- Quality control: check if two process measurements move together (temperature and reaction yield).
- Research: quantify association between continuous measurements (e.g., blood pressure and sodium intake).
quick checklist before computing
- Ensure sample size is sufficient. Very small n yields unstable estimates.
- Plot the data to detect nonlinear patterns or outliers.
- Decide whether to use sample or population formulas.
- Consider transforming variables (log, square root) if distributions are skewed; transformations change covariance and correlation.
how to report your findings
Report the statistic, the method used (sample vs population), sample size, and a confidence interval when relevant. Example: "Sample Pearson correlation r = 0.99 (n = 5), indicating a very strong positive linear association; computed using sample covariance and sample standard deviations." Add a scatter plot with a fitted line to illustrate the relationship.
For step-by-step computation without manual arithmetic, try the covariance and correlation tools at Calculatorr to paste data, choose options and get instant results with visualizations.