Covariance Calculator
Find sample and population covariance for any two-variable dataset, with a step-by-step deviation table.
📊 What is Covariance?
Covariance is a statistical measure that quantifies how two random variables change together. When covariance is positive, the two variables tend to move in the same direction: higher-than-average values of X are paired with higher-than-average values of Y. When covariance is negative, they move in opposite directions: above-average X values tend to coincide with below-average Y values. A covariance of zero indicates no linear relationship between the variables.
Covariance shows up in a wide range of practical applications. In finance, portfolio managers use the covariance between asset returns to quantify diversification benefits: two stocks with negative covariance tend to offset each other's losses. In machine learning, principal component analysis (PCA) relies on the covariance matrix of features to find the directions of maximum variance. In biology and medicine, covariance between physiological measurements (such as body weight and blood pressure) reveals how health markers are interconnected.
A common point of confusion is the difference between covariance and correlation. Covariance retains the units of the original variables (if X is in centimetres and Y is in kilograms, covariance is in cm x kg), which makes its raw value hard to interpret or compare across different datasets. The Pearson correlation coefficient r is simply the covariance divided by the product of the two standard deviations, which scales the result to a unitless number between -1 and +1. Use covariance when you need the raw joint variability for further calculations (such as portfolio variance), and use correlation when you want an interpretable measure of linear association strength.
This calculator handles both the most common use case (raw paired data) and the textbook case (pre-computed summary statistics). It returns both sample covariance (using the n-1 denominator for unbiased estimation) and population covariance (using n), so you can choose whichever is appropriate for your analysis. The deviation product table in Raw Data mode shows every step of the calculation, making this tool useful for checking homework as well as verifying research results.
📐 Formula
📖 How to Use This Calculator
Steps
💡 Example Calculations
Example 1 - Study Hours vs Exam Score
Five students: hours studied (X) and exam score (Y)
Example 2 - Temperature vs Ice Cream Sales (Negative Relationship)
Coffee sales (Y) vs temperature Celsius (X): higher temperature, lower hot coffee demand
Example 3 - Summary Stats Mode (Textbook Problem)
Given: n = 6, SumX = 42, SumY = 54, SumXY = 396
❓ Frequently Asked Questions
🔗 Related Calculators
What is covariance and what does it measure?
Covariance measures how two variables change together. A positive covariance means that when X is above its mean, Y tends to also be above its mean. A negative covariance means they move in opposite directions. A covariance near zero suggests the variables are linearly unrelated. Unlike the correlation coefficient, covariance is not scaled, so its magnitude depends on the measurement units of X and Y.
What is the formula for sample covariance?
Sample covariance is Cov(X,Y) = Sum((xi - x-mean)(yi - y-mean)) / (n-1). The n-1 denominator (Bessel correction) makes the estimator unbiased for the true population covariance when working with a sample. The computational equivalent is Cov(X,Y) = (n*SumXY - SumX*SumY) / (n*(n-1)), which avoids computing the means first and is numerically more stable for large datasets.
What is the difference between sample covariance and population covariance?
Population covariance divides the sum of cross-deviations by n (the total count), while sample covariance divides by n-1. When you have data for the entire population, use n. When your data is a sample drawn from a larger population (the typical case in practice), use n-1 to get an unbiased estimate of the true population covariance. For large n the difference is small, but for n < 30 it matters.
How is covariance related to the correlation coefficient?
The Pearson correlation coefficient r equals the covariance divided by the product of the two standard deviations: r = Cov(X,Y) / (Sx * Sy). This scaling removes the effect of measurement units and bounds r between -1 and +1. A covariance of 50 between height (cm) and weight (kg) says very little on its own. Dividing by the SDs gives r, which is directly interpretable as the strength of the linear relationship.
What does it mean if covariance is zero?
Zero covariance means there is no linear relationship between X and Y in your data. However, it does not mean the variables are independent. Two variables can have zero covariance yet have a strong non-linear relationship (for example, a perfect U-shape where Y = X squared centered at the mean gives zero covariance with X). Always pair a covariance analysis with a scatter plot.
Can covariance be negative?
Yes. A negative covariance means that when X is above its mean, Y tends to be below its mean, and vice versa. For example, hours spent watching TV and GPA might have a negative covariance: students who watch more TV tend to have lower grades. The sign is the key piece of information covariance adds that variance alone cannot provide.
What is the unit of covariance?
Covariance has units equal to the product of the units of X and Y. If X is measured in centimetres and Y in kilograms, covariance is in cm*kg. This makes covariance values difficult to compare across different variable pairs or different unit systems, which is one reason the dimensionless correlation coefficient r is preferred for communication.
How many data points do I need for a reliable covariance estimate?
As a practical guideline, at least n = 10 pairs gives a rough estimate, and n >= 30 gives a reasonably stable one. With very small samples (n = 3 to 5), the sample covariance is highly sensitive to individual points and can swing dramatically with the addition or removal of one observation. Report n alongside the covariance so readers can judge reliability.
What is a covariance matrix?
A covariance matrix (also called the variance-covariance matrix) is a square matrix where each entry (i, j) is the covariance between variable i and variable j. The diagonal entries are the variances (covariance of each variable with itself). The off-diagonal entries are the pairwise covariances. Covariance matrices are the foundation of principal component analysis (PCA), multivariate regression, and portfolio theory in finance.
How is covariance used in portfolio theory?
In Markowitz portfolio theory, the variance of a two-asset portfolio is: Var(P) = w1^2 * Var(X) + w2^2 * Var(Y) + 2*w1*w2*Cov(X,Y), where w1 and w2 are the portfolio weights. A negative covariance between two assets reduces overall portfolio variance, which is the mathematical basis for diversification. Investors combine assets with low or negative covariance to reduce risk without proportionally reducing expected return.
How do I use the Summary Stats mode?
The Summary Stats mode lets you compute covariance when you have n (count), SumX (sum of all X values), SumY (sum of all Y values), and SumXY (sum of all xi*yi products) from a textbook problem or published table. Enter these four values and click Calculate. This is useful for homework problems that provide pre-computed summaries or when working from a published study that reports only aggregate statistics.
Is covariance symmetric?
Yes. Cov(X, Y) always equals Cov(Y, X). The formula is symmetric: Sum((xi - x-mean)(yi - y-mean)) = Sum((yi - y-mean)(xi - x-mean)). Swapping the roles of X and Y gives the same number. This also means that in a covariance matrix, the matrix is symmetric about its main diagonal.