qfeval_functions.functions.nanslope

nanslope(x, y, dim=(), keepdim=False)[source]

Compute the slope of simple linear regression between two tensors, ignoring NaN values.

This function calculates the slope (beta coefficient) of the linear regression line that best fits the relationship between x and y, excluding NaN values from the computation. The slope represents the rate of change in y per unit change in x.

The function implements the standard ordinary least squares (OLS) formula for the slope coefficient in simple linear regression:

\[\beta = \frac{\text{Cov}(X, Y)}{\text{Var}(X)} = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2}\]

where \(\bar{x}\) and \(\bar{y}\) are the means of x and y respectively, computed only over valid (non-NaN) pairs.

If either x or y has a NaN at a given position, that pair is excluded from all calculations including mean computation and slope estimation.

Parameters:
  • x (Tensor) – The independent variable tensor (predictor).

  • y (Tensor) – The dependent variable tensor (response). Must be broadcastable with x.

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

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

Returns:

The slope coefficients computed only over valid (non-NaN) pairs. If there are insufficient valid pairs or if the variance of x is zero, the result may contain NaN values. The shape depends on the input dimensions, dim, and keepdim parameters.

Return type:

Tensor

Example

>>> # Simple linear relationship with some NaN values
>>> x = torch.tensor([1.0, 2.0, nan, 4.0, 5.0])
>>> y = torch.tensor([2.0, 4.0, 6.0, nan, 10.0])
>>> QF.nanslope(x, y)
tensor(2.)
>>> # 2D tensors with slopes along rows
>>> x = torch.tensor([[1.0, 2.0, 3.0],
...                   [1.0, nan, 3.0]])
>>> y = torch.tensor([[2.0, 4.0, 6.0],
...                   [3.0, 5.0, nan]])
>>> QF.nanslope(x, y, dim=1)
tensor([2., nan])
>>> # Perfect linear relationship
>>> x = torch.tensor([1.0, 2.0, 3.0, 4.0])
>>> y = torch.tensor([3.0, 5.0, 7.0, 9.0])  # y = 2x + 1
>>> QF.nanslope(x, y)
tensor(2.)
>>> # With keepdim
>>> x = torch.tensor([[1.0, nan, 3.0],
...                   [2.0, 4.0, 6.0]])
>>> y = torch.tensor([[2.0, 4.0, nan],
...                   [1.0, 2.0, 3.0]])
>>> QF.nanslope(x, y, dim=1, keepdim=True)
tensor([[   nan],
        [0.5000]])
>>> # Broadcasting example
>>> x = torch.tensor([[1.0], [2.0], [3.0]])
>>> y = torch.tensor([2.0, 4.0, 6.0])
>>> QF.nanslope(x, y)
tensor(0.)
>>> # Zero variance in x (undefined slope)
>>> x = torch.tensor([2.0, 2.0, 2.0])
>>> y = torch.tensor([1.0, 3.0, 5.0])
>>> QF.nanslope(x, y)
tensor(nan)

Warning

The slope calculation requires at least 2 valid (non-NaN) pairs of observations. With fewer valid pairs, the result will be NaN. Additionally, if all valid x values are identical (zero variance), the slope is mathematically undefined.

See also

nancorrel(): NaN-aware correlation coefficient computation. nancovar(): NaN-aware covariance computation. nanmean(): NaN-aware mean computation. nanmulmean(): NaN-aware element-wise product mean.