## Covariance formula

Covariance states the relationship between two random variables. The formula is applied to get the variance between two variables. It states that till what range with the change in one variable changes together. If one variable is of a higher range other will also have a higher range and lower the one variable other would be of a lower range. Covariance is measured in units.

### Positive covariance-

When both the variables move in the same direction it is called positive covariance.

### Negative covariance-

When both the variables move in opposite directions it is said as negative covariance.

In the formula of covariance, the covariance between two variables X and Y can be denoted as Cov(x,y). The formula of covariance varies in some circumstances. Formulas for the covariance of population and sample are different.

## Covariance formula for population

Cov(x,y) = ∑(x_{i} – x ) × (y_{i} – y)/ (N)

## Covariance formula for sample

Cov(x,y) = ∑(x_{i} – x ) × (y_{i} – y)/ (N – 1)

Where, X_{i} –values of the X-variable

Y_{i} – values of the Y-variable

X̄ – mean of the X-variable

Ȳ – mean of the Y-variable

n – number of data points

The covariance formula can be also written as

ρ(X,Y)=Cov(X,Y)/σ_{X} * σ_{Y}

ρ(X,Y) – correlation between the variables X and Y

Cov(X,Y) – covariance between the variables X and Y

σ_{X} – standard deviation of the X-variable

σ_{Y} – standard deviation of the Y-variable

## Example

**1. Find covariance for following data x = {6,5,3,4,2}, y = {12,10,8,6,4}**

Given data, x = {6,5,3,4,2}, y = {12,10,8,6,4} & N = 5

X̄ = (6 + 5 + 3 + 4 + 2) / 5

= 20 / 5

= 4

Ȳ = (12 + 10 +8 + 6 + 4) / 5

= 40 / 5

= 8

Using the sample covariance formula

Cov(x,y) = ∑(x_{i} – x ) × (y_{i} – y)/ (N – 1)

= [(6 – 4)(12 – 8) + (5 – 4)(10 – 8) + (3 – 4)(8 – 8) + (4 – 4)(6 – 8) + (2 – 4)(4 – 8)] / 5 – 1

= 8 + 2 + 0 + 0 + 6 / 4

= 16 / 4

= 4

Using the Population covariance formula

Cov(x,y) = ∑(x_{i} – x ) × (y_{i} – y)/ (N)

= [(6 – 4)(12 – 8) + (5 – 4)(10 – 8) + (3 – 4)(8 – 8) + (4 – 4)(6 – 8) + (2 – 4)(4 – 8)] / 5

= 8 + 2 + 0 + 0 + 6 / 5

= 16 / 5

= 3.2

**2. The standard deviation of X is 15 and the standard deviation of Y is 20. The correlation between X and Y is 5. Find covariance of X and Y.**

Given data, σ_{X} =15, σ_{Y} =20 and ρ(X,Y) = 5

ρ(X,Y) = Cov(X,Y)/σ_{X} * σ_{Y}

5= Cov(X,Y)/ 15*20

Cov(X,Y)= 1500