vp.stats.quantile
Sample quantiles of Hyndman and Fan's nine types, as R's quantile() computes them.
Part of the distribution vipyr; it runs from a checkout of the
repository. Planned on PyPI as pip install vipyr.
vp.stats.quantile(x, q, type=7) is R's quantile(x, q, type = type):
types 1-3 are the discontinuous definitions (the inverse of the empirical
distribution function, its average at the jumps, the nearest even order
statistic), types 4-9 interpolate between order statistics at
a + q (n + 1 - a - b) with Hyndman and Fan's (a, b). Type 7 is R's and
NumPy's default; type 8 is the one Hyndman and Fan recommend. R's
4-epsilon fuzz in the index is kept, so a q that lands on an order
statistic up to rounding takes that order statistic, as in R.
Signature
vp.stats.quantile(x, q, *, type=7) Parameters
- x : ndarray[float64], required
- The sample.
- q : float | ndarray[float64], required
- The probabilities.
- type : int, default
7 - Hyndman and Fan's type, 1 to 9 (R's quantile(type = ); 7 is R's and NumPy's default).
Returns
A frozen QuantileResult. Iterating it gives the fields below in order; it also has
params, tests, log and check().
- values : float | ndarray[float64]
- The sample quantiles, q's shape.
- type : int
- The type used.
Raises
- ValueError
- A type outside 1 to 9, or q outside [0, 1].
- TypeError
- An argument of the wrong type, or an unknown keyword.
- vipyr.VivBuildError
- An ImportError: no C compiler, or the program's C does not build.
Examples
import vipyr as vp
x = [2.0, 7.0, 1.0, 8.0, 2.0, 8.0, 1.0, 8.0]
for t in (1, 6, 7, 8):
print(t, vp.stats.quantile(x, [0.25, 0.5], type=t).values)
r = vp.stats.quantile(x, 0.9)
assert r.check() 1 [1. 2.]
6 [1.25 4.5 ]
7 [1.75 4.5 ]
8 [1.41666667 4.5 ]Each example ran as written when these pages were generated, and the output shown is what it printed.
Checked against
- Reference
- R 4.3.3's quantile() for all nine types (lib/stats/describe.viv, python/tests/test_stats.py).
Source
- Viv: lib/stats/describe.viv
- Viv: lib/stats/api.viv