torch.cov
- torch.cov(input, *, correction=1, fweights=None, aweights=None) → Tensor
Estimates the covariance matrix of the variables given by the input matrix, where rows are
the variables and columns are the observations.
A covariance matrix is a square matrix giving the covariance of each pair of variables. The diagonal contains
the variance of each variable (covariance of a variable with itself). By definition, if input represents
a single variable (Scalar or 1D) then its variance is returned.
The sample covariance of the variables and is given by:
where and are the simple means of the and respectively, and is the .
If fweights and/or aweights are provided, the weighted covariance
is calculated, which is given by:
where denotes fweights or aweights (f and a for brevity) based on whichever is
provided, or if both are provided, and
is the weighted mean of the variable. If not
provided, and/or can be seen as a vector of appropriate size.
input (Tensor) – A 2D matrix containing multiple variables and observations, or a Scalar or 1D vector representing a single variable.
correction (int, optional) – difference between the sample size and sample degrees of freedom.
Defaults to Bessel’s correction, correction = 1 which returns the unbiased estimate,
even if both fweights and aweights are specified. correction = 0
will return the simple average. Defaults to .
Example:
>>> x = torch.tensor([[0, 2], [1, 1], [2, 0]]).T
>>> x
tensor([[0, 1, 2],
[2, 1, 0]])
>>> torch.cov(x)
tensor([[ 1., -1.],
[-1., 1.]])
correctionfa1fweights (tensor, optional) – A Scalar or 1D tensor of observation vector frequencies representing the number of
times each observation should be repeated. Its numel must equal the number of columns of input.
Must have integral dtype. Ignored if None. Defaults to None.
aweights (tensor, optional) – A Scalar or 1D array of observation vector weights.
These relative weights are typically large for observations considered “important” and smaller for
observations considered less “important”. Its numel must equal the number of columns of input.
Must have floating point dtype. Ignored if None. Defaults to None.
(Tensor) The covariance matrix of the variables.