Skip to content

QuantileAggregation

QuantileAggregation(
    q: Union[float, List[float]],
    interpolation: QuantileInterpolations = "linear",
    fill_value: float = 0.0,
)

Bases: Aggregation

Feature-wise \(q\)-th quantile of the elements of each set.

For every feature \(d\), with \(x_{\pi_1,d} \le \dots \le x_{\pi_n,d}\) the sorted values of a set of size \(n\), the quantile is read at position \(q \cdot (n - 1)\), interpolating between the two neighbouring values when this position is not an integer.

Parameters:

Name Type Description Default
q Union[float, List[float]]

Quantile, or list of quantiles, in [0, 1]. With several quantiles, the results are concatenated along the last axis.

required
interpolation QuantileInterpolations

Interpolation between two values a <= b: "linear" (a + (b - a) * frac), "lower" (a), "higher" (b), "nearest" or "midpoint". Default: "linear"

'linear'
fill_value float

Output of empty sets. Default: 0.0

0.0

Example:

import mlx.core as mx
from mlx_graphs.nn.aggr import QuantileAggregation

aggr = QuantileAggregation(q=[0.25, 0.75])
x = mx.random.normal((6, 16))
index = mx.array([0, 0, 1, 1, 1, 2])
>>> aggr(x, index).shape
(3, 32)
Source code in mlx_graphs/nn/aggr/quantile.py
def __init__(
    self,
    q: Union[float, List[float]],
    interpolation: QuantileInterpolations = "linear",
    fill_value: float = 0.0,
):
    super().__init__()
    qs = list(q) if isinstance(q, (list, tuple)) else [q]
    if len(qs) == 0:
        raise ValueError("Provide at least one quantile value for `q`.")
    if not all(0.0 <= quantile <= 1.0 for quantile in qs):
        raise ValueError("`q` must be in the range [0, 1].")
    if interpolation not in get_args(QuantileInterpolations):
        raise ValueError(f"Invalid interpolation method '{interpolation}'")
    self._q = q
    self.interpolation = interpolation
    self.fill_value = fill_value

from mlx_graphs.nn.aggr import QuantileAggregation · mlx_graphs/nn/aggr/quantile.py