Correlation quantifies the strength of a linear relationship between two variables. When there is no correlation between two variables, then there is no tendency for the values of the variables to increase or decrease in tandem. Two variables that are uncorrelated are not necessarily independent, however, because they might have a nonlinear relationship.
You can use linear correlation to investigate whether a linear relationship exists between variables without having to assume or fit a specific model to your data. Two variables that have a small or no linear correlation might have a strong nonlinear relationship. However, calculating linear correlation before fitting a model is a useful way to identify variables that have a simple relationship. Another way to explore how variables are related is to make scatter plots of your data.
Covariance quantifies the strength of a linear relationship between two variables in units relative to their variances. Correlations are standardized covariances, giving a dimensionless quantity that measures the degree of a linear relationship, separate from the scale of either variable.
The following MATLAB® functions compute sample correlation coefficients and covariance. These sample coefficients are estimates of the true covariance and correlation coefficients of the population from which the data sample is drawn.
Use the MATLAB
cov function to calculate the
sample covariance matrix for a data matrix (where each column represents a separate
The sample covariance matrix has the following properties:
diag(cov(X))is a vector of variances for each data column. The variances represent a measure of the spread or dispersion of data in the corresponding column. (The
varfunction calculates variance.)
sqrt(diag(cov(X)))is a vector of standard deviations. (The
stdfunction calculates standard deviation.)
The off-diagonal elements of the covariance matrix represent the covariances between the individual data columns.
X can be a vector or a matrix. For an
m-by-n matrix, the covariance matrix is
For an example of calculating the covariance, load the sample data in
count.dat that contains a 24-by-3
Calculate the covariance matrix for this data:
MATLAB responds with the following result:
ans = 1.0e+003 * 0.6437 0.9802 1.6567 0.9802 1.7144 2.6908 1.6567 2.6908 4.6278
The covariance matrix for this data has the following form:
Here, s2ij is the sample covariance between column i and
column j of the data. Because the
matrix contains three columns, the covariance matrix is 3-by-3.
In the special case when a vector is the argument of
the function returns the variance.
corrcoef produces a matrix of
sample correlation coefficients for a data matrix (where each column represents a
separate quantity). The correlation coefficients range from -1 to 1, where
Values close to 1 indicate that there is a positive linear relationship between the data columns.
Values close to -1 indicate that one column of data has a negative linear relationship to another column of data (anticorrelation).
Values close to or equal to 0 suggest there is no linear relationship between the data columns.
For an m-by-n matrix, the correlation-coefficient matrix is n-by-n. The arrangement of the elements in the correlation coefficient matrix corresponds to the location of the elements in the covariance matrix, as described in Covariance.
For an example of calculating correlation coefficients, load the sample data in
count.dat that contains a 24-by-3
Type the following syntax to calculate the correlation coefficients:
This results in the following 3-by-3 matrix of correlation coefficients:
ans = 1.0000 0.9331 0.9599 0.9331 1.0000 0.9553 0.9599 0.9553 1.0000
Because all correlation coefficients are close to 1, there is a strong positive
correlation between each pair of data columns in the