In pandas, we have pd.rolling_quantile()
. And in numpy, we have np.percentile()
, but I'm not sure how to do the rolling/moving version of it.
To explain what I meant by moving/rolling percentile/quantile:
Given array [1, 5, 7, 2, 4, 6, 9, 3, 8, 10]
, the moving quantile 0.5
(i.e. moving percentile 50%) with window size 3 is:
1
5 - 1 5 7 -> 0.5 quantile = 5
7 - 5 7 2 -> 5
2 - 7 2 4 -> 4
4 - 2 4 6 -> 4
6 - 4 6 9 -> 6
9 - 6 9 3 -> 6
3 - 9 3 8 -> 8
8 - 3 8 10 -> 8
10
So [5, 5, 4, 4, 6, 6, 8, 8]
is the answer. To make the resulting series the same length as the input, some implementation inserts NaN
or None
, while pandas.rolling_quantile()
allows to compute the first two quantile values by a smaller window.