Skip to content

ding.torch_utils.math_helper

ding.torch_utils.math_helper

cov(x, rowvar=False, bias=False, ddof=None, aweights=None)

Overview

Estimates covariance matrix like numpy.cov.

Arguments: - x (:obj:torch.Tensor): A 1-D or 2-D tensor containing multiple variables and observations. Each row of x represents a variable, and each column a single observation of all those variables. - rowvar (:obj:bool): If rowvar is True by default, and each column is a single observation of all those variables. Otherwise, each column represents a variable, while the rows contain observations. - bias (:obj:bool): Default normalization (False) is by dividing N - 1, where N is the number of observations given (unbiased estimate). If bias is True, then normalization is by N. - ddof (:obj:Optional[int]): If ddof is not None, it implies that the argument bias is overridden. Note that ddof=1 will return the unbiased estimate (equals to bias=False), and ddof=0 will return the biased estimation (equals to bias=True). - aweights (:obj:Optional[torch.Tensor]): 1-D tensor of observation vector weights. These relative weights are typically large for observations considered “important” and smaller for observations considered less “important”. If ddof=0, the tensor of weights can be used to assign weights to observation vectors. Returns: - cov_mat (:obj:torch.Tensor): Covariance matrix calculated.

Full Source Code

../ding/torch_utils/math_helper.py

1from typing import Optional 2import torch 3 4 5def cov( 6 x: torch.Tensor, 7 rowvar: bool = False, 8 bias: bool = False, 9 ddof: Optional[int] = None, 10 aweights: Optional[torch.Tensor] = None 11) -> torch.Tensor: 12 """ 13 Overview: 14 Estimates covariance matrix like ``numpy.cov``. 15 Arguments: 16 - x (:obj:`torch.Tensor`): A 1-D or 2-D tensor containing multiple variables and observations. Each row of \ 17 ``x`` represents a variable, and each column a single observation of all those variables. 18 - rowvar (:obj:`bool`): If ``rowvar`` is True by default, and each column is a single observation of all those \ 19 variables. Otherwise, each column represents a variable, while the rows contain observations. 20 - bias (:obj:`bool`): Default normalization (False) is by dividing ``N - 1``, where ``N`` is the number of \ 21 observations given (unbiased estimate). If ``bias`` is ``True``, then normalization is by ``N``. 22 - ddof (:obj:`Optional[int]`): If ``ddof`` is not ``None``, it implies that the argument ``bias`` is \ 23 overridden. Note that ``ddof=1`` will return the unbiased estimate (equals to ``bias=False``), and \ 24 ``ddof=0`` will return the biased estimation (equals to ``bias=True``). 25 - aweights (:obj:`Optional[torch.Tensor]`): 1-D tensor of observation vector weights. These relative weights \ 26 are typically large for observations considered “important” and smaller for observations considered less \ 27 “important”. If ``ddof=0``, the tensor of weights can be used to assign weights to observation vectors. 28 Returns: 29 - cov_mat (:obj:`torch.Tensor`): Covariance matrix calculated. 30 """ 31 if x.dim() == 1 and rowvar: 32 raise NotImplementedError 33 # ensure at least 2D 34 if x.dim() == 1: 35 x = x.view(-1, 1) 36 37 # treat each column as a data point, each row as a variable 38 if rowvar and x.shape[0] != 1: 39 x = x.t() 40 41 if ddof is None: 42 if bias == 0: 43 ddof = 1 44 else: 45 ddof = 0 46 47 w = aweights 48 if w is not None: 49 if not torch.is_tensor(w): 50 w = torch.tensor(w, dtype=torch.float) 51 w_sum = torch.sum(w) 52 avg = torch.sum(x * (w / w_sum)[:, None], 0) 53 else: 54 avg = torch.mean(x, 0) 55 56 # Determine the normalization 57 if w is None: 58 fact = x.shape[0] - ddof 59 elif ddof == 0: 60 fact = w_sum 61 # elif aweights is None: 62 # fact = w_sum - ddof 63 else: 64 fact = w_sum - ddof * torch.sum(w * w) / w_sum 65 66 xm = x.sub(avg.expand_as(x)) 67 68 if w is None: 69 X_T = xm.t() 70 else: 71 X_T = torch.mm(torch.diag(w), xm).t() 72 73 c = torch.mm(X_T, xm) 74 c = c / fact 75 76 return c.squeeze()