Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds exponential moving average algorithm #10273

Merged
merged 8 commits into from
Oct 12, 2023

Conversation

PoojanSmart
Copy link
Contributor

@PoojanSmart PoojanSmart commented Oct 11, 2023

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • 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".

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 11, 2023
Comment on lines 10 to 15


def exponential_moving_average(series: list[float], window_size: int) -> list[float]:
"""
Returns the exponential moving average of the given array list
>>> exponential_moving_average([2, 5, 3, 8.2, 6, 9, 10], 3)
Copy link
Member

@cclauss cclauss Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The financial calculations seem artificial and trivial when they are supplied with all the data upfront.

Could we convert series: list[float] --> series: Iterable[float] so that we could continuously create/add to an EMA as new data came in from some generator expression? We will no longer be able to do len(series) but will other issues arise?

Suggested change
def exponential_moving_average(series: list[float], window_size: int) -> list[float]:
"""
Returns the exponential moving average of the given array list
>>> exponential_moving_average([2, 5, 3, 8.2, 6, 9, 10], 3)
from collections.abc import Iterable
def exponential_moving_average(series: Iteratable[float], window_size: int) -> list[float]:
"""
Returns the exponential moving average of the given array list
>>> exponential_moving_average(iter([2, 5, 3, 8.2, 6, 9, 10]), 3)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of return list[float] could we consider yielding floats as we finish windows?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion!
I have added generator as parameter instead of an iterator which acts as mock for file or data stream.

financial/exponential_moving_average.py Outdated Show resolved Hide resolved
@algorithms-keeper algorithms-keeper bot added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Oct 11, 2023
@algorithms-keeper algorithms-keeper bot added require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Oct 12, 2023
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.


doctest.testmod()

def test_gen_func(arr: list[float]):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file financial/exponential_moving_average.py, please provide doctest for the function test_gen_func

Please provide return type hint for the function: test_gen_func. If the function does not return a value, please provide the type hint as: def function() -> None:

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels Oct 12, 2023
@algorithms-keeper algorithms-keeper bot removed require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Oct 12, 2023
t = 0

# Exponential average at timestamp t
st = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
st = None
st = 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@@ -0,0 +1,72 @@
"""
Calculates exponential moving average (EMA) on the series of numbers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Active tense

Suggested change
Calculates exponential moving average (EMA) on the series of numbers
Calculate the exponential moving average (EMA) on the series of numbers.

"""
Calculates exponential moving average (EMA) on the series of numbers
Wikipedia Reference: https://en.wikipedia.org/wiki/Exponential_smoothing
Reference: https://www.investopedia.com/terms/e/ema.asp#toc-what-is-an-exponential-moving-average-ema
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines of Python code should be max 88 characters so let's not overdo it.

Suggested change
Reference: https://www.investopedia.com/terms/e/ema.asp#toc-what-is-an-exponential-moving-average-ema
https://www.investopedia.com/terms/e/ema.asp#toc-what-is-an-exponential-moving-average-ema

"""
Returns the generator which generates exponential moving average of the given
series generator
>>> list(exponential_moving_average((ele for ele in [2, 5, 3, 8.2, 6, 9, 10]), 3))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just easier to read...

Suggested change
>>> list(exponential_moving_average((ele for ele in [2, 5, 3, 8.2, 6, 9, 10]), 3))
>>> list(exponential_moving_average(iter([2, 5, 3, 8.2, 6, 9, 10]), 3))

alpha = 2 / (1 + window_size)

# Defining timestamp t
t = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t = 0
timestamp = 0

Or maybe even better, ticks.

financial/exponential_moving_average.py Show resolved Hide resolved
t = 0

# Exponential average at timestamp t
st = 0.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
st = 0.0
exponential_average = 0.0



def exponential_moving_average(
series_generator: Iterator[float], window_size: int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just plain series is fine.

Suggested change
series_generator: Iterator[float], window_size: int
series: Iterator[float], window_size: int

Comment on lines 67 to 69
test_generator = (ele for ele in test_series)
test_window_size = 3
result = exponential_moving_average(test_generator, test_window_size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test_generator = (ele for ele in test_series)
test_window_size = 3
result = exponential_moving_average(test_generator, test_window_size)
test_window_size = 3
result = exponential_moving_average(series=iter(test_series), window_size=test_window_size)

financial/exponential_moving_average.py Outdated Show resolved Hide resolved
@algorithms-keeper algorithms-keeper bot added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Oct 12, 2023
@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels Oct 12, 2023
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Oct 12, 2023
@cclauss cclauss merged commit 7ea8129 into TheAlgorithms:master Oct 12, 2023
@cclauss
Copy link
Member

cclauss commented Oct 12, 2023

Final edits: ce8ecce

@cclauss cclauss mentioned this pull request Oct 15, 2023
14 tasks
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* Adds exponential moving average algorithm

* code clean up

* spell correction

* Modifies I/O types of function

* Replaces generator function

* Resolved mypy type error

* readibility of code and documentation

* Update exponential_moving_average.py

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants