Skip to content

Commit

Permalink
fix[python, rust]: special-case ewm_mean(alpha=1) (#5019)
Browse files Browse the repository at this point in the history
  • Loading branch information
matteosantama committed Sep 29, 2022
1 parent 98838ca commit 4ea2f78
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions polars/polars-arrow/src/kernels/ewm/average.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ where
I::IntoIter: TrustedLen,
T: Float + NativeType + AddAssign,
{
if alpha.is_one() {
return ewm_mean_alpha_equals_one(xs, min_periods);
}

let one_sub_alpha = T::one() - alpha;

let mut opt_mean = None;
Expand Down Expand Up @@ -42,6 +46,28 @@ where
.collect_trusted()
}

/// To prevent numerical instability (and as a slight optimization), we
/// special-case ``alpha=1``.
fn ewm_mean_alpha_equals_one<I, T>(xs: I, min_periods: usize) -> PrimitiveArray<T>
where
I: IntoIterator<Item = Option<T>>,
I::IntoIter: TrustedLen,
T: Float + NativeType + AddAssign,
{
let mut non_null_count = 0usize;
xs.into_iter()
.map(|opt_x| {
if opt_x.is_some() {
non_null_count += 1;
}
match non_null_count < min_periods {
true => None,
false => opt_x,
}
})
.collect_trusted()
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 4ea2f78

Please sign in to comment.