#15449·Python

exponential_moving_average returns an incorrect second value when window_size=1

Author: AliGaffarToksoyCreated Sep 27, 2026Updated Sep 27, 2026
Labelsbug

Repository commit

7f81fcd68c195ce71534de09d1b69d2b61665902

Python version (python --version)

Python 3.14.7

Dependencies version (pip freeze)

No third-party dependencies are required for this reproduction; the function uses only the Python standard library.

Expected behavior

For window_size=1, the documented smoothing factor is:

alpha = 2 / (1 + window_size) = 1

Therefore, the documented recurrence should return each input value unchanged.

For input [10.0, 20.0, 30.0], the expected output is:

[10.0, 20.0, 30.0]

Actual behavior

The function returns [10.0, 15.0, 30.0], so the second value is incorrect.

Reproduction from the repository root:

python
from financial.exponential_moving_average import exponential_moving_average

actual = list(
    exponential_moving_average(iter([10.0, 20.0, 30.0]), window_size=1)
)
print(actual)
# [10.0, 15.0, 30.0]

assert actual == [10.0, 20.0, 30.0]
# AssertionError

The initialization branch uses if i <= window_size. At i=1 and window_size=1, it calculates (moving_average + stock_price) * 0.5, producing 15.0 instead of applying alpha=1.

The existing doctest uses window_size=3 and passes, but does not cover this case.

The unmodified source file at the reported commit was tested with Python 3.14.7.