Previous Up Next

9.2.1  Covariance and correlation: covariance correlation covariance_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.

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

  • Examples.

    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)


    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(X,Y ⟨, W) returns the correlation of X and Y.

  • Example.
    Input:

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

    Output:

    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.
    Input:

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

    Output:






    25
    4
    ,
    100
    645






    Previous Up Next