qfeval_functions.functions.nanskew

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

Compute the skewness along specified dimensions, ignoring NaN values.

This function calculates the skewness (third standardized moment) of a tensor along the specified dimension(s), excluding NaN values from the computation. Skewness measures the asymmetry of a probability distribution around its mean, indicating whether the data is concentrated more on one side of the distribution.

The skewness is computed as:

\[\text{skew} = \frac{\text{E}[(X - \mu)^3]}{\sigma^3}\]

where \(\mu\) is the mean and \(\sigma\) is the standard deviation, computed only over valid (non-NaN) values. Positive skewness indicates a longer tail on the right side of the distribution, while negative skewness indicates a longer tail on the left side.

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

  • dim (Union[int, Tuple[int, ...]]) – The dimension(s) along which to compute the skewness. If not specified (default is empty tuple), computes over all dimensions.

  • unbiased (bool) – If True (default), uses unbiased estimation with bias correction. If False, uses biased estimation.

  • keepdim (bool) – Whether the output tensor has dim retained or not. Default is False.

Returns:

The skewness values computed only over valid (non-NaN) values. The shape depends on the input dimensions and the keepdim parameter.

Return type:

Tensor

Example

>>> # Simple skewness with some NaN values
>>> x = torch.tensor([1.0, 2.0, nan, 4.0, 5.0, 10.0])
>>> QF.nanskew(x, dim=0)
tensor(1.1846)
>>> # 2D tensor with skewness along columns
>>> x = torch.tensor([[1.0, nan, 3.0, 4.0],
...                   [2.0, 5.0, nan, 6.0],
...                   [3.0, 7.0, 8.0, nan]])
>>> QF.nanskew(x, dim=1)
tensor([-0.9352, -1.2933, -1.4579])
>>> # Skewed distribution
>>> x = torch.tensor([1.0, 1.0, 1.0, nan, 2.0, 10.0])
>>> QF.nanskew(x, dim=0)
tensor(2.1713)
>>> # With keepdim
>>> x = torch.tensor([[1.0, 2.0, nan],
...                   [4.0, nan, 6.0]])
>>> QF.nanskew(x, dim=1, keepdim=True)
tensor([[nan],
        [nan]])
>>> # Biased estimation
>>> x = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
>>> QF.nanskew(x, dim=0, unbiased=False)
tensor(0.)
>>> # Negative skewness (left tail)
>>> x = torch.tensor([1.0, 8.0, 9.0, nan, 9.0, 10.0])
>>> QF.nanskew(x, dim=0)
tensor(-2.0287)

Warning

Skewness calculations can be sensitive to outliers. A single extreme value can significantly affect the skewness measure, especially with small sample sizes.

See also

nankurtosis(): NaN-aware kurtosis function. nanvar(): NaN-aware variance function. nanstd(): NaN-aware standard deviation function. nanmean(): NaN-aware mean function.