polars-xdt icon indicating copy to clipboard operation
polars-xdt copied to clipboard

fix documentation of ewma_by_time

Open azmyrajab opened this issue 10 months ago • 2 comments

Thanks for the nice package. A minor ask -

I was trying to follow the maths w/ ewma_by_time and noticed what I think is a minor typo:

In docstring:

        .. math::

            y_0 &= x_0

            \alpha_i &= \exp(-\lambda(t_i - t_{i-1}))

            y_i &= \alpha_i x_i + (1 - \alpha_i) y_{i-1}; \quad i > 0

In actual code, and what I think is correct:

let delta_time = time - prev_time;
// equivalent to:
// alpha = exp(-delta_time*ln(2) / half_life)
let alpha = (0.5_f64).powf(delta_time as f64 / half_life as f64);
let result = (1. - alpha) * value + alpha * prev_result;

Notice alpha hits the previous result (i.e. should be y_{i-1}) and 1 - alpha hits latest data (x_i).

If you agree, it might make sense to do the same w/ the upstreamed polars ewm_mean_by.

Anyways, thanks for the nice work on this feature.

azmyrajab avatar Apr 21 '24 23:04 azmyrajab