quantileSelect

fun <T : Comparable<T>> List<T>.quantileSelect(q: Double, method: QuantileMethod = QuantileMethod.NEAREST): T(source)

Selects the element at the q-th quantile position from this list.

Unlike quantile, this function works with any Comparable type (not just Double) and always returns an actual element from the list rather than an interpolated value. This is useful when you need, for example, the 20th percentile from an ordered list of strings.

Only non-interpolating methods are supported: QuantileMethod.INVERTED_CDF, QuantileMethod.CLOSEST_OBSERVATION, QuantileMethod.LOWER, QuantileMethod.HIGHER, and QuantileMethod.NEAREST.

Example:

listOf("a", "b", "c", "d", "e").quantileSelect(0.5) // "c"
listOf(10, 20, 30, 40, 50).quantileSelect(0.25, QuantileMethod.LOWER) // 20

Note: the default method is QuantileMethod.NEAREST, not QuantileMethod.LINEAR (which is the default for quantile), because quantileSelect only supports non-interpolating methods.

Return

the element at the q-th quantile position.

Parameters

q

the quantile to compute, in 0, 1.

method

the non-interpolating quantile method. Defaults to QuantileMethod.NEAREST.