### 20.2.1 Covariance and correlation

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. The covariance command calculates covariances.

- covariance takes two mandatory and one optional argument:
  - X and Y, two lists.
  - Optionally, W, a list of weights or a matrix (wjk) where wjk is the weight of the pair (xj,yk).
  If the arguments are all lists, then can be entered as the columns of a single matrix.

  If the arguments consist of two lists and a matrix, to make it simpler to enter 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:

  ⎡
  ⎢
  ⎣

  “XY” | Y
  XT | W

  ⎤
  ⎥
  ⎦

  For this, you have to give covariance a second argument of -1.

- covariance(X,Y⟨,W⟩) returns the covariance of X and Y.

#### Examples

covariance([1,2,3,4],[1,4,9,16])

or:

covariance([[1,1],[2,4],[3,9],[4,16]])

25
4

covariance([1,2,3,4],[1,4,9,16],[3,1,5,2])

or:

covariance([1,2,3,4],[1,4,9,16],[[3,0,0,0],[0,1,0,0],[0,0,5,0],[0,0,0,2]])

or:

covariance([["XY",1,4,9,16],[1,3,0,5,0],[2,0,1,0,0],[3,0,0,5,0],[4,0,0,0,2]],-1)

662
121

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)), cov(X,Y) is the covariance of X and Y, and σ(X) and σ(Y) are the standard deviations of X and Y, respectively.

The correlation command finds the correlation of two lists and take the same types of arguments as the covariance command.

- correlation takes two mandatory and one optional argument:
  - X and Y, two lists.
  - Optionally, W, a list of weights or a matrix (wjk) where wjk is the weight of the pair (xj,yk).
  If the arguments are all lists, then can be entered as the columns of a single matrix.

  If the arguments consist of two lists and a matrix, to make it simpler to enter 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:

  ⎡
  ⎢
  ⎣

  "XY" | Y
  XT | W

  ⎤
  ⎥
  ⎦

  For this, you have to give correlation a second argument of -1.

- correlation(X,Y⟨,W⟩) returns the correlation of X and Y.

#### Example

correlation([1,2,3,4],[1,4,9,16])

100

4

√

645

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.

- covariance_correlation takes two mandatory and one optional argument:
  - X and Y, two lists.
  - Optionally, W, a list of weights or a matrix (wjk) where wjk is the weight of the pair (xj,yk).
  If the arguments are all lists, then can be entered as the columns of a single matrix.

  If the arguments consist of two lists and a matrix, to make it simpler to enter 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:

  ⎡
  ⎢
  ⎣

  "XY" | Y
  XT | W

  ⎤
  ⎥
  ⎦

  For this, you have to give covariance_correlation a second argument of -1.

- covariance_correlation(X,Y⟨,W⟩) returns a list consisting of the covariance and the correlation of X and Y.

#### Example

covariance_correlation([1,2,3,4],[1,4,9,16])

⎡
⎢
⎢
⎢
⎢
⎣

25
4

,

100

4

√

645

⎤
⎥
⎥
⎥
⎥
⎦