Skip to content

Commit

Permalink
refactor(rust): Add remove_one to VarState for use in rolling variance
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp committed Feb 27, 2025
1 parent c1e6be9 commit 960c6f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion crates/polars-compute/src/var_cov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl VarState {
}
}

pub fn add_one(&mut self, x: f64) {
pub fn insert_one(&mut self, x: f64) {
// Just a specialized version of
// self.combine(&Self { weight: 1.0, mean: x, dp: 0.0 })
let new_weight = self.weight + 1.0;
Expand All @@ -72,6 +72,17 @@ impl VarState {
self.mean = new_mean;
}

pub fn remove_one(&mut self, x: f64) {
// Just a specialized version of
// self.combine(&Self { weight: -1.0, mean: x, dp: 0.0 })
let new_weight = self.weight - 1.0;
let delta_mean = self.mean - x;
let new_mean = self.mean - delta_mean / new_weight;
self.dp += (new_mean - x) * delta_mean;
self.weight = new_weight;
self.mean = new_mean;
}

pub fn combine(&mut self, other: &Self) {
if other.weight == 0.0 {
return;
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-expr/src/reduce/var_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<T: PolarsNumericType> Reducer for VarStdReducer<T> {
#[inline(always)]
fn reduce_one(&self, a: &mut Self::Value, b: Option<T::Native>, _seq_id: u64) {
if let Some(x) = b {
a.add_one(x.as_());
a.insert_one(x.as_());
}
}

Expand Down

0 comments on commit 960c6f9

Please sign in to comment.