Skip to content

scatter_std

scatter_std(
    values: array,
    index: array,
    out_size: int,
    semi_grad: bool = False,
    eps: float = 1e-05,
) -> array

Computes the (population) standard deviation of the values grouped by index (along axis 0).

As in PyG's StdAggregation, the variance is clamped to eps before the square root (so that the gradient stays finite) and standard deviations up to sqrt(eps) are set to 0.

Parameters:

Name Type Description Default
values array

Input array of shape [num_values, ...].

required
index array

Array of shape [num_values] with the group of each value.

required
out_size int

Number of groups.

required
semi_grad bool

See scatter_var(). Default: False

False
eps float

Lower bound of the variance. Default: 1e-5

1e-05

Returns:

Type Description
array

An array of shape [out_size, ...] with the standard deviation of

array

each group.

Source code in mlx_graphs/utils/scatter.py
def scatter_std(
    values: mx.array,
    index: mx.array,
    out_size: int,
    semi_grad: bool = False,
    eps: float = 1e-5,
) -> mx.array:
    """Computes the (population) standard deviation of the values grouped by
    index (along axis 0).

    As in PyG's ``StdAggregation``, the variance is clamped to ``eps`` before
    the square root (so that the gradient stays finite) and standard
    deviations up to ``sqrt(eps)`` are set to 0.

    Args:
        values: Input array of shape ``[num_values, ...]``.
        index: Array of shape ``[num_values]`` with the group of each value.
        out_size: Number of groups.
        semi_grad: See :func:`scatter_var`. Default: ``False``
        eps: Lower bound of the variance. Default: ``1e-5``

    Returns:
        An array of shape ``[out_size, ...]`` with the standard deviation of
        each group.
    """
    var = scatter_var(values, index, out_size, semi_grad=semi_grad)
    out = mx.sqrt(mx.maximum(var, eps))
    return mx.where(out <= math.sqrt(eps), mx.zeros_like(out), out)

from mlx_graphs.utils.scatter import scatter_std · mlx_graphs/utils/scatter.py