Covariance of data samples
MuPAD® notebooks are not recommended. 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::covariance([x_{1}, x_{2}, …]
,[y_{1}, y_{2}, …]
, <Sample  Population
>) stats::covariance([[x_{1}, y_{1}], [x_{2}, y_{2}], …]
, <Sample  Population
>) stats::covariance(s
, <c_{1}, c_{2}
>, <Sample  Population
>) stats::covariance(s
, <[c_{1}, c_{2}]
>, <Sample  Population
>) stats::covariance(s_{1}
, <c_{1}
>,s_{2}
, <c_{2}
>, <Sample  Population
>)
stats::covariance([x_{1}, x_{2},
…, x_{n}], [y_{1},
y_{2}, …, y_{n}])
returns
the covariance
,
where and are the arithmetic means of the data x_{i} and y_{i}, respectively.
stats::covariance([x_{1}, x_{2},
…, x_{n}], [y_{1},
y_{2}, …, y_{n}], Population)
returns
.
If the input data are floatingpoint numbers, the sums defining the covariance 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.
For exact input data, exact symbolic expressions are returned.
The column indices c_{1}
, c_{2}
are
optional if the data are given by a stats::sample
object s
containing
only two nonstring data columns. If the data are provided by two
samples s_{1}
, s_{2}
,
the column indices are optional for samples containing only one nonstring
data column.
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.
We compute the covariance of samples passed as lists:
X := [2, 33/7, 21/9, PI]: Y := [3, 5, 1, 7]: stats::covariance(X, Y)
Alternatively, the data may be passed as a list of data pairs:
stats::covariance([[2, 3], [33/7, 5], [21/9, 1], [PI, 7]])
If all data are floatingpoint numbers, the result is a float:
stats::covariance(float(X), float(Y))
delete X, Y:
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 covariance of the first column and the third column in several equivalent ways:
stats::covariance(s, 1, 3), stats::covariance(s, [1, 3]), stats::covariance(s, 1, s, 3)
delete s:
The covariance of symbolic data is returned as a symbolic expression:
stats::covariance([x1, x2], [y1, y2])
expand(%)

The statistical data: arithmetical expressions. The number of data x_{i} must coincide with the number of data y_{i}. 

Samples of type stats::sample 

Column indices: positive integers. Column 

The data are regarded as a "sample", not as a full population. This is the default. 

The data are regarded as the whole population, not as a sample. 