# Covariance Calculator

**
Instructions:
**
Use this Covariance Calculator to find the covariance coefficient between two variables \(X\) and \(Y\) that you provide.
Please input the sample data for the independent variable \((X_i)\) and the dependent variable (\(Y_i\)), in the form below:

## How to use this Covariance Calculator

The use of this calculator is simple: You need to input the sample data for the variables \(X\) and \(Y\), and press the "Calculate" button. The calculator will show you all the steps required to compute the covariance coefficient.

### How do you compute the sample covariance

In order to compute the covariance, first, we need to have two samples of the same size: \(X_1, X_2, ...., X_n\) and \(Y_1, Y_2, ...., Y_n\). Then, using this information about the samples, you use the following formula:

\[ cov(X, Y) = \displaystyle \frac{1}{n-1}\left(\sum_{i=1}^n X_i Y_i - \left( \sum_{i=1}^n X_i \right) \times \left( \sum_{i=1}^n Y_i \right) \right) \]Usually, this is computed by constructing a table with \(X_i\) and \(Y_i\) values, but also with the products \(X_i Y_i\) in a column.:

### Alternative formulas to compute the sample covariance

Often times, you will see a different formula for sample covariance shown as:

\[ cov(X, Y) = \displaystyle \frac{1}{n-1}\left(\sum_{i=1}^n (X_i - \bar X)(Y_i - \bar Y) \right)\]This formula is absolutely equivalent to the previous ones, and it is a matter of taste whether you use this or the other one.

Some people think that the latter formula is better because it shows the covariance as this product of deviations from the mean. But other people think that the latter is inefficient, because it is forced to compute the sample means, which are not required in the former one.

### Applications of the Covariance

The covariance is an interesting measure that gets overshadowed by the correlation in basic statistics, although that is not the case for advanced statistics.

The following are direct uses of the covariance:

- In basic statistics, as a non-normalized measure of association between two variables.
- In multivariate statistics, where the covariance matrix plays a crucial role.
- In finance, as a measure of relative risk with respect to the market, via the calculation of a firm's beta.

### Are the covariance and correlation related in any way?

Yes, they are. Both the covariance and the correlation coefficient measure the degree of linear association between two variables.

The main difference is that the correlation measures the association relative to the standard deviations, which makes the correlation coefficient range between -1 and 1, which makes a MUCH more interpretable measure of association than the covariance itself

Still, the covariance coefficient, even if it is less interpretable, has its uses in finance, especially in the calculation of the beta for a company.

### Covariance Calculator continuous case

Notice that the case above corresponds to the sample correlation. When you know the distribution of the X and Y variables, with continuous distributions, as well as their joint distribution, you can compute the exact covariance using the expression:

\[cov(X, Y) = E(XY) - E(X)E(Y)\]### Example: Covariance Calculation for two samples

**Question**: Consider two variables X and Y, for which you have the following sample data:

Obs. | \(X\) | \(Y\) |

1 | 2 | 1 |

2 | 3 | 2 |

3 | 3 | 3 |

4 | 2 | 4 |

5 | 3 | 5 |

6 | 4 | 3 |

7 | 5 | 4 |

8 | 6 | 5 |

Solution:

We need to compute the covariance, which is computed by first computing cross products of the sample data.

Now, with the provided sample data, we need to construct the following table, which will be used for the calculation of the covariance coefficient:

Obs. | \(X\) | \(Y\) | \(X_i \cdot Y_i\) |

1 | 2 | 1 | 2 |

2 | 3 | 2 | 6 |

3 | 3 | 3 | 9 |

4 | 2 | 4 | 8 |

5 | 3 | 5 | 15 |

6 | 4 | 3 | 12 |

7 | 5 | 4 | 20 |

8 | 6 | 5 | 30 |

Sum = | 28 | 27 | 102 |

Based on the table above, we compute the following sum of cross-products that will be used in the calculation of the covariance:

\[ \begin{array}{ccl} SS_{XY} & = & \displaystyle \sum_{i=1}^n X_i Y_i - \frac{1}{n}\left(\sum_{i=1}^n X_i\right)\left(\sum_{i=1}^n Y_i\right) \\\\ \\\\ & = & \displaystyle 102 - \frac{1}{8} \times 756 \\\\ \\\\ & = & \displaystyle 7.5 \end{array}\]Now, the covariance between \(X\) and \(Y\) is computed using the following expression:

\[ \begin{array}{ccl} cov(X, Y) & = & \displaystyle \frac{SS_{XY}}{n-1} \\\\ \\\\ & = & \displaystyle \frac{7.5}{8 -1} \\\\ \\\\ & = & \displaystyle 1.071 \end{array}\]Therefore, based on the sample data provided, it is found that the sample covariance coefficient is \(cov(X, Y) = 1.071\).