Repository navigation
Commit cbd04e9
committed
fix financial/exponential_moving_average: seed uses window_size values, not window_size+1
The initialization branch guarded with `if i <= window_size`, so it ran the
SMA-seed branch for window_size+1 iterations before switching to the EMA
recurrence. With window_size=1 (alpha=1) the documented recurrence should
echo each input unchanged, but the seed branch averaged the first two
prices into 15.0 instead of yielding 20.0, so
exponential_moving_average(iter([10,20,30]), 1) returned [10, 15, 30].
Change the guard to `i < window_size`. The existing window_size=3 doctest
is unchanged because alpha=0.5 makes the seed and EMA branches arithmetically
identical at that specific index; add a window_size=1 doctest as the witness.
Fixes #154491 parent 7f81fcd commit cbd04e9
1 file changed
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
22 | 24 | | |
23 | 25 | | |
24 | 26 | | |
| |||
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
52 | | - | |
| 54 | + | |
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
| |||
0 commit comments