Repository navigation
Fixing double and triple exponential smoothing algorithms, and adding an accompanying test suite #2629
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
Open
john-on-line27
wants to merge
6
commits into
apache:main
Choose a base branch
from
john-on-line27:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fixing double and triple exponential smoothing algorithms, and adding an accompanying test suite #2629
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cf03eb8
[MINOR] Exception handling for exponential smoothing algorithms
7ca015e
Improved exception handling, commenting, as well as error correction
8892bb3
Completion of correction and testing
14ca4ab
Merge pull request #1 from john-on-line27/feature/ema-utils-tests
john-on-line27 77b517f
Fixing the input values for R functions
0ce1413
[MINOR] Fixing comments regarding Push Request
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.sysds.runtime.util; | ||
|
|
||
| import org.apache.sysds.runtime.matrix.data.MatrixValue; | ||
| import org.apache.sysds.test.AutomatedTestBase; | ||
| import org.apache.sysds.test.TestConfiguration; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import java.util.Arrays; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import java.util.HashMap; | ||
|
|
||
| import static org.apache.sysds.test.TestUtils.readRMatrixFromFS; | ||
| import static org.apache.sysds.runtime.util.EMAUtils.double_exponential_smoothing; | ||
| import static org.apache.sysds.runtime.util.EMAUtils.triple_exponential_smoothing; | ||
| import static org.apache.sysds.runtime.util.EMAUtils.Container; | ||
|
|
||
| public class EMAUtilsTest extends AutomatedTestBase { | ||
| private static final String TEST_NAME = "exponentialSmoothing"; | ||
| private static final String TEST_DIR = "functions/builtin/"; | ||
| private static final String TEST_CLASS_DIR = TEST_DIR + EMAUtilsTest.class.getSimpleName() + "/"; | ||
|
|
||
| private static final double REFERENCE_EPS = 1e-4; | ||
| private static final Double[] TS_DATA = { | ||
| 112., 118., 132., 129., 121., 135., 148., 148., 136., 119., 104., 118., | ||
| 115., 126., 141., 135., 125., 149., 170., 170., 158., 133., 114., 140., | ||
| 145., 150., 178., 163., 172., 178., 199., 199., 184., 162., 146., 166., | ||
| 171., 180., 193., 181., 183., 218., 230., 242., 209., 191., 172., 194., | ||
| 196., 196., 236., 235., 229., 243., 264., 272., 237., 211., 180., 201. | ||
| }; | ||
| @Override | ||
| public void setUp() { | ||
| addTestConfiguration(TEST_NAME, | ||
| new TestConfiguration(TEST_CLASS_DIR, TEST_NAME, new String[] { "R_double", "R_triple", "R_triple_odd" })); | ||
| } | ||
|
|
||
| private static String toRCsv() { | ||
| return Arrays.stream(EMAUtilsTest.TS_DATA) | ||
| .map(String::valueOf) | ||
| .collect(Collectors.joining(",")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDoubleExponentialSmoothing() { | ||
| fullRScriptName = SCRIPT_DIR + TEST_DIR + TEST_NAME + ".R"; | ||
| String resultPath = output("R_double"); | ||
|
|
||
| rCmd = getRCmd("double", resultPath, toRCsv()); | ||
| runRScript(true); | ||
|
|
||
| HashMap<MatrixValue.CellIndex, Double> rOutput = readRMatrixFromFS(resultPath); | ||
| Assert.assertFalse("R output should contain values", rOutput.isEmpty()); | ||
|
|
||
| int numRows = (int) rOutput.keySet().stream().mapToLong(idx -> idx.row).max().orElse(0); | ||
| double[] rFittedValues = new double[numRows]; | ||
| for (int i = 1; i <= numRows; i++) { | ||
| rFittedValues[i - 1] = rOutput.get(new MatrixValue.CellIndex(i, 1)); | ||
| } | ||
|
|
||
|
|
||
| Container javaFittedValues = double_exponential_smoothing(TS_DATA, 0.3, 0.1); | ||
|
|
||
| Assert.assertEquals("Array length mismatch", rFittedValues.length, javaFittedValues.values.length-2); | ||
| for (int i = 0; i < rFittedValues.length; i++) { | ||
| Assert.assertEquals("Mismatch at index " + i, rFittedValues[i], javaFittedValues.values[i+2], REFERENCE_EPS); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testTripleExponentialSmoothing(){ | ||
| int freq = 12; | ||
| fullRScriptName = SCRIPT_DIR + TEST_DIR + TEST_NAME + ".R"; | ||
| String resultPath = output("R_triple"); | ||
|
|
||
| rCmd = getRCmd("triple", resultPath, toRCsv()); | ||
| runRScript(true); | ||
|
|
||
| HashMap<MatrixValue.CellIndex, Double> rOutputTriple = readRMatrixFromFS(resultPath); | ||
| Assert.assertFalse( | ||
| "R output should contain values", | ||
| rOutputTriple.isEmpty() | ||
| ); | ||
|
|
||
| int numRows = (int) rOutputTriple.keySet().stream() | ||
| .mapToLong(idx -> idx.row) | ||
| .max() | ||
| .orElse(0); | ||
|
|
||
| double[] rFittedValues = new double[numRows]; | ||
|
|
||
| for (int i = 0; i < numRows; i++) { | ||
| rFittedValues[i] = rOutputTriple.getOrDefault( | ||
| new MatrixValue.CellIndex(i + 1, 1), 0.0); | ||
| } | ||
|
|
||
| Container javaResult = triple_exponential_smoothing( | ||
| TS_DATA, 0.3, 0.1, 0.2, freq); | ||
|
|
||
| Assert.assertEquals("Array length mismatch", rFittedValues.length, javaResult.values.length - freq); | ||
|
|
||
| for (int i = 0; i < rFittedValues.length; i++) { | ||
| Assert.assertEquals("Mismatch at index " + i, rFittedValues[i], javaResult.values[i + freq], REFERENCE_EPS); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testOddTripleExponentialSmoothing(){ | ||
| int freq = 7; | ||
| fullRScriptName = SCRIPT_DIR + TEST_DIR + TEST_NAME + ".R"; | ||
| String resultPath = output("R_triple_odd"); | ||
|
|
||
| rCmd = getRCmd("odd_triple", resultPath, toRCsv()); | ||
| runRScript(true); | ||
|
|
||
| HashMap<MatrixValue.CellIndex, Double> rOutputTriple = readRMatrixFromFS(resultPath); | ||
| Assert.assertFalse( | ||
| "R output should contain values", | ||
| rOutputTriple.isEmpty() | ||
| ); | ||
|
|
||
| int numRows = (int) rOutputTriple.keySet().stream() | ||
| .mapToLong(idx -> idx.row) | ||
| .max() | ||
| .orElse(0); | ||
|
|
||
| double[] rFittedValues = new double[numRows]; | ||
|
|
||
| for (int i = 0; i < numRows; i++) { | ||
| rFittedValues[i] = rOutputTriple.getOrDefault( | ||
| new MatrixValue.CellIndex(i + 1, 1), 0.0); | ||
| } | ||
|
|
||
| Container javaResult = triple_exponential_smoothing( | ||
| TS_DATA, 0.3, 0.1, 0.2, freq); | ||
|
|
||
| Assert.assertEquals("Array length mismatch", rFittedValues.length, javaResult.values.length - freq); | ||
|
|
||
| for (int i = 0; i < rFittedValues.length; i++) { | ||
| Assert.assertEquals("Mismatch at index " + i, rFittedValues[i], javaResult.values[i + freq], REFERENCE_EPS); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we change this test to perform exponential smoothing by calling a DML script rather than calling the Java method directly. Examples for such tests can be found in
BuiltinPowerTransformTest.javaandBuiltinEMATest.java.Also, what is the difference between this test class and the
BuiltinEMATest.java. Can we merge these two test classes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Originally I could not find the test files. I can merge the two test files, however, the current test functions in BuiltinEMATest.java are failing, and since the scope of the thesis does not overlap with the subjects of the tests, it will be unlikely that I will have the time to fix them. @ywcb00
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the clarification. No worries, I will combine (and fix) the two test files before merging them to the main branch. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ywcb00 Let me push the other fixes, and then that would be great!