Previous Up Next

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 (XX)(Y−Ȳ), where X and Ȳ are the means of X and Y, respectively. The covariance command calculates covariances.

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.

Example

correlation([1,2,3,4],[1,4,9,16])
     
100
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.

Example

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





25
4
,
100
645





          

Previous Up Next