Rate this Page

torch.cov#

torch.cov(input,*,correction=1,fweights=None,aweights=None)Tensor#

Estimates the covariance matrix of the variables given by theinput matrix, where rows arethe variables and columns are the observations.

A covariance matrix is a square matrix giving the covariance of each pair of variables. The diagonal containsthe variance of each variable (covariance of a variable with itself). By definition, ifinput representsa single variable (Scalar or 1D) then its variance is returned.

The sample covariance of the variablesxx andyy is given by:

cov(x,y)=i=1N(xixˉ)(yiyˉ)max(0, N  δN)\text{cov}(x,y) = \frac{\sum^{N}_{i = 1}(x_{i} - \bar{x})(y_{i} - \bar{y})}{\max(0,~N~-~\delta N)}

wherexˉ\bar{x} andyˉ\bar{y} are the simple means of thexx andyy respectively, andδN\delta N is thecorrection.

Iffweights and/oraweights are provided, the weighted covarianceis calculated, which is given by:

covw(x,y)=i=1Nwi(xiμx)(yiμy)max(0, i=1Nwi  i=1Nwiaii=1Nwi δN)\text{cov}_w(x,y) = \frac{\sum^{N}_{i = 1}w_i(x_{i} - \mu_x^*)(y_{i} - \mu_y^*)}{\max(0,~\sum^{N}_{i = 1}w_i~-~\frac{\sum^{N}_{i = 1}w_ia_i}{\sum^{N}_{i = 1}w_i}~\delta N)}

whereww denotesfweights oraweights (f anda for brevity) based on whichever isprovided, orw=f×aw = f \times a if both are provided, andμx=i=1Nwixii=1Nwi\mu_x^* = \frac{\sum^{N}_{i = 1}w_ix_{i} }{\sum^{N}_{i = 1}w_i} is the weighted mean of the variable. If notprovided,f and/ora can be seen as a1\mathbb{1} vector of appropriate size.

Parameters

input (Tensor) – A 2D matrix containing multiple variables and observations, or aScalar or 1D vector representing a single variable.

Keyword Arguments
  • 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 bothfweights andaweights are specified.correction=0will return the simple average. Defaults to1.

  • fweights (tensor,optional) – A Scalar or 1D tensor of observation vector frequencies representing the number oftimes each observation should be repeated. Its numel must equal the number of columns ofinput.Must have integral dtype. Ignored ifNone. Defaults toNone.

  • aweights (tensor,optional) – A Scalar or 1D array of observation vector weights.These relative weights are typically large for observations considered “important” and smaller forobservations considered less “important”. Its numel must equal the number of columns ofinput.Must have floating point dtype. Ignored ifNone. Defaults toNone.

Returns

(Tensor) The covariance matrix of the variables.

See also

torch.corrcoef() normalized covariance matrix.

Example:

>>>x=torch.tensor([[0,2],[1,1],[2,0]]).T>>>xtensor([[0, 1, 2],        [2, 1, 0]])>>>torch.cov(x)tensor([[ 1., -1.],        [-1.,  1.]])>>>torch.cov(x,correction=0)tensor([[ 0.6667, -0.6667],        [-0.6667,  0.6667]])>>>fw=torch.randint(1,10,(3,))>>>fwtensor([1, 6, 9])>>>aw=torch.rand(3)>>>awtensor([0.4282, 0.0255, 0.4144])>>>torch.cov(x,fweights=fw,aweights=aw)tensor([[ 0.4169, -0.4169],        [-0.4169,  0.4169]])