Path: blob/main/crates/polars-compute/src/ewm/options.rs
7884 views
use std::hash::{Hash, Hasher};12#[cfg(feature = "serde")]3use serde::{Deserialize, Serialize};45#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]6#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]7#[derive(Debug, Copy, Clone, PartialEq)]8#[must_use]9pub struct EWMOptions {10pub alpha: f64,11pub adjust: bool,12pub bias: bool,13pub min_periods: usize,14pub ignore_nulls: bool,15}1617impl Default for EWMOptions {18fn default() -> Self {19Self {20alpha: 0.5,21adjust: true,22bias: false,23min_periods: 1,24ignore_nulls: true,25}26}27}2829impl Hash for EWMOptions {30fn hash<H: Hasher>(&self, state: &mut H) {31self.alpha.to_bits().hash(state);32self.adjust.hash(state);33self.bias.hash(state);34self.min_periods.hash(state);35self.ignore_nulls.hash(state);36}37}3839impl EWMOptions {40pub fn and_min_periods(mut self, min_periods: usize) -> Self {41self.min_periods = min_periods;42self43}44pub fn and_adjust(mut self, adjust: bool) -> Self {45self.adjust = adjust;46self47}48pub fn and_span(mut self, span: usize) -> Self {49assert!(span >= 1);50self.alpha = 2.0 / (span as f64 + 1.0);51self52}53pub fn and_half_life(mut self, half_life: f64) -> Self {54assert!(half_life > 0.0);55self.alpha = 1.0 - (-(2.0f64.ln()) / half_life).exp();56self57}58pub fn and_com(mut self, com: f64) -> Self {59assert!(com > 0.0);60self.alpha = 1.0 / (1.0 + com);61self62}63pub fn and_ignore_nulls(mut self, ignore_nulls: bool) -> Self {64self.ignore_nulls = ignore_nulls;65self66}67}686970