azmyrajab / polars_ols

Polars least squares extension - enables fast linear model polar expressions
MIT License
100 stars 9 forks source link

[Feature request] windowed wrls #23

Open m00ngoose opened 2 months ago

m00ngoose commented 2 months ago

I propose some additional functionality; consider a dataframe with multiple observations per day, and suppose I wish to regress with daily state updates (generalizing: update based on some suitable class of windowing function, rather than update once / row). This could apply to all of rolling/expanding/rls. The output shape would be according to the window groups (eg. one row per day containing coefficients for that window). Additionally, I would like to be able to provide per-row sample weights.

azmyrajab commented 3 weeks ago

Hi @m00ngoose , I thought a bit about this - and my proposal for the flexible spec you are after is:

(
    df
    .with_row_index()  # <-- in general can be any intraday timestamp, daily date, or index
    .rolling(index_column="index", period="252i", min_periods=1) # <--- period can be temporal e.g. "7d" if index is time
    .agg(pl.col("y").least_squares.ols(cs.starts_with("x"), mode="coefficients")) # <-- can pass weights, ridge penalty, etc. here
)

Could you have a go at trying this and letting me know if this works for you?