The covariance of two random variables measures their connectedness; i.e., whether they tend to change with each other. If X and Y are two random variables, then the covariance is the expected value of (X−X)(Y−Ȳ), where X and Ȳ are the means of X and Y, respectively. You can calculate covariances with the covariance command.
If X and Y are given by lists of the same size, then covariance(X,Y) will return their covariance. For example, if you enter
then you will get
Alternatively, you could use a matrix with two columns instead of two lists to enter X and Y; the command
is another way to enter the above calculation.
If the entries in the lists X=[a0,…,an−1] and Y=[b0,…,bn−1] have different weights, say aj and bj have weight wj, then covariance can be given a third list W=[w0,…,wn−1] (or alternatively, you could use a matrix with three columns). For example, if you enter
then you will get
If each pair of entries in the lists X=[a0,…,am−1] and Y=[b0,…,b0] have different weights, say aj and bk have weight wjk, then covariance can be given a third argument of an m× n matrix W=(wjk). (Note that in this case the lists X and Y don’t have to be the same length.) For example, the covariance computed above could also have been computed by entering
which would of course return
In this case, to make it simpler to enter the data in a spreadsheet, the lists X and Y and the matrix W can be combined into a single matrix, by augmenting W with the list Y on the top and the transpose of the list X on the left, with a filler in the upper left hand corner;
|
When you use this method, you need to give covariance a second argument of -1. The above covariance can then be computed with the command
The linear correlation coefficient of two random variables is another way to measure their connectedness. Given random variables X and Y, their correlation is defined as cov(X,Y)/(σ(X)σ(Y)), where σ(X) and σ(Y) are the standard deviations of X and Y, respectively. The correlation can be computed with the correlation command, which takes the same types of arguments as the covariance command. If you enter
you will get
The covariance_correlation command will compute both the covariance and correlation simultaneously, and return a list with both values. This command takes the same type of arguments as the covariance and correlation commands. For example, if you enter
you will get