Skip to content

Adds exponential moving average algorithm - #10273

Merged
cclauss merged 8 commits into
TheAlgorithms:masterfrom
PoojanSmart:master
Oct 12, 2023
Merged

Adds exponential moving average algorithm#10273
cclauss merged 8 commits into
TheAlgorithms:masterfrom
PoojanSmart:master

Conversation

@PoojanSmart

@PoojanSmartPoojanSmart commented Oct 11, 2023

Copy link
Copy Markdown
Contributor

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-keeperalgorithms-keeperBot 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)

@cclausscclaussOct 11, 2023

Copy link
Copy Markdown
Member

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
defexponential_moving_average(series: list[float], window_size: int) ->list[float]:
"""
Returnstheexponentialmovingaverageofthegivenarraylist
>>>exponential_moving_average([2, 5, 3, 8.2, 6, 9, 10], 3)
fromcollections.abcimportIterable
defexponential_moving_average(series: Iteratable[float], window_size: int) ->list[float]:
"""
Returnstheexponentialmovingaverageofthegivenarraylist
>>>exponential_moving_average(iter([2, 5, 3, 8.2, 6, 9, 10]), 3)

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

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.

Comment threadfinancial/exponential_moving_average.py Outdated
@algorithms-keeperalgorithms-keeperBot 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-keeperalgorithms-keeperBot added require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Oct 12, 2023

@algorithms-keeperalgorithms-keeperBot left a comment

Copy link
Copy Markdown

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]):

Copy link
Copy Markdown

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-keeperalgorithms-keeperBot 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-keeperalgorithms-keeperBot 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
Copy Markdown
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
Copy Markdown
ContributorAuthor

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
Copy Markdown
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
Calculatesexponentialmovingaverage (EMA) ontheseriesofnumbers
Calculatetheexponentialmovingaverage (EMA) ontheseriesofnumbers.

"""
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
Copy Markdown
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
Copy Markdown
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((eleforelein[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
Copy Markdown
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.

Comment threadfinancial/exponential_moving_average.py
t = 0

# Exponential average at timestamp t
st = 0.0

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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= (eleforeleintest_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)

Comment threadfinancial/exponential_moving_average.py Outdated
@algorithms-keeperalgorithms-keeperBot 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-keeperalgorithms-keeperBot 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-keeperalgorithms-keeperBot removed the awaiting reviews This PR is ready to be reviewed label Oct 12, 2023
@cclauss
cclauss merged commit 7ea8129 into TheAlgorithms:masterOct 12, 2023
@cclauss

Copy link
Copy Markdown
Member

Final edits: ce8ecce

@cclausscclauss mentioned this pull request Oct 15, 2023
14 tasks
@isidroasisidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for freeto 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

@PoojanSmart@cclauss