nk 1.0.0
C++ Numerical Computing Runtime
Loading...
Searching...
No Matches
quantile_select.hpp
1// ops/include/nk/ops/quantile_select.hpp
2//
3// nth_element-based quantile selection over a raw double buffer — the numerical
4// primitive behind stats iqr / mad / median-of-slice. Raw-buffer, core-free.
5// Promoted to the kernel layer so any toolbox needing the quantile of a flat
6// buffer shares one O(n)-average implementation instead of re-deriving it.
7
8#pragma once
9
10#include <cstddef>
11
12namespace nk::ops {
13
14// Linear-interpolation quantile of s[0,n) at probability p — MATLAB R2025b
15// default (type 5: order-statistic positions (k-0.5)/N, i.e. q = p*N + 0.5
16// clamped to [1, N]). Selects with nth_element (O(n) average); the picked order
17// statistics are exactly the sorted ones, so the result is bit-identical to a
18// full-sort quantile, only cheaper.
19//
20// MUTATES the buffer (nth_element reorders s in place) — callers pass a scratch
21// copy. Returns NaN for n == 0.
22double sliceQuantile(double *s, std::size_t n, double p);
23
24} // namespace nk::ops