-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from pola-rs/ewm-time
Add Ewma_by_time
- Loading branch information
Showing
8 changed files
with
268 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use polars::prelude::*; | ||
use polars_arrow::array::PrimitiveArray; | ||
use pyo3_polars::export::polars_core::export::num::Pow; | ||
|
||
pub(crate) fn impl_ewma_by_time_float( | ||
times: &Int64Chunked, | ||
values: &Float64Chunked, | ||
halflife: i64, | ||
adjust: bool, | ||
time_unit: TimeUnit, | ||
) -> Float64Chunked { | ||
let mut out = Vec::with_capacity(times.len()); | ||
if values.is_empty() { | ||
return Float64Chunked::full_null("", times.len()); | ||
} | ||
|
||
let halflife = match time_unit { | ||
TimeUnit::Milliseconds => halflife / 1_000, | ||
TimeUnit::Microseconds => halflife, | ||
TimeUnit::Nanoseconds => halflife * 1_000, | ||
}; | ||
|
||
let mut prev_time: i64 = times.get(0).unwrap(); | ||
let mut prev_result = values.get(0).unwrap(); | ||
let mut prev_alpha = 0.0; | ||
out.push(Some(prev_result)); | ||
let _ = values | ||
.iter() | ||
.zip(times.iter()) | ||
.skip(1) | ||
.map(|(value, time)| { | ||
match (time, value) { | ||
(Some(time), Some(value)) => { | ||
let delta_time = time - prev_time; | ||
let result: f64; | ||
if adjust { | ||
let alpha = | ||
(prev_alpha + 1.) * Pow::pow(0.5, delta_time as f64 / halflife as f64); | ||
result = (value + alpha * prev_result) / (1. + alpha); | ||
prev_alpha = alpha; | ||
} else { | ||
// equivalent to: | ||
// alpha = exp(-delta_time*ln(2) / halflife) | ||
prev_alpha = (0.5_f64).powf(delta_time as f64 / halflife as f64); | ||
result = (1. - prev_alpha) * value + prev_alpha * prev_result; | ||
} | ||
prev_time = time; | ||
prev_result = result; | ||
out.push(Some(result)); | ||
} | ||
_ => out.push(None), | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
let arr = PrimitiveArray::<f64>::from(out); | ||
Float64Chunked::from(arr) | ||
} | ||
|
||
pub(crate) fn impl_ewma_by_time( | ||
times: &Int64Chunked, | ||
values: &Series, | ||
halflife: i64, | ||
adjust: bool, | ||
time_unit: TimeUnit, | ||
) -> Series { | ||
match values.dtype() { | ||
DataType::Float64 => { | ||
let values = values.f64().unwrap(); | ||
impl_ewma_by_time_float(times, values, halflife, adjust, time_unit).into_series() | ||
} | ||
DataType::Int64 | DataType::Int32 => { | ||
let values = values.cast(&DataType::Float64).unwrap(); | ||
let values = values.f64().unwrap(); | ||
impl_ewma_by_time_float(times, values, halflife, adjust, time_unit).into_series() | ||
} | ||
DataType::Float32 => { | ||
// todo: preserve Float32 in this case | ||
let values = values.cast(&DataType::Float64).unwrap(); | ||
let values = values.f64().unwrap(); | ||
impl_ewma_by_time_float(times, values, halflife, adjust, time_unit).into_series() | ||
} | ||
dt => panic!("Expected values to be signed numeric, got {:?}", dt), | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import polars as pl | ||
from polars.testing import assert_frame_equal | ||
import polars_xdt as xdt | ||
from datetime import datetime, timedelta | ||
|
||
|
||
def test_ewma_by_time(): | ||
df = pl.DataFrame( | ||
{ | ||
"values": [0.0, 1, 2, None, 4], | ||
"times": [ | ||
datetime(2020, 1, 1), | ||
datetime(2020, 1, 3), | ||
datetime(2020, 1, 10), | ||
datetime(2020, 1, 15), | ||
datetime(2020, 1, 17), | ||
], | ||
} | ||
) | ||
result = df.select( | ||
ewma=xdt.ewma_by_time( | ||
"values", times="times", halflife=timedelta(days=4) | ||
), | ||
) | ||
expected = pl.DataFrame( | ||
{ | ||
"ewma": [ | ||
0.0, | ||
0.585786437626905, | ||
1.52388878049859, | ||
None, | ||
3.2336858398518338, | ||
] | ||
} | ||
) | ||
assert_frame_equal(result, expected) |