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..d89fb1ad755 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,31 +71,36 @@ 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(); ArrayList data_list = new ArrayList<>(); 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); @@ -105,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); } @@ -113,8 +118,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; } @@ -145,7 +150,32 @@ public Container(Double[] vals, double error) { double rsme; } + 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]" + ); + } + } + + } + + 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]; @@ -186,12 +216,26 @@ public static Container single_exponential_smoothing(Double[] data, Double alpha } public static Container double_exponential_smoothing(Double[] data, Double alpha, Double beta) { + + // 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; 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]); @@ -202,7 +246,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 { @@ -211,11 +257,15 @@ 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 for (int i = 0; i < data.length; i++) { if (!Double.isNaN(data[i])) { not_missing.add(data[i]); @@ -224,6 +274,10 @@ 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++) { sum += Math.pow(not_missing.get(i) - not_missing_pred.get(i), 2); @@ -235,7 +289,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) { + Double gamma, Integer freq) { + + // Exception handling + validateRange(alpha, beta, gamma); + validateFrequency(freq, data); + + //Initialization data double l = freq * 2; ArrayList start_data = new ArrayList<>(); @@ -243,11 +303,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; } @@ -261,9 +325,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); @@ -272,6 +338,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<>(); @@ -282,6 +349,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()) { @@ -293,6 +361,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]; @@ -316,6 +385,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(); @@ -338,6 +409,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); @@ -356,6 +428,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]); @@ -364,6 +437,11 @@ 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++) { sum += Math.pow(not_missing.get(i) - not_missing_pred.get(i), 2); 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..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,11 +21,14 @@ import static org.junit.Assert.assertTrue; +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 +44,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 +63,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); } @@ -71,110 +78,49 @@ 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); + runTest(f, ExecType.CP, 100, "triple", 12, 0.5, 0.7, 0.1, 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, Double.NaN, Double.NaN, Double.NaN, - na_ma_ref, 200); + 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, e); } @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 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, 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 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, 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 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, 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 max_error) { Types.ExecMode platformOld = setExecMode(et); @@ -183,13 +129,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 +147,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()); + 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); - assertTrue(calcRMSE(data, reference) <= max_error); + double rmse = calcRMSE(data, reference); + 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 new file mode 100644 index 00000000000..811e47024a8 --- /dev/null +++ b/src/test/scripts/functions/builtin/exponentialMovingAverage.R @@ -0,0 +1,51 @@ +#------------------------------------------------------------- +# +# 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]); + } +} + +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=""));