From 058db4d242a6e2dddb60ebd22300168bfdcccb46 Mon Sep 17 00:00:00 2001 From: Owen Braun Date: Tue, 29 Sep 2026 16:03:21 +0200 Subject: [PATCH 1/8] [MINOR] Exception handling for exponential smoothing algorithms 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. --- .../apache/sysds/runtime/util/EMAUtils.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java b/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java index f6fd1d14294..04effee1f62 100644 --- a/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java +++ b/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java @@ -145,7 +145,27 @@ public Container(Double[] vals, double error) { double rsme; } + static void validateRange(Double... values) { + if (values == null || values.length == 0){ + throw new IllegalArgumentException("No arguments provided"); + } + for (Double value : values) { + if (value > 1 || value < 0) { + throw new IllegalArgumentException( + String.format("The Input %f is out of range [0,1]", value) + ); + } + } + + } public static Container single_exponential_smoothing(Double[] data, Double alpha) { + + try { + validateRange(alpha); + } catch (IllegalArgumentException e) { + System.err.println("Alpha is out of bounds"); + } + int n = data.length; Double[] pred = new Double[n]; @@ -186,6 +206,12 @@ public static Container single_exponential_smoothing(Double[] data, Double alpha } public static Container double_exponential_smoothing(Double[] data, Double alpha, Double beta) { + try { + validateRange(alpha, beta); + } catch (IllegalArgumentException e) { + System.err.println("Alpha is out of bounds"); + } + int n = data.length; ArrayList pred = new ArrayList<>(n-1); @@ -236,6 +262,13 @@ public static Container double_exponential_smoothing(Double[] data, Double alpha public static Container triple_exponential_smoothing(Double[] data, Double alpha, Double beta, Double gamma, Integer freq) { + + try { + validateRange(alpha, beta, gamma); + } catch (IllegalArgumentException e) { + System.err.println("Alpha is out of bounds"); + } + double l = freq * 2; ArrayList start_data = new ArrayList<>(); From caa1967052b270426308519d3a28c41e04943462 Mon Sep 17 00:00:00 2001 From: Owen Braun Date: Tue, 20 Oct 2026 13:30:35 +0200 Subject: [PATCH 2/8] Improved exception handling, commenting, as well as error correction 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. --- .../apache/sysds/runtime/util/EMAUtils.java | 58 +++++++++++++------ 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java b/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java index 04effee1f62..c447a6da797 100644 --- a/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java +++ b/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java @@ -145,27 +145,32 @@ public Container(Double[] vals, double error) { double rsme; } - static void validateRange(Double... values) { + public static void validateRange(Double... values) { if (values == null || values.length == 0){ throw new IllegalArgumentException("No arguments provided"); } for (Double value : values) { if (value > 1 || value < 0) { throw new IllegalArgumentException( - String.format("The Input %f is out of range [0,1]", value) + "Input is out of range [0,1]" ); } } } - public static Container single_exponential_smoothing(Double[] data, Double alpha) { - try { - validateRange(alpha); - } catch (IllegalArgumentException e) { - System.err.println("Alpha is out of bounds"); + public static void validateFrequency(int freq, Double[] data){ + if (freq <= 0){ + throw new IllegalArgumentException("Value too low"); + } else if (2*freq >= data.length) { + throw new ArrayIndexOutOfBoundsException("Frequency too high"); } + } + public static Container single_exponential_smoothing(Double[] data, Double alpha) { + + validateRange(alpha); + int n = data.length; Double[] pred = new Double[n]; @@ -206,18 +211,18 @@ public static Container single_exponential_smoothing(Double[] data, Double alpha } public static Container double_exponential_smoothing(Double[] data, Double alpha, Double beta) { - try { - validateRange(alpha, beta); - } catch (IllegalArgumentException e) { - System.err.println("Alpha is out of bounds"); - } + // Exception handling + validateRange(alpha, beta); + + // Initializing variables int n = data.length; ArrayList pred = new ArrayList<>(n-1); Double[] s = new Double[n-1]; Double[] b = new Double[n-1]; + // Setting up s_0, b_0, y_0 s[0] = data[1]; b[0] = data[1] - data[0]; pred.add(s[0] + b[0]); @@ -228,7 +233,9 @@ public static Container double_exponential_smoothing(Double[] data, Double alpha ArrayList not_missing_pred = new ArrayList<>(); int n_size = 0; + // Iterative updating of the model for (int i = 1; i < n-1; i++) { + // Imputes missing data if (Double.isNaN(data[i + 1])) { val = pred.get(i - 1); } else { @@ -242,6 +249,7 @@ public static Container double_exponential_smoothing(Double[] data, Double alpha pred.add(0, data[0]); + // Initializing the data for calculation of RMSE for (int i = 0; i < data.length; i++) { if (!Double.isNaN(data[i])) { not_missing.add(data[i]); @@ -250,6 +258,7 @@ public static Container double_exponential_smoothing(Double[] data, Double alpha } } + // Calculation of RMSE double sum = .0; for (int i = 0; i < not_missing.size(); i++) { sum += Math.pow(not_missing.get(i) - not_missing_pred.get(i), 2); @@ -263,12 +272,11 @@ public static Container double_exponential_smoothing(Double[] data, Double alpha public static Container triple_exponential_smoothing(Double[] data, Double alpha, Double beta, Double gamma, Integer freq) { - try { - validateRange(alpha, beta, gamma); - } catch (IllegalArgumentException e) { - System.err.println("Alpha is out of bounds"); - } + // Exception handling + validateRange(alpha, beta, gamma); + validateFrequency(freq, data); + //Initialization data double l = freq * 2; ArrayList start_data = new ArrayList<>(); @@ -276,11 +284,15 @@ public static Container triple_exponential_smoothing(Double[] data, Double alpha start_data.add(data[i]); } + // Building a centered moving average for the inititialization of the level variable ArrayList filt = new ArrayList<>(); ArrayList trend = new ArrayList<>(); double len = freq; + // To set up the centered moving average filter, there is two cases, odd and even frequency. + // For the odd frequency, there is a true center point, so the filter weights are the same. + // For the even frequency, there is no center point, therefore, the edge weights are halved. if (freq % 2 == 0) { len = freq - 1; } @@ -294,9 +306,11 @@ public static Container triple_exponential_smoothing(Double[] data, Double alpha filt.add(0.5/freq); } + // Applying the CMA filter to the trend double trend_len = l - filt.size() + 1; - for (int i = 0; i < l - trend_len; i++) { + // Goes through the start data, and sums the average trend over the first to seasons. + for (int i = 0; i < trend_len; i++) { double sum = 0; for (int j = i; j < i + filt.size(); j++) { sum += start_data.get(j) * filt.get(j - i); @@ -305,6 +319,7 @@ public static Container triple_exponential_smoothing(Double[] data, Double alpha trend.add(sum); } + // Removing the trend, to reveal the seasonal pattern int cut = (int) (l - trend.size()) / 2; ArrayList season_tmp = new ArrayList<>(); @@ -315,6 +330,7 @@ public static Container triple_exponential_smoothing(Double[] data, Double alpha Double[] season = new Double[freq]; + // Average by season position for (int i = 0; i < freq; i++) { double combined = 0; if (i + freq < trend.size()) { @@ -326,6 +342,7 @@ public static Container triple_exponential_smoothing(Double[] data, Double alpha season[(i + (freq / 2)) % freq] = combined; } + // Preparing the data for linear regression, in order to initialize level double sum = 0; for (int i = 0; i < season.length; i++) { sum += season[i]; @@ -349,6 +366,8 @@ public static Container triple_exponential_smoothing(Double[] data, Double alpha int n = data.length; + + // Initalize arrays and create first forecast double[] s = new double[n - freq]; s[0] = linreg.intercept(); @@ -371,6 +390,7 @@ public static Container triple_exponential_smoothing(Double[] data, Double alpha int n_size = 0; + // Update the model per data point for (int i = 1; i < n - freq; i++) { if (Double.isNaN(data[i + freq - 1] )) { val = pred.get(i - 1); @@ -389,6 +409,7 @@ public static Container triple_exponential_smoothing(Double[] data, Double alpha pred.add(i, data[i]); } + // Initialize data for the RMSE calculation for (int i = 0; i < data.length; i++) { if (!Double.isNaN(data[i])) { not_missing.add(data[i]); @@ -397,6 +418,7 @@ public static Container triple_exponential_smoothing(Double[] data, Double alpha } } + // Calculate the RMSE sum = .0; for (int i = 0; i < not_missing.size(); i++) { sum += Math.pow(not_missing.get(i) - not_missing_pred.get(i), 2); From 7afe3e86aa5380e2a7ccf59d6e4eecf55d2b9343 Mon Sep 17 00:00:00 2001 From: Owen Braun Date: Sat, 31 Oct 2026 14:10:32 +0100 Subject: [PATCH 3/8] Completion of correction and testing 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. --- .../apache/sysds/runtime/util/EMAUtils.java | 33 ++-- .../sysds/runtime/util/EMAUtilsTest.java | 170 ++++++++++++++++++ .../functions/builtin/exponentialSmoothing.R | 99 ++++++++++ 3 files changed, 291 insertions(+), 11 deletions(-) create mode 100644 src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java create mode 100644 src/test/scripts/functions/builtin/exponentialSmoothing.R diff --git a/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java b/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java index c447a6da797..2e544ee32c9 100644 --- a/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java +++ b/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java @@ -72,7 +72,7 @@ public double coef() { public class EMAUtils { public static FrameBlock exponentialMovingAverageImputation(FrameBlock block, int search_iterations, String mode, - int freq, Double alpha, Double beta, Double gamma) { + int freq, Double alpha, Double beta, Double gamma) { int cols = block.getNumColumns(); int rows = block.getNumRows(); @@ -149,13 +149,13 @@ public static void validateRange(Double... values) { if (values == null || values.length == 0){ throw new IllegalArgumentException("No arguments provided"); } - for (Double value : values) { - if (value > 1 || value < 0) { - throw new IllegalArgumentException( - "Input is out of range [0,1]" - ); - } - } + for (Double value : values) { + if (value > 1 || value < 0) { + throw new IllegalArgumentException( + "Input is out of range [0,1]" + ); + } + } } @@ -215,6 +215,14 @@ public static Container double_exponential_smoothing(Double[] data, Double alpha // Exception handling validateRange(alpha, beta); + if (data == null || data.length < 3) { + throw new IllegalArgumentException("At least three observations are needed to calculate RMSE"); + } + + if (data[0] == null || data[1] == null || Double.isNaN(data[0]) || Double.isNaN(data[1])) { + throw new IllegalArgumentException("The first two observations must not be missing"); + } + // Initializing variables int n = data.length; @@ -244,9 +252,12 @@ public static Container double_exponential_smoothing(Double[] data, Double alpha s[i] = alpha * val + (1 - alpha) * (s[i-1] + b[i-1]); b[i] = beta * (s[i] - s[i-1]) + (1 - beta) * b[i-1]; - pred.add(s[i] + b[i]); - } + if (i < n - 2){ + pred.add(s[i] + b[i]); + } + } + pred.add(0, data[1]); pred.add(0, data[0]); // Initializing the data for calculation of RMSE @@ -270,7 +281,7 @@ public static Container double_exponential_smoothing(Double[] data, Double alpha } public static Container triple_exponential_smoothing(Double[] data, Double alpha, Double beta, - Double gamma, Integer freq) { + Double gamma, Integer freq) { // Exception handling validateRange(alpha, beta, gamma); diff --git a/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java b/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java new file mode 100644 index 00000000000..250f4038e6a --- /dev/null +++ b/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java @@ -0,0 +1,170 @@ +/* + * 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.HashMap; + +import static org.apache.sysds.test.TestUtils.readRMatrixFromFS; +import static org.apache.sysds.runtime.util.EMAUtils.*; + +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" })); + } + + @Test + public void testDoubleExponentialSmoothing() { + fullRScriptName = SCRIPT_DIR + TEST_DIR + TEST_NAME + ".R"; + String resultPath = output("R_double"); + + rCmd = getRCmd("double", resultPath); + runRScript(true); + + HashMap rOutput = readRMatrixFromFS(resultPath); + Assert.assertFalse("R output should contain values", rOutput.isEmpty()); + + // 1. Extract optimized parameters + double rAlpha = rOutput.getOrDefault(new MatrixValue.CellIndex(1, 1),0.0); + double rBeta = rOutput.getOrDefault(new MatrixValue.CellIndex(1, 2),0.0); + + // 2. Extract the entire sequence of fitted values + 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, 3)); + } + + + Container javaFittedValues = double_exponential_smoothing(TS_DATA, rAlpha, rBeta); + + 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); + runRScript(true); + + HashMap rOutputTriple = readRMatrixFromFS(resultPath); + Assert.assertFalse( + "R output should contain values", + rOutputTriple.isEmpty() + ); + + double rAlpha = rOutputTriple.getOrDefault( + new MatrixValue.CellIndex(1, 1), 0.0); + double rBeta = rOutputTriple.getOrDefault( + new MatrixValue.CellIndex(1, 2), 0.0); + double rGamma = rOutputTriple.getOrDefault( + new MatrixValue.CellIndex(1, 3), 0.0); + + 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, 4), 0.0); + } + + Container javaResult = triple_exponential_smoothing( + TS_DATA, rAlpha, rBeta, rGamma, 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); + runRScript(true); + + HashMap rOutputTriple = readRMatrixFromFS(resultPath); + Assert.assertFalse( + "R output should contain values", + rOutputTriple.isEmpty() + ); + + double rAlpha = rOutputTriple.getOrDefault( + new MatrixValue.CellIndex(1, 1), 0.0); + double rBeta = rOutputTriple.getOrDefault( + new MatrixValue.CellIndex(1, 2), 0.0); + double rGamma = rOutputTriple.getOrDefault( + new MatrixValue.CellIndex(1, 3), 0.0); + + 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, 4), 0.0); + } + + Container javaResult = triple_exponential_smoothing( + TS_DATA, rAlpha, rBeta, rGamma, 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); + } + } +} \ No newline at end of file diff --git a/src/test/scripts/functions/builtin/exponentialSmoothing.R b/src/test/scripts/functions/builtin/exponentialSmoothing.R new file mode 100644 index 00000000000..cc51eadae43 --- /dev/null +++ b/src/test/scripts/functions/builtin/exponentialSmoothing.R @@ -0,0 +1,99 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +args <- commandArgs(trailingOnly = TRUE) + +if (length(args) != 2L) { + stop("Expected arguments: ") +} + +mode <- args[1] +output_path <- args[2] + +ts_data <- c( + 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 +) + +if (mode == "double") { + hw_model <- stats::HoltWinters(ts_data, beta = NULL, gamma = FALSE) + fit_vals <- as.numeric(fitted(hw_model)[, "xhat"]) + + res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 3) + res_matrix[, 1] <- hw_model$alpha + res_matrix[, 2] <- hw_model$beta + res_matrix[, 3] <- fit_vals + +} else if (mode == "triple") { + ts_obj <- ts(ts_data, frequency = 12) + + hw_model <- stats::HoltWinters( + ts_obj, + alpha = NULL, + beta = NULL, + gamma = NULL, + seasonal = "additive" + ) + + fit_vals <- as.numeric(fitted(hw_model)[, "xhat"]) + + res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 4) + res_matrix[, 1] <- hw_model$alpha + res_matrix[, 2] <- hw_model$beta + res_matrix[, 3] <- hw_model$gamma + res_matrix[, 4] <- fit_vals + +} else if (mode == "odd_triple") { + ts_obj <- ts(ts_data, frequency = 7) + + hw_model <- stats::HoltWinters( + ts_obj, + alpha = NULL, + beta = NULL, + gamma = NULL, + seasonal = "additive" + ) + + fit_vals <- as.numeric(fitted(hw_model)[, "xhat"]) + + res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 4) + res_matrix[, 1] <- hw_model$alpha + res_matrix[, 2] <- hw_model$beta + res_matrix[, 3] <- hw_model$gamma + res_matrix[, 4] <- fit_vals + + } else { + stop(paste("Unknown smoothing mode:", mode)) +} + +dir.create( + dirname(output_path), + recursive = TRUE, + showWarnings = FALSE +) + +Matrix::writeMM( + Matrix::Matrix(res_matrix, sparse = TRUE), + file = output_path +) \ No newline at end of file From d0248ca9cbebe80c9fe7281d7d1087864ae82df5 Mon Sep 17 00:00:00 2001 From: Owen Braun Date: Sat, 31 Oct 2026 17:40:20 +0100 Subject: [PATCH 4/8] Fixing the input values for R functions 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. --- .../sysds/runtime/util/EMAUtilsTest.java | 31 +++---------- .../functions/builtin/exponentialSmoothing.R | 44 +++++++++---------- 2 files changed, 26 insertions(+), 49 deletions(-) diff --git a/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java b/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java index 250f4038e6a..e0284ffe9f6 100644 --- a/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java +++ b/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java @@ -60,19 +60,14 @@ public void testDoubleExponentialSmoothing() { HashMap rOutput = readRMatrixFromFS(resultPath); Assert.assertFalse("R output should contain values", rOutput.isEmpty()); - // 1. Extract optimized parameters - double rAlpha = rOutput.getOrDefault(new MatrixValue.CellIndex(1, 1),0.0); - double rBeta = rOutput.getOrDefault(new MatrixValue.CellIndex(1, 2),0.0); - - // 2. Extract the entire sequence of fitted values 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, 3)); + rFittedValues[i - 1] = rOutput.get(new MatrixValue.CellIndex(i, 1)); } - Container javaFittedValues = double_exponential_smoothing(TS_DATA, rAlpha, rBeta); + 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++) { @@ -95,13 +90,6 @@ public void testTripleExponentialSmoothing(){ rOutputTriple.isEmpty() ); - double rAlpha = rOutputTriple.getOrDefault( - new MatrixValue.CellIndex(1, 1), 0.0); - double rBeta = rOutputTriple.getOrDefault( - new MatrixValue.CellIndex(1, 2), 0.0); - double rGamma = rOutputTriple.getOrDefault( - new MatrixValue.CellIndex(1, 3), 0.0); - int numRows = (int) rOutputTriple.keySet().stream() .mapToLong(idx -> idx.row) .max() @@ -111,11 +99,11 @@ public void testTripleExponentialSmoothing(){ for (int i = 0; i < numRows; i++) { rFittedValues[i] = rOutputTriple.getOrDefault( - new MatrixValue.CellIndex(i + 1, 4), 0.0); + new MatrixValue.CellIndex(i + 1, 1), 0.0); } Container javaResult = triple_exponential_smoothing( - TS_DATA, rAlpha, rBeta, rGamma, freq); + TS_DATA, 0.3, 0.1, 0.2, freq); Assert.assertEquals("Array length mismatch", rFittedValues.length, javaResult.values.length - freq); @@ -139,13 +127,6 @@ public void testOddTripleExponentialSmoothing(){ rOutputTriple.isEmpty() ); - double rAlpha = rOutputTriple.getOrDefault( - new MatrixValue.CellIndex(1, 1), 0.0); - double rBeta = rOutputTriple.getOrDefault( - new MatrixValue.CellIndex(1, 2), 0.0); - double rGamma = rOutputTriple.getOrDefault( - new MatrixValue.CellIndex(1, 3), 0.0); - int numRows = (int) rOutputTriple.keySet().stream() .mapToLong(idx -> idx.row) .max() @@ -155,11 +136,11 @@ public void testOddTripleExponentialSmoothing(){ for (int i = 0; i < numRows; i++) { rFittedValues[i] = rOutputTriple.getOrDefault( - new MatrixValue.CellIndex(i + 1, 4), 0.0); + new MatrixValue.CellIndex(i + 1, 1), 0.0); } Container javaResult = triple_exponential_smoothing( - TS_DATA, rAlpha, rBeta, rGamma, freq); + TS_DATA, 0.3, 0.1, 0.2, freq); Assert.assertEquals("Array length mismatch", rFittedValues.length, javaResult.values.length - freq); diff --git a/src/test/scripts/functions/builtin/exponentialSmoothing.R b/src/test/scripts/functions/builtin/exponentialSmoothing.R index cc51eadae43..50c30e29121 100644 --- a/src/test/scripts/functions/builtin/exponentialSmoothing.R +++ b/src/test/scripts/functions/builtin/exponentialSmoothing.R @@ -37,51 +37,47 @@ ts_data <- c( ) if (mode == "double") { - hw_model <- stats::HoltWinters(ts_data, beta = NULL, gamma = FALSE) + hw_model <- stats::HoltWinters( + ts_data, + alpha=0.3, + beta = 0.1, + gamma = FALSE) fit_vals <- as.numeric(fitted(hw_model)[, "xhat"]) - res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 3) - res_matrix[, 1] <- hw_model$alpha - res_matrix[, 2] <- hw_model$beta - res_matrix[, 3] <- fit_vals + res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 1) + res_matrix[, 1] <- fit_vals } else if (mode == "triple") { ts_obj <- ts(ts_data, frequency = 12) hw_model <- stats::HoltWinters( ts_obj, - alpha = NULL, - beta = NULL, - gamma = NULL, + alpha = 0.3, + beta = 0.1, + gamma = 0.2, seasonal = "additive" ) fit_vals <- as.numeric(fitted(hw_model)[, "xhat"]) - res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 4) - res_matrix[, 1] <- hw_model$alpha - res_matrix[, 2] <- hw_model$beta - res_matrix[, 3] <- hw_model$gamma - res_matrix[, 4] <- fit_vals + res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 1) + res_matrix[, 1] <- fit_vals } else if (mode == "odd_triple") { ts_obj <- ts(ts_data, frequency = 7) hw_model <- stats::HoltWinters( - ts_obj, - alpha = NULL, - beta = NULL, - gamma = NULL, - seasonal = "additive" - ) + ts_obj, + alpha = 0.3, + beta = 0.1, + gamma = 0.2, + seasonal = "additive" + ) fit_vals <- as.numeric(fitted(hw_model)[, "xhat"]) - res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 4) - res_matrix[, 1] <- hw_model$alpha - res_matrix[, 2] <- hw_model$beta - res_matrix[, 3] <- hw_model$gamma - res_matrix[, 4] <- fit_vals + res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 1) + res_matrix[, 1] <- fit_vals } else { stop(paste("Unknown smoothing mode:", mode)) From f7f8952af493ef9051b4f27362caff49f7a46d61 Mon Sep 17 00:00:00 2001 From: Owen Braun Date: Thu, 5 Nov 2026 17:39:55 +0100 Subject: [PATCH 5/8] [MINOR] Fixing comments regarding Push Request 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. --- .../apache/sysds/runtime/util/EMAUtils.java | 5 ++--- .../sysds/runtime/util/EMAUtilsTest.java | 18 +++++++++++---- .../functions/builtin/exponentialSmoothing.R | 22 +++++++++++-------- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java b/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java index 2e544ee32c9..aa5008b4f3e 100644 --- a/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java +++ b/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java @@ -17,7 +17,7 @@ * under the License. */ - +// Testing for the utils can be found at src\test\java\org\apache\sysds\test\functions\builtin\part1 package org.apache.sysds.runtime.util; import java.util.ArrayList; @@ -71,8 +71,7 @@ public double coef() { public class EMAUtils { - public static FrameBlock exponentialMovingAverageImputation(FrameBlock block, int search_iterations, String mode, - int freq, Double alpha, Double beta, Double gamma) { + public static FrameBlock exponentialMovingAverageImputation(FrameBlock block, int search_iterations, String mode, int freq, Double alpha, Double beta, Double gamma) { int cols = block.getNumColumns(); int rows = block.getNumRows(); diff --git a/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java b/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java index e0284ffe9f6..50bcbe1a1b6 100644 --- a/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java +++ b/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java @@ -24,11 +24,15 @@ 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.*; +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"; @@ -49,12 +53,18 @@ public void setUp() { 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); + rCmd = getRCmd("double", resultPath, toRCsv()); runRScript(true); HashMap rOutput = readRMatrixFromFS(resultPath); @@ -81,7 +91,7 @@ public void testTripleExponentialSmoothing(){ fullRScriptName = SCRIPT_DIR + TEST_DIR + TEST_NAME + ".R"; String resultPath = output("R_triple"); - rCmd = getRCmd("triple", resultPath); + rCmd = getRCmd("triple", resultPath, toRCsv()); runRScript(true); HashMap rOutputTriple = readRMatrixFromFS(resultPath); @@ -118,7 +128,7 @@ public void testOddTripleExponentialSmoothing(){ fullRScriptName = SCRIPT_DIR + TEST_DIR + TEST_NAME + ".R"; String resultPath = output("R_triple_odd"); - rCmd = getRCmd("odd_triple", resultPath); + rCmd = getRCmd("odd_triple", resultPath, toRCsv()); runRScript(true); HashMap rOutputTriple = readRMatrixFromFS(resultPath); diff --git a/src/test/scripts/functions/builtin/exponentialSmoothing.R b/src/test/scripts/functions/builtin/exponentialSmoothing.R index 50c30e29121..03067fdfa8b 100644 --- a/src/test/scripts/functions/builtin/exponentialSmoothing.R +++ b/src/test/scripts/functions/builtin/exponentialSmoothing.R @@ -21,21 +21,25 @@ args <- commandArgs(trailingOnly = TRUE) -if (length(args) != 2L) { - stop("Expected arguments: ") +if (length(args) != 3L) { + stop("Expected arguments: ") } mode <- args[1] output_path <- args[2] -ts_data <- c( - 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 +ts_data <- scan( + text = args[3], + what = double(), + sep = ",", + quiet = TRUE ) +if (length(ts_data) == 0L || any(!is.finite(ts_data))) { + stop("Input data must contain finite numeric values") +} + + if (mode == "double") { hw_model <- stats::HoltWinters( ts_data, @@ -92,4 +96,4 @@ dir.create( Matrix::writeMM( Matrix::Matrix(res_matrix, sparse = TRUE), file = output_path -) \ No newline at end of file +) From d9403e01ab7ff441c9f12a41ae1bc34af5be1736 Mon Sep 17 00:00:00 2001 From: ywcb00 Date: Mon, 16 Nov 2026 14:24:29 +0100 Subject: [PATCH 6/8] fix(test/functions/builtin/BuiltinEMATest.java): compare against R reference baseline fix(main/runtime/util/EMAUtils.java): fix minor inconsistencies (e.g., remove copied starting data before returning result) --- .../apache/sysds/runtime/util/EMAUtils.java | 16 +- .../sysds/runtime/util/EMAUtilsTest.java | 161 ------------------ .../builtin/part1/BuiltinEMATest.java | 100 ++++++----- .../builtin/exponentialMovingAverage.R | 44 +++++ .../functions/builtin/exponentialSmoothing.R | 99 ----------- 5 files changed, 108 insertions(+), 312 deletions(-) delete mode 100644 src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java create mode 100644 src/test/scripts/functions/builtin/exponentialMovingAverage.R delete mode 100644 src/test/scripts/functions/builtin/exponentialSmoothing.R diff --git a/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java b/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java index aa5008b4f3e..92ba551b42c 100644 --- a/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java +++ b/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java @@ -73,7 +73,6 @@ public class EMAUtils { public static FrameBlock exponentialMovingAverageImputation(FrameBlock block, int search_iterations, String mode, int freq, Double alpha, Double beta, Double gamma) { int cols = block.getNumColumns(); - int rows = block.getNumRows(); ArrayList data_list = new ArrayList<>(); @@ -112,8 +111,8 @@ else if (i == 0 || lst.rsme < best_cont.rsme) { } } - Double[][] values = new Double[][]{}; - FrameBlock new_block = generateBlock(rows, cols, data_list.toArray(values)); + Double[][] values = data_list.toArray(Double[][]::new); + FrameBlock new_block = generateBlock(values[0].length, values.length, values); return new_block; } @@ -268,6 +267,9 @@ public static Container double_exponential_smoothing(Double[] data, Double alpha } } + pred.remove(0); // removing the first two element again which were copied from the input data + pred.remove(0); + // Calculation of RMSE double sum = .0; for (int i = 0; i < not_missing.size(); i++) { @@ -280,7 +282,7 @@ public static Container double_exponential_smoothing(Double[] data, Double alpha } public static Container triple_exponential_smoothing(Double[] data, Double alpha, Double beta, - Double gamma, Integer freq) { + Double gamma, Integer freq) { // Exception handling validateRange(alpha, beta, gamma); @@ -352,7 +354,7 @@ public static Container triple_exponential_smoothing(Double[] data, Double alpha season[(i + (freq / 2)) % freq] = combined; } - // Preparing the data for linear regression, in order to initialize level + // Preparing the data for linear regression, in order to initialize level double sum = 0; for (int i = 0; i < season.length; i++) { sum += season[i]; @@ -428,6 +430,10 @@ public static Container triple_exponential_smoothing(Double[] data, Double alpha } } + for(int i = 0; i < freq; i++) { + pred.remove(0); + } + // Calculate the RMSE sum = .0; for (int i = 0; i < not_missing.size(); i++) { diff --git a/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java b/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java deleted file mode 100644 index 50bcbe1a1b6..00000000000 --- a/src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * 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 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 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 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); - } - } -} \ No newline at end of file diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinEMATest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinEMATest.java index d4639a64e47..dc9d04227db 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinEMATest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinEMATest.java @@ -21,11 +21,15 @@ import static org.junit.Assert.assertTrue; +import java.util.Arrays; +import java.util.HashMap; +import java.util.TreeMap; + import org.apache.sysds.common.Types; import org.apache.sysds.common.Types.ExecType; import org.apache.sysds.runtime.frame.data.FrameBlock; import org.apache.sysds.runtime.frame.data.columns.Array; -import org.apache.sysds.runtime.io.FrameWriterFactory; +import org.apache.sysds.runtime.matrix.data.MatrixValue.CellIndex; import org.apache.sysds.test.AutomatedTestBase; import org.apache.sysds.test.TestConfiguration; import org.apache.sysds.test.TestUtils; @@ -41,6 +45,10 @@ public class BuiltinEMATest extends AutomatedTestBase { private static final String TEST_CLASS_DIR = TEST_DIR + BuiltinEMATest.class.getSimpleName() + "/"; private static final double e = 0.00001; + private Double[] DATA = new Double[]{41.7275, 24.0418, 32.3281, 37.3287, 46.2132, 29.3463, 36.4829, 42.9777, 48.9015, + 31.1802, 37.7179, 40.4202, 51.2069, 31.8872, 40.9783, 43.7725, 55.5586, 33.8509, 42.0764, 45.6423, 59.7668, + 35.1919, 44.3197, 47.9137}; + @BeforeClass public static void init() { TestUtils.clearDirectory(TEST_DATA_DIR + TEST_CLASS_DIR); @@ -56,7 +64,7 @@ public static void cleanUp() { @Override public void setUp() { TestUtils.clearAssertionInformation(); - addTestConfiguration(TEST_NAME,new TestConfiguration(TEST_CLASS_DIR, TEST_NAME,new String[]{"B"})); + addTestConfiguration(TEST_NAME,new TestConfiguration(TEST_CLASS_DIR, TEST_NAME, new String[]{"B"})); if (TEST_CACHE_ENABLED) { setOutAndExpectedDeletionDisabled(true); } @@ -119,62 +127,48 @@ public void CompareToAirGap() { 461.,499.016129,535.,622.,606.,508.,461.,390.,432.}; Double[][] values = new Double[][]{data}; FrameBlock f = generateBlock(data.length, 1, values); - runTest(f, ExecType.CP, 100, "triple", 4, Double.NaN, Double.NaN, Double.NaN, + runTest(f, ExecType.CP, 100, "triple", 4, 0.5, Double.NaN, Double.NaN, na_ma_ref, 200); } @Test - public void checkSingleRData() { - Double[] data= new Double[]{41.7275, 24.0418, 32.3281, 37.3287, 46.2132, 29.3463, 36.4829, 42.9777, 48.9015, - 31.1802, 37.7179, 40.4202, 51.2069, 31.8872, 40.9783, 43.7725, 55.5586, 33.8509, 42.0764, 45.6423, 59.7668, - 35.1919, 44.3197, 47.9137}; - Double[] na_ma_ref = new Double[]{41.7275, 32.88465, 32.606375, 34.9675375, 40.590368749999996, 34.968334375, - 35.7256171875, 39.35165859375, 44.126579296875, 37.653389648437496, 37.68564482421875, 39.052922412109375, - 45.129911206054686, 38.50855560302735, 39.743427801513675, 41.757963900756835, 48.65828195037842, 41.25459097518921, - 41.665495487594605, 43.6538977437973, 51.71034887189865, 43.451124435949325, 43.88541221797466, 45.899556108987326}; - Double[][] values = new Double[][]{data}; - FrameBlock f = generateBlock(data.length, 1, values); - runTest(f, ExecType.CP, 100, "single", 4, 0.5, Double.NaN, Double.NaN, na_ma_ref, e); + public void testSimpleExponentialSmoothing() { + Double[][] values = new Double[][]{DATA}; + FrameBlock f = generateBlock(DATA.length, 1, values); + runTest(f, ExecType.CP, 100, "single", 4, 0.5, Double.NaN, Double.NaN, null, e); } @Test - public void checkDoubleRData() { - Double[] data= new Double[]{41.7275, 24.0418, 32.3281, 37.3287, 46.2132, 29.3463, 36.4829, 42.9777, 48.9015, - 31.1802, 37.7179, 40.4202, 51.2069, 31.8872, 40.9783, 43.7725, 55.5586, 33.8509, 42.0764, 45.6423, 59.7668, - 35.1919, 44.3197, 47.9137}; - Double[] na_ma_ref = new Double[]{41.7275, 6.356099999999998, 8.149399999999998, 18.841175, 35.47231874999999, - 33.82293593749999, 37.231535546874994, 43.619776464843746, 51.09622780761718, 40.99479652709961, 38.39370675506592, - 38.95093518028259, 47.68689059782028, 38.44509565713406, 39.003049272507425, 41.87148876206725, 52.620536316330345, - 42.448801014379306, 41.38258310980896, 43.697353380071554, 55.93435017018497, 44.57978602269542, 43.40138244327679, - 45.73726004274828}; - Double[][] values = new Double[][]{data}; - FrameBlock f = generateBlock(data.length, 1, values); - runTest(f, ExecType.CP, 100, "double", 4, 0.5, 0.5, Double.NaN, na_ma_ref, e); + public void testDoubleExponentialSmoothing() { + Double[][] values = new Double[][]{DATA}; + FrameBlock f = generateBlock(DATA.length, 1, values); + runTest(f, ExecType.CP, 100, "double", 4, 0.5, 0.5, Double.NaN, null, e); } @Test - public void checkTripleRData() { - Double[] data= new Double[]{41.7275, 24.0418, 32.3281, 37.3287, 46.2132, 29.3463, 36.4829, 42.9777, 48.9015, 31.1802, - 37.7179, 40.4202, 51.2069, 31.8872, 40.9783, 43.7725, 55.5586, 33.8509, 42.0764, 45.6423, 59.7668, 35.1919, - 44.3197, 47.9137}; - Double[] na_ma_ref = new Double[]{41.7275, 24.0418, 32.3281, 37.3287, 43.73937749999999, 28.710109375, 37.62691046875001, - 42.5309708984375, 52.88580151367188, 33.15671534912109, 38.65758862487794, 42.44805077255249, 48.22574473186493, - 31.990608629751197, 38.66479375649214, 44.60066332877576, 53.321224908427446, 36.84788508769925, 42.86094393343228, - 45.86929170959124, 55.2140372941347, 38.72586172952057, 45.025358876239984, 48.23407483458373}; - Double[][] values = new Double[][]{data}; - FrameBlock f = generateBlock(data.length, 1, values); - runTest(f, ExecType.CP, 100, "triple", 4, 0.5, 0.5, 0.5, na_ma_ref, e); + public void testTripleExponentialSmoothing() { + Double[][] values = new Double[][]{DATA}; + FrameBlock f = generateBlock(DATA.length, 1, values); + runTest(f, ExecType.CP, 100, "triple", 4, 0.5, 0.5, 0.5, null, e); + } + + @Test + public void testTripleExponentialSmoothingOddFreq() { + Double[][] values = new Double[][]{DATA}; + FrameBlock f = generateBlock(DATA.length, 1, values); + runTest(f, ExecType.CP, 100, "triple", 7, 0.5, 0.5, 0.5, null, e); } private static double calcRMSE(Double[] list1, Double[] list2) { double sum = 0; - for(int i =0; i< list1.length; i++) - sum += Math.pow(list1[i] - list2[i], 2); - return Math.sqrt(sum / list1.length); + int minLen = Math.min(list1.length, list2.length); + for(int counter = 0; counter < minLen; counter++) + sum += Math.pow(list1[counter] - list2[counter], 2); + return Math.sqrt(sum / minLen); } private void runTest(FrameBlock test_frame, ExecType et, Integer search_iterations, String mode, Integer freq, - Double alpha, Double beta, Double gamma, Double[] reference, double max_error) + double alpha, double beta, double gamma, Double[] reference, double max_error) { Types.ExecMode platformOld = setExecMode(et); @@ -183,13 +177,17 @@ private void runTest(FrameBlock test_frame, ExecType et, Integer search_iteratio String HOME = SCRIPT_DIR + TEST_DIR; fullDMLScriptName = HOME + TEST_NAME + ".dml"; - programArgs = new String[] {"-nvargs", "F=" + input("F"), "O=" + output("O"), "search_iterations=" + - search_iterations, "mode=" + mode, "freq=" + freq, "alpha=" + alpha, "beta=" + beta, "gamma=" + gamma}; - - FrameWriterFactory.createFrameWriter(Types.FileFormat.CSV). - writeFrameToHDFS(test_frame, input("F"), test_frame.getNumRows(), test_frame.getNumColumns()); + programArgs = new String[] {"-nvargs", "F=" + input("F"), "O=" + output("O"), + "search_iterations=" + search_iterations, "mode=" + mode, "freq=" + freq, + "alpha=" + alpha, "beta=" + beta, "gamma=" + gamma}; + fullRScriptName = HOME + TEST_NAME + ".R"; + rCmd = getRCmd(inputDir(), expectedDir(), mode, freq.toString(), Double.toString(alpha), + Double.toString(beta), Double.toString(gamma)); + Types.ValueType[] schema = new Types.ValueType[]{Types.ValueType.STRING}; + writeInputFrameWithMTD("F", test_frame, true, schema, Types.FileFormat.CSV); runTest(true, false, null, -1); + runRScript(true); FrameBlock outputFrame = readDMLFrameFromHDFS("O", Types.FileFormat.CSV); Array values = outputFrame.getColumn(0); @@ -197,8 +195,16 @@ private void runTest(FrameBlock test_frame, ExecType et, Integer search_iteratio for(int i = 0; i < values.size(); i++) data[i] = Double.valueOf(values.get(i).toString()); + if(reference == null) { + HashMap rfile = readRMatrixFromExpectedDir("O"); + TreeMap treemap = new TreeMap<>(); + for(CellIndex ci : rfile.keySet()) + treemap.put(ci.row, rfile.get(ci)); + reference = treemap.values().toArray(Double[]::new); + } - assertTrue(calcRMSE(data, reference) <= max_error); + double rmse = calcRMSE(data, reference); + assertTrue(rmse <= max_error); } catch (Exception ex) { ex.printStackTrace(); diff --git a/src/test/scripts/functions/builtin/exponentialMovingAverage.R b/src/test/scripts/functions/builtin/exponentialMovingAverage.R new file mode 100644 index 00000000000..dffe61c742b --- /dev/null +++ b/src/test/scripts/functions/builtin/exponentialMovingAverage.R @@ -0,0 +1,44 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +args <- commandArgs(trailingOnly = TRUE); +library("Matrix"); +options(digits=22); + +X = as.matrix(read.csv(paste(args[1], "F.csv", sep=""), header=FALSE)); +output_path <- args[2] +mode = args[2+1]; +freq = as.integer(args[3+1]) +beta = FALSE; +gamma = FALSE; +alpha = as.double(args[3+2]); +if(mode == "double") { + beta = as.double(args[4+2]); +} else { + if(mode == "triple") { + beta = as.double(args[4+2]); + gamma = as.double(args[5+2]); + } +} + +ema_model = stats::HoltWinters(x=ts(X, frequency=freq), alpha=alpha, beta=beta, gamma=gamma); +R = as.numeric(ema_model$fitted[, "xhat"]); +writeMM(as(R, "CsparseMatrix"), paste(output_path, "O", sep="")); diff --git a/src/test/scripts/functions/builtin/exponentialSmoothing.R b/src/test/scripts/functions/builtin/exponentialSmoothing.R deleted file mode 100644 index 03067fdfa8b..00000000000 --- a/src/test/scripts/functions/builtin/exponentialSmoothing.R +++ /dev/null @@ -1,99 +0,0 @@ -#------------------------------------------------------------- -# -# 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. -# -#------------------------------------------------------------- - -args <- commandArgs(trailingOnly = TRUE) - -if (length(args) != 3L) { - stop("Expected arguments: ") -} - -mode <- args[1] -output_path <- args[2] - -ts_data <- scan( - text = args[3], - what = double(), - sep = ",", - quiet = TRUE -) - -if (length(ts_data) == 0L || any(!is.finite(ts_data))) { - stop("Input data must contain finite numeric values") -} - - -if (mode == "double") { - hw_model <- stats::HoltWinters( - ts_data, - alpha=0.3, - beta = 0.1, - gamma = FALSE) - fit_vals <- as.numeric(fitted(hw_model)[, "xhat"]) - - res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 1) - res_matrix[, 1] <- fit_vals - -} else if (mode == "triple") { - ts_obj <- ts(ts_data, frequency = 12) - - hw_model <- stats::HoltWinters( - ts_obj, - alpha = 0.3, - beta = 0.1, - gamma = 0.2, - seasonal = "additive" - ) - - fit_vals <- as.numeric(fitted(hw_model)[, "xhat"]) - - res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 1) - res_matrix[, 1] <- fit_vals - -} else if (mode == "odd_triple") { - ts_obj <- ts(ts_data, frequency = 7) - - hw_model <- stats::HoltWinters( - ts_obj, - alpha = 0.3, - beta = 0.1, - gamma = 0.2, - seasonal = "additive" - ) - - fit_vals <- as.numeric(fitted(hw_model)[, "xhat"]) - - res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 1) - res_matrix[, 1] <- fit_vals - - } else { - stop(paste("Unknown smoothing mode:", mode)) -} - -dir.create( - dirname(output_path), - recursive = TRUE, - showWarnings = FALSE -) - -Matrix::writeMM( - Matrix::Matrix(res_matrix, sparse = TRUE), - file = output_path -) From a02b169337bf276a5b41fa1568a9d6894f1f1643 Mon Sep 17 00:00:00 2001 From: ywcb00 Date: Mon, 16 Nov 2026 16:08:15 +0100 Subject: [PATCH 7/8] fix(main/runtime/util/EMAUtils.java): test different parameters when 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 --- .../apache/sysds/runtime/util/EMAUtils.java | 19 +++-- .../builtin/part1/BuiltinEMATest.java | 73 +++---------------- .../builtin/exponentialMovingAverage.R | 7 ++ 3 files changed, 32 insertions(+), 67 deletions(-) diff --git a/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java b/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java index 92ba551b42c..d89fb1ad755 100644 --- a/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java +++ b/src/main/java/org/apache/sysds/runtime/util/EMAUtils.java @@ -79,21 +79,28 @@ public static FrameBlock exponentialMovingAverageImputation(FrameBlock block, in for (int j = 0; j < cols; j++) { String[] values = (String[]) block.getColumnData(j); Double[] data = new Double[values.length]; - for (int i = 0; i< values.length; i++) data[i] = Double.valueOf(values[i]); + for(int i = 0; i< values.length; i++) + data[i] = Double.valueOf(values[i]); Container best_cont = new Container(new Double[]{.0}, 1000); Random rand = new Random(); Container lst = null; + boolean alphaIsNan = Double.isNaN(alpha); + boolean betaIsNan = Double.isNaN(beta); + boolean gammaIsNan = Double.isNaN(gamma); + if(!alphaIsNan && !betaIsNan && !gammaIsNan) + search_iterations = 1; + for (int i = 0; i < search_iterations; i++) { - if (Double.isNaN(alpha)) + if(alphaIsNan) alpha = rand.nextDouble(); - if (Double.isNaN(beta)) + if(betaIsNan) beta = rand.nextDouble(); - if (Double.isNaN(gamma)) + if(gammaIsNan) gamma = rand.nextDouble(); - if (mode.equals("single")) { + if(mode.equals("single")) { lst = single_exponential_smoothing(data, alpha); } else if (mode.equals("double")) { lst = double_exponential_smoothing(data, alpha, beta); @@ -103,7 +110,7 @@ public static FrameBlock exponentialMovingAverageImputation(FrameBlock block, in if(lst == null) throw new DMLRuntimeException("invalid null pointer"); - else if (i == 0 || lst.rsme < best_cont.rsme) { + else if(i == 0 || lst.rsme < best_cont.rsme) { best_cont = lst; data_list.add(best_cont.values); } diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinEMATest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinEMATest.java index dc9d04227db..5b54fe8c913 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinEMATest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinEMATest.java @@ -79,84 +79,37 @@ public void CompareToAirPython() { 278.,284.,277.,317.,313.,318.,374.,413.,405.,355.,306.,271.,306.,315.,301.,356.,348.,355.,422.,465.,467.,404., 347.,305.,336.,340.,318.,362.,348.,363.,435.,491.,505.,404.,359.,310.,337.,360.,342.,406.,396.,420.,472.,548., 559.,463.,407.,362.,405.,417.,391.,419.,461.,472.,535.,622.,606.,508.,461.,390.,432.}; - Double[] na_ma_ref = new Double[]{112.0,118.0,132.0,129.0,121.0,135.0,148.0,148.0,136.0,119.0,104.0,118.0, - 110.64316239316241,124.65882867132864,141.63607177544674,136.7354015394328,127.17767588262429,150.30329316196824, - 161.70161586161797,169.38854499320993,161.19601881138237,145.06448246981816,122.484478767933,127.39157601488053, - 129.72591491348797,152.03705561548736,169.0315776597689,174.0450155013305,161.71883176768532,197.38664830203192, - 200.16326105914544,199.3633290787375,186.7801706766464,166.84581558009475,147.01335928013515,158.28476700133308, - 158.16728220922437,177.39919242218832,197.8931822724927,190.7538466127484,176.3701663182143,203.6815674660317, - 230.97131101134238,237.12395417864016,235.69002588652017,203.9793963562213,177.70695462791417,182.8604180621578, - 181.7019278809486,198.65873966400525,211.81123478285303,224.81149965425675,234.03965829250822,264.637902643598, - 269.337433358522,268.2236823080142,258.90098063468133,226.18596531382858,194.3171221649824,188.13753099285515, - 180.73112166348903,197.19799095036475,202.00699270410672,215.3746071328909,221.7279582439683,263.37938956281806, - 291.04092902170333,315.2526229493343,299.24296207175007,258.8843893281736,215.75701045434437,208.30972791961293, - 204.6705448924415,230.79856526946912,251.66083493325718,257.19830790310965,265.79900618102994,302.134340294941, - 340.023092988112,377.22712842120563,361.68362912145847,319.1713492927022,265.9551211952107,243.11007047239005, - 243.40147848232402,266.55060745123546,292.2235041830092,305.63559628413805,313.4173919289333,352.29010792271606, - 400.1653619571387,430.1970897847026,419.06532694160865,367.4237601305769,299.03974216212833,272.31262029667437, - 264.33527736015446,286.58031520064947,311.91458026777696,337.82852013844627,351.57523114182044,395.0206150729328, - 451.2263986714124,486.29420474135196,484.7046653181514,427.5859319836733,347.2936931627886,307.5407332157359, - 286.9831427702983,298.6001849239334,319.6590363831499,334.1457260693061,341.82615134798755,393.9656366982082, - 460.0996934540272,511.6328818051482,527.2903638248666,447.9666064300616,361.5472947970979,313.6150739283058, - 282.8820652975183,304.4380438439978,341.65102241233495,379.3461886314897,403.40100350027996,467.25353238867564, - 515.0436188516387,566.7544915874026,576.6520376922236,507.8707884571211,417.21104490644575,369.75194100231744, - 350.34762676605754,363.74055663694884,392.5832813888661,391.0929206754841,442.5756334301262,517.5411050850213, - 582.8401346087848,647.5306590463075,635.3370387229387,555.6543956799871,467.86361741316273,403.15056379128157}; Double[][] values = new Double[][]{data}; FrameBlock f = generateBlock(data.length, 1, values); - runTest(f, ExecType.CP, 100, "triple", 12, 0.5, 0.7, 0.1, na_ma_ref, e); - } - - @Test - public void CompareToAirGap() { - Double[] data= new Double[]{112.,118.,132.,129., 131.,135.,148.,148.,149.,119.,104.,118.,115.,126.,141.,135.,125., - 149.,170.,170.,Double.NaN,133.,Double.NaN,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.,204.,188.,235.,227.,234.,Double.NaN,302.,293.,259.,229.,203.,229.,242.,233.,267.,269.,270.,315.,364.,347., - 312.,274.,237.,278.,284.,277.,Double.NaN,Double.NaN,Double.NaN,374.,413.,405.,355.,306.,271.,306.,315.,301.,356., - 348.,355.,Double.NaN,465.,467.,404.,347.,Double.NaN,336.,340.,318.,Double.NaN,348.,363.,435.,491.,505.,404.,359., - 310.,337.,360.,342.,406.,396.,420.,472.,548.,559.,463.,407.,362.,Double.NaN,417.,391.,419.,461.,Double.NaN,535., - 622.,606.,508.,461.,390.,432.}; - Double[] na_ma_ref = new Double[]{112.,118.,132.,129.,133.1596639,135.,148.,148.,129.8606557,119.,104.,118.,115.,126., - 141.,135.,125.,149.,170.,170.,151.7909091,133.,144.8090909,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.,204.,188.,235.,227.,234.,256.6349206,302.,293.,259.,229.,203.,229.,242.,233.,267.,269.,270., - 315.,364.,347.,312.,274.,237.,278.,284.,277.,298.0641026,330.4516129,362.3589744,374.,413.,405.,355.,306.,271., - 306.,315.,301.,356.,348.,355.,396.9677419,465.,467.,404.,347.,360.95,336.,340.,318.,354.1311475,348.,363.,435., - 491.,505.,404.,359.,310.,337.,360.,342.,406.,396.,420.,472.,548.,559.,463.,407.,362.,410.766129,417.,391.,419., - 461.,499.016129,535.,622.,606.,508.,461.,390.,432.}; - Double[][] values = new Double[][]{data}; - FrameBlock f = generateBlock(data.length, 1, values); - runTest(f, ExecType.CP, 100, "triple", 4, 0.5, Double.NaN, Double.NaN, - na_ma_ref, 200); + runTest(f, ExecType.CP, 100, "triple", 12, 0.5, 0.7, 0.1, e); } @Test public void testSimpleExponentialSmoothing() { Double[][] values = new Double[][]{DATA}; FrameBlock f = generateBlock(DATA.length, 1, values); - runTest(f, ExecType.CP, 100, "single", 4, 0.5, Double.NaN, Double.NaN, null, e); + runTest(f, ExecType.CP, 100, "single", 4, 0.5, Double.NaN, Double.NaN, e); } @Test public void testDoubleExponentialSmoothing() { Double[][] values = new Double[][]{DATA}; FrameBlock f = generateBlock(DATA.length, 1, values); - runTest(f, ExecType.CP, 100, "double", 4, 0.5, 0.5, Double.NaN, null, e); + runTest(f, ExecType.CP, 100, "double", 4, 0.5, 0.5, Double.NaN, e); } @Test public void testTripleExponentialSmoothing() { Double[][] values = new Double[][]{DATA}; FrameBlock f = generateBlock(DATA.length, 1, values); - runTest(f, ExecType.CP, 100, "triple", 4, 0.5, 0.5, 0.5, null, e); + runTest(f, ExecType.CP, 100, "triple", 4, 0.5, 0.5, 0.5, e); } @Test public void testTripleExponentialSmoothingOddFreq() { Double[][] values = new Double[][]{DATA}; FrameBlock f = generateBlock(DATA.length, 1, values); - runTest(f, ExecType.CP, 100, "triple", 7, 0.5, 0.5, 0.5, null, e); + runTest(f, ExecType.CP, 100, "triple", 7, 0.5, 0.5, 0.5, e); } private static double calcRMSE(Double[] list1, Double[] list2) { @@ -168,7 +121,7 @@ private static double calcRMSE(Double[] list1, Double[] list2) { } private void runTest(FrameBlock test_frame, ExecType et, Integer search_iterations, String mode, Integer freq, - double alpha, double beta, double gamma, Double[] reference, double max_error) + double alpha, double beta, double gamma, double max_error) { Types.ExecMode platformOld = setExecMode(et); @@ -195,16 +148,14 @@ private void runTest(FrameBlock test_frame, ExecType et, Integer search_iteratio for(int i = 0; i < values.size(); i++) data[i] = Double.valueOf(values.get(i).toString()); - if(reference == null) { - HashMap rfile = readRMatrixFromExpectedDir("O"); - TreeMap treemap = new TreeMap<>(); - for(CellIndex ci : rfile.keySet()) - treemap.put(ci.row, rfile.get(ci)); - reference = treemap.values().toArray(Double[]::new); - } + HashMap rfile = readRMatrixFromExpectedDir("O"); + TreeMap treemap = new TreeMap<>(); + for(CellIndex ci : rfile.keySet()) + treemap.put(ci.row, rfile.get(ci)); + Double reference[] = treemap.values().toArray(Double[]::new); double rmse = calcRMSE(data, reference); - assertTrue(rmse <= max_error); + assertTrue("RMSE of " + rmse + " exceeds the maximal error of " + max_error, rmse <= max_error); } catch (Exception ex) { ex.printStackTrace(); diff --git a/src/test/scripts/functions/builtin/exponentialMovingAverage.R b/src/test/scripts/functions/builtin/exponentialMovingAverage.R index dffe61c742b..811e47024a8 100644 --- a/src/test/scripts/functions/builtin/exponentialMovingAverage.R +++ b/src/test/scripts/functions/builtin/exponentialMovingAverage.R @@ -39,6 +39,13 @@ if(mode == "double") { } } +if(is.na(alpha)) + alpha = FALSE; +if(is.na(beta)) + beta = FALSE; +if(is.na(gamma)) + gamma = FALSE; + ema_model = stats::HoltWinters(x=ts(X, frequency=freq), alpha=alpha, beta=beta, gamma=gamma); R = as.numeric(ema_model$fitted[, "xhat"]); writeMM(as(R, "CsparseMatrix"), paste(output_path, "O", sep="")); From 098e2147c005e5ea0e3037ef5f75e70bad062ff8 Mon Sep 17 00:00:00 2001 From: ywcb00 Date: Tue, 17 Nov 2026 09:21:05 +0100 Subject: [PATCH 8/8] chore(test/functions/builtin/BuiltinEMATest.java): remove unused import --- .../sysds/test/functions/builtin/part1/BuiltinEMATest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinEMATest.java b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinEMATest.java index 5b54fe8c913..a1afa7c8d9c 100644 --- a/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinEMATest.java +++ b/src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinEMATest.java @@ -21,7 +21,6 @@ import static org.junit.Assert.assertTrue; -import java.util.Arrays; import java.util.HashMap; import java.util.TreeMap;