Covariance

Computes the covariance of two tensors along named dimension. If the inputs are of rank $N$ and $M$ respectively, the output will be a $(N-1)\times(M-1)$ rank tensor, where the $(i,j)$ element is the covariance of the $i$-th slice of the first argument along the named dimension, and the $j$-th slice along the named dimension. As such, it is conformant with the definition of cov function in Octave, but not with the equivalently named function in Matlab:

Compatibility Note:: Octave always treats rows of X and Y as multivariate random variables. For two inputs, however, MATLAB treats X and Y as two univariate distributions regardless of their shapes, and will calculate ‘cov ([X(:), Y(:)])’ whenever the number of elements in X and Y are equal. This will result in a 2x2 matrix. Code relying on MATLAB’s definition will need to be changed when running in Octave.

If only a single argument $x$ is supplied to the covariance, then the result is equivalent to cov$(x,x)$, ie each slice is covaried with each other slice.

The formula for covariance between stochastic variables $x$ and $y$ is

\begin{displaymath}
\mathrm{cov}(x,y)=\frac1{N-1}\sum_i(x_i-\langle
x\rangle)(y_i-\langle y\rangle)
\end{displaymath}