qfeval_functions.functions.ma
- ma(x, span, dim=-1)[source]
Compute the moving (sliding window) average of a tensor.
This function calculates the average of elements within a sliding window of size
spanalong the specified dimension. The output tensor has the same shape as the input tensor. For positions where the sliding window cannot fully cover preceding elements (i.e., the firstspan - 1elements along the selected dimension), the result isnan.The moving average is computed as:
\[\text{MA}[i] = \frac{1}{\text{span}} \sum_{j=i-\text{span}+1}^{i} x[j]\]This is a simple moving average (SMA) where all values in the window have equal weight.
- Parameters:
- Returns:
A tensor of the same shape as the input, containing the moving averages. The first
span - 1elements along the specified dimension arenan.- Return type:
Example
>>> # Simple moving average with window size 3 >>> x = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0]) >>> QF.ma(x, span=3) tensor([nan, nan, 2., 3., 4.])
>>> # 2D tensor with moving average along columns >>> x = torch.tensor([[1.0, 2.0, 3.0, 4.0], ... [5.0, 6.0, 7.0, 8.0], ... [9.0, 10.0, 11.0, 12.0]]) >>> QF.ma(x, span=2, dim=1) tensor([[ nan, 1.5000, 2.5000, 3.5000], [ nan, 5.5000, 6.5000, 7.5000], [ nan, 9.5000, 10.5000, 11.5000]])
>>> # Moving average along the first dimension >>> x = torch.tensor([[1.0, 2.0], ... [3.0, 4.0], ... [5.0, 6.0], ... [7.0, 8.0]]) >>> QF.ma(x, span=3, dim=0) tensor([[nan, nan], [nan, nan], [3., 4.], [5., 6.]])