Skip to content

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

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

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

Conversation

@aashish254

@aashish254 aashish254 commented Sep 27, 2026 •

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, 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 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, so it is a real witness, 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 the awaiting reviews This PR is ready to be reviewed label Sep 27, 2026
@algorithms-keeper

Copy link
Copy Markdown

Closing this pull request as invalid

@aashish254, this pull request is being closed as none of the checkboxes have been marked. It is important that you go through the checklist and mark the ones relevant to this pull request. Please read the Contributing guidelines.

If you're facing any problem on how to mark a checkbox, please read the following instructions:

  • Read a point one at a time and think if it is relevant to the pull request or not.
  • If it is, then mark it by putting a x between the square bracket like so: [x]

NOTE: Only [x] is supported so if you have put any other letter or symbol between the brackets, that will be marked as invalid. If that is the case then please open a new pull request with the appropriate changes.

@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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