0

I am looking for rolling_product similar to rolling_sum in polars. Rolling_apply sum is slow compared to rolling_sum.

If it’s not possible, need help to create rolling_prod function. Unable to locate rolling_sum in GIT. Could someone share the location of rolling_sum in git

  • 1
    I'm not sure why `rolling_product` does not exist - it may be worth asking on the issues tracker. As for git - are you looking for: https://github.com/pola-rs/polars/tree/main/polars/polars-arrow/src/kernels/rolling – jqurious Apr 22 '23 at 12:58

1 Answers1

1

There is currently no rolling_product in Polars. You can construct yourself using rolling_apply as you say, but this is not the most efficient as it recomputes the product over each window every time as you have found out.

I can think of two possible solutions that may be faster by avoiding complete recomputation and can readily be implemented using existing methods. Note, haven't benchmarked either of these, and for sure a specialized implementation will beat these approaches.

  1. use cumprod to generate the cumulative product, and then shift this X periods, where X is your window for the rolling product, to generate the lagged version. Take the ratio of the unlagged and the lagged version.
  2. use that a * b can be written as exp(log(a) + log(b)). So you could do something like
pl.col("a").log().rolling_sum(...).exp()

This may introduce numeric instabilities though, and wont work with non-positive numbers.

jvz
  • 1,183
  • 6
  • 13