Create a Correlation Matrix in Julia

Create a Correlation Matrix in Julia

  • Julia
  • 3 mins read

A correlation matrix is a matrix that shows the correlation between different variables. To create a correlation matrix in Julia, you can use the cor() function from the StatsBase package. Here is an example of how to use this function:

Create a Correlation Matrix in Julia Example

To use the cor() function, you provide a matrix of data as input, and the function will calculate the correlation between each pair of variables (columns) in the matrix. The output will be a new matrix with the same dimensions as the input, showing the correlation between each pair of variables.

using StatsBase

# create a matrix of random numbers
X = rand(10,5)
10×5 Matrix{Float64}:
 0.266003    0.941806  0.466579   0.132464   0.00723692
 0.14576     0.5567    0.201634   0.672711   0.706726
 0.88164     0.35485   0.893702   0.0474657  0.367922
 0.527521    0.446506  0.610684   0.35055    0.7055
 0.579598    0.707715  0.25792    0.251061   0.124533
 0.361571    0.18881   0.7496     0.590208   0.0299176
 0.00436752  0.673661  0.867111   0.517097   0.788455
 0.165947    0.216833  0.877111   0.904612   0.863395
 0.176214    0.539258  0.60794    0.273418   0.337017
 0.152839    0.829886  0.0428891  0.0862019  0.580752

# calculate the correlation matrix
cor(X)
5×5 Matrix{Float64}:
  1.0       -0.268567   0.176412  -0.469869  -0.38045
 -0.268567   1.0       -0.637051  -0.572848  -0.193162
  0.176412  -0.637051   1.0        0.309968   0.130486
 -0.469869  -0.572848   0.309968   1.0        0.48544
 -0.38045   -0.193162   0.130486   0.48544    1.0

This will calculate the correlation between each pair of columns in the matrix X, and return the resulting correlation matrix.

The diagonal elements of the matrix (the elements on the main diagonal from the top left to the bottom right) will always be equal to 1.0, because each variable is perfectly correlated with itself. The other elements of the matrix will show the correlation between each pair of variables.

Here are a few more examples of using the cor() function to calculate a correlation matrix in Julia:

# example 1: create a matrix of random numbers and calculate the correlation matrix
X = rand(10,5)
10×5 Matrix{Float64}:
 0.74043   0.467064   0.270146   0.0517008  0.770775
 0.893883  0.607247   0.725127   0.405361   0.00625644
 0.554839  0.795495   0.791016   0.522617   0.787623
 0.945697  0.0963479  0.318941   0.268879   0.29345
 0.735988  0.240981   0.0945101  0.861974   0.519164
 0.51626   0.218447   0.320476   0.47964    0.686641
 0.208114  0.76552    0.709114   0.690301   0.98065
 0.22616   0.124498   0.469696   0.0938146  0.249991
 0.235817  0.341411   0.822801   0.188022   0.808245
 0.763764  0.119712   0.88768    0.205909   0.648473
cor(X)
5×5 Matrix{Float64}:
  1.0          -0.190356  -0.284188  -0.000127202  -0.48902
 -0.190356      1.0        0.395275   0.362783      0.352048
 -0.284188      0.395275   1.0       -0.188636      0.184532
 -0.000127202   0.362783  -0.188636   1.0           0.177631
 -0.48902       0.352048   0.184532   0.177631      1.0

# example 2: create a matrix of random numbers and calculate the correlation matrix,
# but only for the first three columns of the matrix
X = rand(10,5)
cor(X[:,1:3])
3×3 Matrix{Float64}:
  1.0       -0.042811   0.20584
 -0.042811   1.0       -0.063041
  0.20584   -0.063041   1.0
# example 3: create a matrix of random numbers and calculate the correlation matrix,
# but only for the last two columns of the matrix
X = rand(10,5)
cor(X[:,4:5])
2×2 Matrix{Float64}:
 1.0       0.376607
 0.376607  1.0
# example 4: create a matrix of random numbers and calculate the correlation matrix,
# but only for the first and last columns of the matrix
X = rand(10,5)
cor(X[:,[1,5]])
2×2 Matrix{Float64}:
  1.0         -0.00105065
 -0.00105065   1.0

In each of these examples, the cor() function will calculate the correlation matrix for the specified columns of the matrix X. The output will be a matrix with the same dimensions as the input, showing the correlation between each pair of variables.

Correlation matrices are often used in statistics and data analysis to understand the relationships between different variables, and to help identify potential correlations or trends in the data. So, it is a useful tool for understanding and analyzing datasets.

Related

  1. How to Create Diagonal Matrix in Julia?
  2. Convert dataframe to matrix in Julia
  3. Convert vector to matrix in Julia
  4. How to Create Matrix in Julia?