Skip to content

[MINOR] Fix exponential smoothing algorithms - #2636

Open
ywcb00 wants to merge 8 commits into
apache:mainfrom
ywcb00:pr-2629
Open

ywcb00 wants to merge 8 commits into
apache:mainfrom
ywcb00:pr-2629

Conversation

@ywcb00

@ywcb00 ywcb00 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

This is a duplicate of #2629 for merging.
This PR fixes some flaws of exponential smoothing and modifies the corresponding unit tests to compare against R as a reference result.

@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 63.41463% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.15%. Comparing base (aac5249) to head (098e214).

Files with missing lines Patch % Lines
...n/java/org/apache/sysds/runtime/util/EMAUtils.java 63.41% 6 Missing and 9 partials ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##               main    #2636       +/-   ##
=============================================
- Coverage     76.59%   72.15%    -4.44%     
- Complexity        0    51171    +51171     
=============================================
  Files           458     2105     +1647     
  Lines         31204   230012   +198808     
  Branches          0    38665    +38665     
=============================================
+ Hits          23901   165970   +142069     
- Misses         7303    52842    +45539     
- Partials          0    11200    +11200     
Flag Coverage Δ
java 71.46% <63.41%> (?)
python 76.59% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Owen Braun and others added 8 commits November 17, 2026 09:19
Previously, the variables which created the exponential smoothing had no validation, so a mistake or malicious actor could easily break the algorithm by using an invalid alpha, beta or gamma. In order to fix this issue, I created a helper function to validate the range of the smoothing  variables. This function is optimal, because it lends itself to reuse amongst simple, double and exponential smoothing.
Firstly, the exception handling, which was previously in the main function, was moved into the helper functions. This was done to fit with best practices in exception handling.

Secondly, a new helper function was implemented, so that the frequencies being put into the triple exponential are not zero or smaller, as a lower bound, or larger than half of the size of the incoming data array (as the algorithm uses double the size of the frequency to initialize the variables). The first aspect of this test is trivially beneficial, as it makes sure that no invalid values can be input into the algorithm. The second one is also best practice for this specific instance of the algorithm, as it currently fits to the initialization process of the TES algorithm, but is not applicable to all instances of TES.

Thirdly, an error was found in the triple exponential smoothing algorithm, where the application of the centered moving average filter would result in an indexOutOfBoundsException for frequencies with an odd number, as the for loop was incorrectly declared. The downside to this is the assumption that the current workflow for the initialization of the starting variables is either valid or optimal.

Finally, I have added comments to the exponential smoothing algorithms, so that future contributors have a better understanding of what is happening in underneath the hood.
In order to ensure the full correctness of the Java double and triple exponential smoothing algorithms, the current Java file is being tested by a new EMAUtils.java file, which then proceeds to get reference results from R. The reason why this was the option taken, was that there had been many issues when running the original code, so testing was needed. This includes three main cases, the regular double exponential smoothing algorithm, as well as the odd and even frequency cases for triple exponential smoothing. The reason why I split the last two, is due to the centered moving average used for the initialization, there is two different workflows for the even and odd cases, and in previous iterations of the program, the odd case would crash due to an exception. The reasoning behind the JUnit/R set up comes from the fact that R has prebuilt DES/TES functions, so the correctness of the R reference results can be assumed.

The downsides to this approach are two-fold. Firstly, the sample of test data is very small. This is so that the testing does not last a long amount of time, however some may say the length is insufficient. Secondly, the inbuilt reference functions seek to find the optimal alpha, beta and gamma to minimize the RMSE, but since this is irrelevant to what is being tested, it slows down the actual testing.

Assisted by: AI
AI contributions include the generation of test data, as well as debugging the connection between the JUnit tests and the R tests. The specific AI used was ChatGPT, by OpenAI, and the specific model was GPT-6.1 Sol.
Initially the default had been set to search for the optimal alpha, beta and gamma values, the testing was much slower than needed, now I have just added a fixed value for all values across the JUnit and R tests.
Firstly, formatting changes have been made, including indenting and new line differences in EMAUtils, as well as ending the R file with a new line. Secondly, the imports in EMAUtilsTest has been made more specific, so unneccesary components are not used.  Thirdly, the data is fed from the JUnit file into the R test, so that the data remains consistent amongst both files.
…ference baseline

fix(main/runtime/util/EMAUtils.java): fix minor inconsistencies (e.g., remove copied starting data before returning result)
…searching for the best parameters in exponential smoothing function

fix(test/functions/builtin/BuiltinEMATest.java): remove test case with missing value in the time series data due to missing reference

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

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant