Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 94 additions & 16 deletions src/main/java/org/apache/sysds/runtime/util/EMAUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Double[]> 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);
Expand All @@ -105,16 +110,16 @@ 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);
}

}
}

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;
}

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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<Double> 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]);
Expand All @@ -202,7 +246,9 @@ public static Container double_exponential_smoothing(Double[] data, Double alpha
ArrayList<Double> 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 {
Expand All @@ -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]);
Expand All @@ -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);
Expand All @@ -235,19 +289,29 @@ 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<Double> start_data = new ArrayList<>();

for (int i = 0; i < l; i++) {
start_data.add(data[i]);
}

// Building a centered moving average for the inititialization of the level variable
ArrayList<Double> filt = new ArrayList<>();
ArrayList<Double> 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;
}
Expand All @@ -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);
Expand All @@ -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<Double> season_tmp = new ArrayList<>();
Expand All @@ -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()) {
Expand All @@ -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];
Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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]);
Expand All @@ -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);
Expand Down
Loading
Loading