How to compute covariance and correlation between two datasets: formulas, steps and examples

Math

How to compute covariance and correlation between two datasets: formulas, steps and examples

Published: September 17, 2026
Updated: September 17, 2026

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

  1. Collect paired observations (xi, yi) for i = 1..n. Both lists must have the same length and correspond element-wise.
  2. Compute the sample means x̄ = (Σ xi)/n and ȳ = (Σ yi)/n.
  3. For each pair, compute the product of deviations: (xi - x̄)(yi - ȳ).
  4. Sum those products: Σ (xi - x̄)(yi - ȳ).
  5. 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:

ixiyi(xi-x̄)(yi-ȳ)product
121-4-3.815.2
243-2-1.83.6
3640-0.80
48722.24.4
510944.216.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

  1. Compute sample covariance cov(X,Y) as above.
  2. Compute sample standard deviation for X: sx = sqrt(Σ (xi - x̄)^2 / (n - 1)).
  3. Compute sample standard deviation for Y: sy similarly.
  4. Compute r = cov(X,Y) / (sx * sy).

continued example — correlation

From the table, compute squared deviations:

i(xi-x̄)^2(yi-ȳ)^2
11614.44
243.24
300.64
444.84
51617.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.

Why Choose Calculatorr.com?

We're dedicated to providing the most accurate, easy-to-use calculators for all your needs.

100% Free

All of our calculators are completely free to use, no hidden fees or subscriptions.

Private & Secure

Your data never leaves your browser. We don't store any of your calculations.

Mobile Friendly

Use our calculators on any device - desktop, tablet, or smartphone.