qfeval_functions.functions.nanvar

nanvar(x, dim=(), unbiased=True, keepdim=False)[source]

Compute the variance of a tensor, ignoring NaN values.

This function calculates the variance of tensor elements along specified dimensions while excluding NaN values. The variance can be computed using either the unbiased estimator (dividing by N-1) or the biased estimator (dividing by N), where N is the number of non-NaN elements.

Parameters:
  • x (Tensor) – The input tensor.

  • dim (Union[int, Tuple[int, ...]]) – The dimension(s) along which to compute the variance. If not specified (default is an empty tuple), the variance is computed over all elements. Can be a single dimension or multiple dimensions.

  • unbiased (bool) – If True (default), uses Bessel’s correction and divides by N-1 where N is the number of non-NaN elements. If False, divides by N.

  • keepdim (bool) – If True, the output tensor has the same number of dimensions as the input, with the reduced dimensions having size 1. If False (default), the reduced dimensions are removed.

Returns:

The variance of non-NaN elements. The shape depends on the dim and keepdim parameters.

Return type:

Tensor

Example

>>> x = torch.tensor([1.0, 2.0, nan, 4.0, 5.0])
>>> QF.nanvar(x)
tensor(3.3333)
>>> # With biased estimator
>>> QF.nanvar(x, unbiased=False)
tensor(2.5000)
>>> # 2D tensor with dimension specification
>>> x = torch.tensor([[1.0, 2.0, nan],
...                   [4.0, nan, 6.0]])
>>> QF.nanvar(x, dim=1)
tensor([0.5000, 2.0000])
>>> # Keep dimensions
>>> QF.nanvar(x, dim=1, keepdim=True)
tensor([[0.5000],
        [2.0000]])