Covariance Calculator
Covariance measures how two variables vary together: positive when they rise together, negative when one rises as the other falls. For X = 1, 2, 3, 4, 5 and Y = 2, 4, 5, 4, 5 the sample covariance is 1.5 and the population covariance is 1.2.
Last updated · By S. Dilshan · Runs in your browser, nothing is uploaded
Step-by-Step Solution
What Is Covariance?
Covariance describes the direction of the linear relationship between two variables. If large values of X tend to go with large values of Y, the covariance is positive. If large values of X go with small values of Y, it is negative. If there is no consistent pattern, it is close to zero.
Covariance is the raw ingredient behind two of the most important tools in statistics. Divide it by the product of the two standard deviations and you get the correlation coefficient. Divide it by the variance of X and you get the slope of the regression line. In finance, the covariance of asset returns is the key input for measuring portfolio risk.
Reading the Sign
| Covariance | Meaning | Example |
|---|---|---|
| Positive | The variables tend to move in the same direction | Hours studied and exam score |
| Negative | The variables tend to move in opposite directions | Outside temperature and heating cost |
| Near zero | No consistent linear relationship | Shoe size and exam score |
Covariance Formula
xᵢandyᵢare the paired observations.x̄andȳ(or μₓ and μᵧ) are the means of X and Y.- Each term multiplies how far X is from its mean by how far Y is from its mean. Two values on the same side of their means give a positive product, and opposite sides give a negative one.
- The sample version divides by n − 1, the population version by N.
Covariance of a variable with itself is its variance. Unlike correlation, covariance depends on the units, so its size cannot be compared across different pairs of variables.
Worked Example: Covariance of X and Y
X = 1, 2, 3, 4, 5 and Y = 2, 4, 5, 4, 5, so n = 5.
| x | y | x − 3 | y − 4 | Product |
|---|---|---|---|---|
| 1 | 2 | −2 | −2 | 4 |
| 2 | 4 | −1 | 0 | 0 |
| 3 | 5 | 0 | 1 | 0 |
| 4 | 4 | 1 | 0 | 0 |
| 5 | 5 | 2 | 1 | 2 |
| Sum | 6 |
- The means are x̄ = 3 and ȳ = 4.
- The sum of the products is 6.
- Sample covariance: 6 ÷ 4 = 1.5.
- Population covariance: 6 ÷ 5 = 1.2.
- Correlation: the sample standard deviations are 1.5811 for X and 1.2247 for Y, so r = 1.5 ÷ (1.5811 × 1.2247) = 0.7746.
The positive covariance confirms that Y tends to rise with X, and the correlation of 0.77 tells you the relationship is fairly strong.
How to Use This Covariance Calculator
- Enter your X values in the first box and your Y values in the second, in matching order.
- Check that both lists contain the same number of values (at least three).
- Choose Sample (n − 1) or Population (n).
- Read the covariance, the correlation coefficient, both means and the number of pairs. The steps show every product.
Real-World Uses of Covariance
- Portfolio theory: the covariance matrix of asset returns drives diversification and risk calculations.
- Regression and machine learning: slopes, principal component analysis and many multivariate methods are built from covariances.
- Economics: measuring how variables such as income and spending move together.
- Engineering: filtering and estimating signals, as in Kalman filters.
- Biology: studying how traits co-vary within a population.
Covariance in a Two-Asset Portfolio
The variance of a portfolio depends on covariance: Var(A + B) = Var(A) + Var(B) + 2 × Cov(A, B). Suppose two assets each have variance 0.04 (a 20% standard deviation) and a covariance of 0.01. An equal-weighted portfolio has variance 0.25 × 0.04 + 0.25 × 0.04 + 2 × 0.25 × 0.01 = 0.025, a standard deviation of about 15.8%. If the assets moved in lockstep, with covariance 0.04, the variance would be 0.04 and the risk 20%. The lower the covariance, the greater the diversification benefit.
What Are the Most Common Mistakes With the Covariance Calculator?
- Comparing covariances across units. Because covariance has the units of X times Y, a larger number does not mean a stronger relationship. Use correlation for that.
- Misreading zero. Zero covariance means no linear relationship, not independence. A U-shaped pattern can have zero covariance.
- Mismatched pairs. The order of X and Y must line up observation by observation.
- Using the wrong divisor. Use n − 1 for a sample, n for a population.
- Outliers. One extreme pair can dominate the sum of products.
- Assuming causation. Variables can co-vary because of a third factor.
Where Can You Verify These Formulas?
The definitions and formulas on this page follow standard references. Check them against these sources:
Built and maintained by S. Dilshan. Found a mistake? Email [email protected] with the page address.
Frequently Asked Questions
What is covariance in statistics?
It is a measure of how two variables change together. Positive values mean they move in the same direction, negative values in opposite directions.
What is the covariance formula?
Sample covariance = Σ(x − x̄)(y − ȳ) ÷ (n − 1). For a population, divide by N instead.
What is the difference between covariance and correlation?
Correlation is covariance divided by the product of the standard deviations. It is unit-free and always between −1 and 1, which makes it easier to interpret.
Can covariance be negative?
Yes. A negative covariance means that when one variable is above its mean, the other tends to be below its mean.
What does a covariance of zero mean?
It means there is no linear relationship between the variables. They may still be related in a nonlinear way.
When should I use population instead of sample covariance?
Use population covariance when your data includes every member of the group. Use sample covariance when the data is a sample used to estimate a larger population.