quantile#
- caput.algorithms.median.quantile(A: numpy.typing.NDArray[numpy.int_ | numpy.float64], W: numpy.typing.NDArray[numpy.int_ | numpy.float64], q: float, method: Literal['lower', 'higher', 'split'] = 'split') numpy.ndarray[numpy.floating | numpy.integer] | numpy.integer | numpy.floating[source]#
Calculate the weighted quantile of a set of data.
The weighted quantile is always calculated along the last axis.
The weights must be postive or zero for the calculation to make sense. This is not checked within this routine, and so you must sanitize the input before calling.
In the case that all elements have zero weight, a standard uniformly weighted quantile calculation is performed. If there is one, and only one non-zero weighted element that is always returned. For two or more non-zero weighted elements, we can proceed as expected.
If the quantile is “split”, i.e. it lies exactly on the boundary between two elements, we use the bounding non-zero weighted elements to calculate the quantile according to the chosen method.
Examples of special cases:
>>> quantile([1.0, 2.0, 3.0, 4.0], [1, 1, 0, 2], 0.5) 3.0 >>> quantile([1.0, 2.0, 3.0, 4.0], [1, 1, 0, 2], 0.5) 3.0 >>> quantile([1.0, 2.0, 4.0, 3.0], [0, 0, 0, 0], 0.5) 2.5
- Parameters:
- A
ndarray[int|float] The array of data. Only 32 and 64 bit integers and floats are supported.
- W
ndarray[int|float] The array of weights. Only 32 and 64 bit integers and floats are supported.
- q
float The quantile as a number from zero to one.
- method{“lower”, “higher”, “split”}
Method to use if the requested quantile is exactly between two elements. Must be one of “lower”, “higher” or “split”. Default is “split”.
- A
- Returns:
- quantile
ndarray The calculated quantile. This has the same shape as A with the last axis removed. If A was one dimensional the value returned is a scalar.
- quantile
- Raises:
ValueErrorIf the array shapes do not match, or the quantile value is invalid.
TypeErrorIf the array types are not supported.