Skip to content

Feature request: Centered roll #2

@RichieHakim

Description

@RichieHakim

As is, the roll is left-aligned, or 'real-time'. This is great for some uses, but it would be great if there was an option to have a 'centered' option such that the edges are rolled all the way to the end (decreasing number of elements in sliding window towards the edges). Such options exist in rolling stats packages like pandas.rolling(center=True, min_periods=1).quantile(q).
I'm aware that a centered method can be achieved by effectively lagging the output by pipe.lag. However, it is not possible to recover the right edge of the output. To be clear, when you compare the output traces from these two functions, you can clearly see how rolling_quantiles is not able to achieve the same kind of output.

import numpy as np
import pandas as pd
import rolling_quantiles as rq
import matplotlib.pyplot as plt

import multiprocessing as mp
from functools import partial

def rp_pandas(x_in, win_len, ptile=10):
    x_in_df = pd.Series(x_in)
    x_out_pd = x_in_df.rolling(window=win_len, center=True, min_periods=1).quantile(quantile=(ptile/100))
    return np.array(x_out_pd)

def rp_rq_centered(x_in, win_len, ptile=10):
    pipe = rq.Pipeline( rq.LowPass(window=win_len, quantile=(ptile/100)) )
    lag = int(np.floor(pipe.lag))
    return pipe.feed(x_in)[lag:]

def mp_rolling_quantile(x_in, win_len, ptile, function):
    pool = mp.Pool(processes=None)
    results = pool.map(partial(function , win_len=win_len, ptile=ptile), [x_in[ii] for ii in np.arange(x_in.shape[0])])
    pool.close()
    pool.join()
    return np.row_stack(results)

x_in = np.random.rand(100, 100)
win_len = 11
ptile = 50


output_pd = mp_rolling_quantile(x_in, win_len, ptile, rp_pandas)
output_rq = mp_rolling_quantile(x_in, win_len, ptile, rp_rq_centered)

%matplotlib notebook
plt.figure()
plt.plot(x_in[0])
plt.plot(output_pd[0][0:], linewidth=2)
plt.plot(output_rq[0][0:], linewidth=2)
plt.legend(['x_in', 'pd: centered=True, min_periods=1', 'rq: centered'])

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions