Skip to content

Commit

Permalink
fix trima smoothing function (#105)
Browse files Browse the repository at this point in the history
wrong operator that just so happened to produce expected results for
test cases.
  • Loading branch information
chungg authored Sep 20, 2024
1 parent 9a8b1f4 commit 6529f11
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/smooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ pub fn lrf<T: ToPrimitive>(data: &[T], window: usize) -> impl Iterator<Item = f6
/// ```
pub fn trima<T: ToPrimitive>(data: &[T], window: usize) -> impl Iterator<Item = f64> + '_ {
let win1 = window.div_ceil(2);
let win2 = if window & 2 == 0 { win1 + 1 } else { win1 };
let win2 = if window % 2 == 0 { win1 + 1 } else { win1 };
sma(&sma(data, win1).collect::<Vec<f64>>(), win2)
.collect::<Vec<f64>>()
.into_iter()
Expand Down
28 changes: 28 additions & 0 deletions tests/ma_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,34 @@ fn test_trima_even() {
],
&result
));
let result = smooth::trima(&stats.close, 14).collect::<Vec<_>>();
assert_eq!(stats.close.len(), result.len());
assert_eq!(
&[
49.236071927206865,
48.14642906188965,
46.93589333125524,
45.86107185908727,
44.97500031335014,
44.23750019073486,
43.55553579330444,
42.89839288166591,
42.35678570611136,
41.95571429388863,
41.68125002724784,
41.63321440560477,
41.791250228881836,
42.07142884390695,
42.441786016736714,
42.94375031335014,
43.4982145854405,
44.100357396262034,
44.75000020435878,
45.37928588049752,
46.01383944920131
],
&result[14 - 1..]
);
}

#[test]
Expand Down

0 comments on commit 6529f11

Please sign in to comment.