Correlation between data samples

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


stats::correlation([x1, x2, …], [y1, y2, …], <BravaisPearson | Fechner>)
stats::correlation([[x1, y1], [x2, y2], …], <BravaisPearson | Fechner>)
stats::correlation(s, <c1, c2>, <BravaisPearson | Fechner>)
stats::correlation(s, <[c1, c2]>, <BravaisPearson | Fechner>)
stats::correlation(s1, <c1>, s2, <c2>, <BravaisPearson | Fechner>)


stats::correlation([x1, x2, …], [y1, y2, …]) returns the linear (Bravais-Pearson) correlation coefficient


where and are the means of the data xi and yi.

stats::correlation([x1, x2, …], [y1, y2, …], Fechner) returns the Fechner correlation , where n is the sample size. The number vi is 1, if and have the same sign or are both 0. It is , if either or is 0. Otherwise, vi = 0.

Both the Bravais-Pearson correlation as well as the Fechner correlation are numbers between - 1 and 1.

The Bravais-Pearson correlation is close to 1 if the data pairs xi, yi are approximately related by a 'positive' linear relation (i.e., yiaxi + b with some positive coefficient a). It is close to - 1 if there is a 'negative' linear relation (with some negative coefficient a).

Correlation coefficients close to 0 correspond to non-linear relations or to unrelated data, respectively.

If the input data are floating-point numbers, the sums defining the Bravais-Pearson correlation are computed in a numerically stable way. If a floating-point result is desired, it is recommended to make sure that all input data are floats.

The Fechner correlation is always returned as a rational number.

The column indices c1, c2 are optional if the data are given by a stats::sample object s containing only two non-string data columns. If the data are provided by two samples s1, s2, the column indices are optional for samples containing only one non-string data column.


The Fechner correlation should not be computed for symbolic data. This may lead to unexpected results, if the sign of symbolic parameters cannot be determined.

External statistical data stored in an ASCII file can be imported into a MuPAD® session via import::readdata. In particular, see Example 1 of the corresponding help page.


Example 1

We compute the correlation of samples passed as lists:

X := [7, 33/7, 3, 5, 2]: Y := [3, 5, 1, 7, 2]:
stats::correlation(X, Y)

Alternatively, the data may be passed as a list of data pairs:

stats::correlation([[7, 3], [33/7, 5], [3, 1], [5, 7], [2, 2]])

If all data are floating-point numbers, the result is a float:

stats::correlation(float(X), float(Y))

The Fechner correlation of the data is always returned as a rational number:

stats::correlation(X, Y, Fechner),
stats::correlation(float(X), float(Y), Fechner)

The following exact result indicates an exact linear between the data pairs:

stats::correlation([0, 1, 2, 3], [7, 5, 3, 1])

Indeed, there is the `negative' linear relation y = 7 - 2 x between the data pairs.

delete X, Y:

Example 2

We create a sample of type stats::sample:

s := stats::sample([[1.0, 2.4, 3.0], 
                    [7.0, 4.8, 4.0], 
                    [3.3, 3.0, 5.0]])
1.0  2.4  3.0
7.0  4.8  4.0
3.3  3.0  5.0

We compute the correlation between the data of the first and the third column in several equivalent ways:

stats::correlation(s, 1, 3),
stats::correlation(s, [1, 3]),
stats::correlation(s, 1, s, 3)

stats::correlation(s, 1, 3, Fechner),
stats::correlation(s, [1, 3], Fechner),
stats::correlation(s, 1, s, 3, Fechner)

delete s:

Example 3

With symbolic data, the Bravais-Pearson correlation is returned as a symbolic expression:

stats::correlation([x1, x2], [y1, y2])



x1, y1, x2, y2, …

The statistical data: arithmetical expressions. The number of data xi must coincide with the number of data yi.

s, s1, s2

Samples of type stats::sample

c1, c2

Column indices: positive integers. Column c1 of s or s1, respectively, provides the data xi. Column c2 of s or s_2, respectively, provides the data yi.


Either BravaisPearson or Fechner. The default is the linear (Bravais-Pearson) correlation.


BravaisPearson, Fechner

Linear (Bravais-Pearson) or Fechner's correlation coefficient. Bravais-Pearson coefficient is the default, but may in some cases where the data is not normally distributed be less useful than Fechner's correlation.

Return Values

The Bravais-Pearson correlation is returned as an arithmetical expression. FAIL is returned if the variance of one of the data samples vanishes (the Bravais-Pearson correlation does not exist).

The Fechner correlation is returned as a rational number.

FAIL is returned if the data samples are empty.

See Also

MuPAD Functions

MuPAD Graphical Primitives