-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] Implement standard deviation (#3005)
# Overview - Add a standard deviation function - similar in implementation to how `AggExpr::count` and `AggExpr::Mean` work ## Notes Implementations differ slightly for non- vs multi- partitioned based dataframes: 1. The non-partitioned implementation uses the simple, naive approach, derived from definition of stddev (i.e., `stddev(X) = sqrt(sum((x_i - mean(X))^2) / N)`). 2. The multi-partitioned implementation calculates `stddev(X) = sqrt(E(X^2) - E(X)^2)`.
- Loading branch information
Raunak Bhagat
authored
Oct 8, 2024
1 parent
f995792
commit 64b8699
Showing
39 changed files
with
1,190 additions
and
759 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,27 @@ | ||
use std::sync::Arc; | ||
|
||
use arrow2::array::PrimitiveArray; | ||
use common_error::DaftResult; | ||
|
||
use super::{as_arrow::AsArrow, DaftCountAggable, DaftMeanAggable, DaftSumAggable}; | ||
use crate::{array::ops::GroupIndices, count_mode::CountMode, datatypes::*}; | ||
impl DaftMeanAggable for &DataArray<Float64Type> { | ||
type Output = DaftResult<DataArray<Float64Type>>; | ||
use crate::{ | ||
array::ops::{DaftMeanAggable, GroupIndices}, | ||
datatypes::*, | ||
utils::stats, | ||
}; | ||
|
||
fn mean(&self) -> Self::Output { | ||
let sum_value = DaftSumAggable::sum(self)?.as_arrow().value(0); | ||
let count_value = DaftCountAggable::count(self, CountMode::Valid)? | ||
.as_arrow() | ||
.value(0); | ||
|
||
let result = match count_value { | ||
0 => None, | ||
count_value => Some(sum_value / count_value as f64), | ||
}; | ||
let arrow_array = Box::new(arrow2::array::PrimitiveArray::from([result])); | ||
impl DaftMeanAggable for DataArray<Float64Type> { | ||
type Output = DaftResult<Self>; | ||
|
||
DataArray::new( | ||
Arc::new(Field::new(self.field.name.clone(), DataType::Float64)), | ||
arrow_array, | ||
) | ||
fn mean(&self) -> Self::Output { | ||
let stats = stats::calculate_stats(self)?; | ||
let data = PrimitiveArray::from([stats.mean]).boxed(); | ||
let field = Arc::new(Field::new(self.field.name.clone(), DataType::Float64)); | ||
Self::new(field, data) | ||
} | ||
|
||
fn grouped_mean(&self, groups: &GroupIndices) -> Self::Output { | ||
use arrow2::array::PrimitiveArray; | ||
let sum_values = self.grouped_sum(groups)?; | ||
let count_values = self.grouped_count(groups, CountMode::Valid)?; | ||
assert_eq!(sum_values.len(), count_values.len()); | ||
let mean_per_group = sum_values | ||
.as_arrow() | ||
.values_iter() | ||
.zip(count_values.as_arrow().values_iter()) | ||
.map(|(s, c)| match (s, c) { | ||
(_, 0) => None, | ||
(s, c) => Some(s / (*c as f64)), | ||
}); | ||
let mean_array = Box::new(PrimitiveArray::from_trusted_len_iter(mean_per_group)); | ||
Ok(DataArray::from((self.field.name.as_ref(), mean_array))) | ||
let grouped_means = stats::grouped_stats(self, groups)?.map(|(stats, _)| stats.mean); | ||
let data = Box::new(PrimitiveArray::from_iter(grouped_means)); | ||
Ok(Self::from((self.field.name.as_ref(), data))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use arrow2::array::PrimitiveArray; | ||
use common_error::DaftResult; | ||
|
||
use crate::{ | ||
array::{ | ||
ops::{DaftStddevAggable, GroupIndices}, | ||
DataArray, | ||
}, | ||
datatypes::Float64Type, | ||
utils::stats, | ||
}; | ||
|
||
impl DaftStddevAggable for DataArray<Float64Type> { | ||
type Output = DaftResult<Self>; | ||
|
||
fn stddev(&self) -> Self::Output { | ||
let stats = stats::calculate_stats(self)?; | ||
let values = self.into_iter().flatten().copied(); | ||
let stddev = stats::calculate_stddev(stats, values); | ||
let field = self.field.clone(); | ||
let data = PrimitiveArray::<f64>::from([stddev]).boxed(); | ||
Self::new(field, data) | ||
} | ||
|
||
fn grouped_stddev(&self, groups: &GroupIndices) -> Self::Output { | ||
let grouped_stddevs_iter = stats::grouped_stats(self, groups)?.map(|(stats, group)| { | ||
let values = group.iter().filter_map(|&index| self.get(index as _)); | ||
stats::calculate_stddev(stats, values) | ||
}); | ||
let field = self.field.clone(); | ||
let data = PrimitiveArray::<f64>::from_iter(grouped_stddevs_iter).boxed(); | ||
Self::new(field, data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ pub mod arrow; | |
pub mod display; | ||
pub mod dyn_compare; | ||
pub mod identity_hash_set; | ||
pub mod stats; | ||
pub mod supertype; |
Oops, something went wrong.