Skip to content

fix financial/exponential_moving_average: seed uses window_size values, not window_size+1 - #15453

Open
aashish254 wants to merge 1 commit into
TheAlgorithms:masterfrom
aashish254:fix/15449-ema-window-size-1
Open

aashish254 wants to merge 1 commit into
TheAlgorithms:masterfrom
aashish254:fix/15449-ema-window-size-1

Conversation

@aashish254

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Fixes #15449

What changes

One guard in financial/exponential_moving_average.py:52: if i <= window_size: → if i < window_size:, plus a window_size=1 doctest as the witness.

The seed branch ran for window_size + 1 iterations before the EMA recurrence started, so the first EMA update was skipped. With window_size=1 (alpha = 2 / (1 + 1) = 1) the documented recurrence must echo each input unchanged, but the seed averaged the first two prices into 15.0:

before:  [10.0, 15.0, 30.0]
after:   [10.0, 20.0, 30.0]

The existing window_size=3 doctest is unchanged: at alpha=0.5 the seed and EMA branches are arithmetically identical at index 3, so the sequence still equals (2, 3.5, 3.25, 5.725, 5.8625, 7.43125, 8.715625).

The new doctest is the only test change; it is the regression witness for the one-line code fix, so code and doctest land together.

Testing

$ python3 -m doctest financial/exponential_moving_average.py -v
2 passed and 0 failed.

Mutation check — reverting only the guard back to <= makes the new doctest fail with the exact reported values, confirming it is a real witness and not a tautology:

Failed example:
    tuple(exponential_moving_average(iter([10.0, 20.0, 30.0]), 1))
Expected:
    (10.0, 20.0, 30.0)
Got:
    (10.0, 15.0, 30.0)

Restoring the guard re-passes all doctests.

…s, 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 TheAlgorithms#15449
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Sep 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

exponential_moving_average returns an incorrect second value when window_size=1

1 participant