Path: blob/main/crates/polars-arrow/src/legacy/kernels/ewm/mod.rs
6940 views
mod average;1mod variance;23use std::hash::{Hash, Hasher};45pub use average::*;6#[cfg(feature = "serde")]7use serde::{Deserialize, Serialize};8pub use variance::*;910#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]11#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]12#[derive(Debug, Copy, Clone, PartialEq)]13#[must_use]14pub struct EWMOptions {15pub alpha: f64,16pub adjust: bool,17pub bias: bool,18pub min_periods: usize,19pub ignore_nulls: bool,20}2122impl Default for EWMOptions {23fn default() -> Self {24Self {25alpha: 0.5,26adjust: true,27bias: false,28min_periods: 1,29ignore_nulls: true,30}31}32}3334impl Hash for EWMOptions {35fn hash<H: Hasher>(&self, state: &mut H) {36self.alpha.to_bits().hash(state);37self.adjust.hash(state);38self.bias.hash(state);39self.min_periods.hash(state);40self.ignore_nulls.hash(state);41}42}4344impl EWMOptions {45pub fn and_min_periods(mut self, min_periods: usize) -> Self {46self.min_periods = min_periods;47self48}49pub fn and_adjust(mut self, adjust: bool) -> Self {50self.adjust = adjust;51self52}53pub fn and_span(mut self, span: usize) -> Self {54assert!(span >= 1);55self.alpha = 2.0 / (span as f64 + 1.0);56self57}58pub fn and_half_life(mut self, half_life: f64) -> Self {59assert!(half_life > 0.0);60self.alpha = 1.0 - (-(2.0f64.ln()) / half_life).exp();61self62}63pub fn and_com(mut self, com: f64) -> Self {64assert!(com > 0.0);65self.alpha = 1.0 / (1.0 + com);66self67}68pub fn and_ignore_nulls(mut self, ignore_nulls: bool) -> Self {69self.ignore_nulls = ignore_nulls;70self71}72}7374#[cfg(test)]75macro_rules! assert_allclose {76($xs:expr, $ys:expr, $tol:expr) => {77assert!(78$xs.iter()79.zip($ys.iter())80.map(|(x, z)| {81match (x, z) {82(Some(a), Some(b)) => (a - b).abs() < $tol,83(None, None) => true,84_ => false,85}86})87.fold(true, |acc, b| acc && b)88);89};90}91#[cfg(test)]92pub(crate) use assert_allclose;939495