tfep.utils.math.cov

tfep.utils.math.cov(x, ddof=1, dim_sample=0, inplace=False, return_mean=False)[source]

Return the covariance matrix of the data.

Note

Since PyTorch 1.10, there is also a torch.cov function. Compare to that function, this function currently does not offer weighting but allows selecting the sample dimension and returning also the mean. Nevertheless, we may drop or change name to this function when we stop supporting PyTorch 1.9.

Parameters:
  • x (torch.Tensor) – A tensor of shape (n, m), where n is the number of samples used to estimate the covariance, and m is the dimension of the multivariate variable. If dim_sample is 1, then the expected shape is (m, n).

  • ddof (int, optional) – The number of dependent degrees of freedom. The covariance will be estimated dividing by n - ddof. Default is 1.

  • dim_sample (int, optional) – The dimension of the features. Default is 0, which means each row of x is a sample and each column a different degree of freedom.

  • inplace (bool, optional) – If True, the input argument x is modified to be centered on its mean. Default is False.

  • return_mean (bool, optional) – If True, the mean of degrees of freedom is also returned. This can save an operation if the mean is also required after computing the covariance matrix.

Returns:

  • cov (torch.Tensor) – A tensor of shape (m, m).

  • mean (torch.Tensor, optional) – A tensor of shape (m,).