## 编程知识 cdmana.com

### Definition, reading and exponential smoothing of time series data (Java)

At the top's request , We need to achieve the following exponential smoothing for resource scheduling load prediction , That's using my favorite Java Do it .

quote 《 Introduction to Econometrics 》 A word of ： One obvious characteristic of time series data different from cross section data is that , Time series data sets are arranged in chronological order .

obviously , Cross section data are considered to be random results , That is to say, random samples are taken from the population . The difference between time series data and cross-sectional data is subtle , Although it also satisfies randomness , But this sequence is marked with a time stamp , In time order , And don't let the random arrangement of time lead to confusion , We can't allow time reversal to restart the process . For such a sequence, we call it a random process , Or time series process .

For time series , One of the problems often studied is to predict , One of the most common methods of smoothing is the exponential method . Here for quadratic exponential smoothing Java The implementation of the （ First exponential smoothing is included in quadratic exponential smoothing ）. Its principle refers to ： https://cloud.tencent.com/developer/article/1058557 . No more details here .

The data also refer to China 1981 - 1983 Annual flat glass monthly production data , The following files are saved as data2.txt

``` In our country 1981 - 1983 Annual flat glass monthly production data
1,240.3
2,222.8
3,243.1
4,222.2
5,222.6
6,218.7
7,234.5
8,248.6
9,261
10,275.3
11,269.4
12,291.2
13,301.9
14,285.5
15,286.6
16,260.5
17,298.5
18,291.8
19,267.3
20,277.9
21,303.5
22,313.3
23,327.6
24,338.3
25,340.37
26,318.51
27,336.85
28,326.64
29,342.9
30,337.53
31,320.09
32,332.17
33,344.01
34,335.79
35,350.67
36,367.37```

For the above data , Time is int type , And the yield is double type , For ease of reading , For the above data, define the row data class

```package timeSeries;

public class RowData {

private int time;
private double value;

public RowData() {
// TODO Auto-generated constructor stub
}

public RowData(int time, double value) {
super();
this.time = time;
this.value = value;
}

public int getTime() {
return time;
}

public void setTime(int time) {
this.time = time;
}

public double getValue() {
return value;
}

public void setValue(double value) {
this.value = value;
}

}```

Then define the file reading class , The data read is RowData Array

```package utilFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import timeSeries.RowData;

public class FileOpts {

File file = new File(dataPath);
ArrayList<RowData> datas = new ArrayList<RowData>();
try {
String line = "";
String[] splitdata;
}
while ((line = br.readLine()) != null) {
splitdata = line.split(",");
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return datas;
}

}```

Then define the time series analysis class , It's actually a function

```package timeSeries;

import java.util.ArrayList;

import java.util.Iterator;

public class ExponentialSmoothing2 {

public static double[][] expSmoothOrder2(int[] time, double[] values, double alpha, int preNum) {
int len = time.length;
//  Return a summary table
double[][] result = new double[len + preNum][7];
//  First column time , The second column of actual observations
for (int i = 0; i < len; i++) {
result[i][0] = time[i];
result[i][1] = values[i];
}

result[0][2] = values[0];
result[0][3] = result[0][2];

//  The third column is exponential smoothing value , Fourth column quadratic exponential smoothing value
// S1, S2 2, 3
for (int i = 1; i < len; i++) {
result[i][2] = alpha*values[i] + (1-alpha)*result[i-1][2];
result[i][3] = alpha*result[i][2] + (1-alpha)*result[i-1][3];
}

//  The fifth column a, The sixth column b
// a, b 4, 5
for (int i = 1; i < len; i++) {
result[i][4] = 2*result[i][2] - result[i][3];
result[i][5] = alpha/(1-alpha) * (result[i][2] - result[i][3]);
}
//  The seventh column predicted value F
// F 6
for (int i = 1; i < len; i++) {
result[i+preNum][6] = result[i][4] + result[i][5] * preNum;
}
return result;
}

public static void main(String[] args) {
//  get data
int len = data.size();
int[] time = new int[len];
double[] values = new double[len];
Iterator<RowData> it = data.iterator();
int index = 0;
while (it.hasNext()) {
RowData rowData = (RowData) it.next();
time[index] = rowData.getTime();
values[index] = rowData.getValue();
index++;
}

//        System.out.println(Arrays.toString(time));
//        System.out.println(Arrays.toString(values));
// ------------------ Quadratic exponential smoothing ---------------------
double[][] pre2= expSmoothOrder2(time, values, 0.5, 1);
System.out.printf("%6s, %6s, %6s, %6s, %6s, %6s, %6s\n", "time", "y", "s1", "s2", "a", "b", "F");
for (int i = 0; i < values.length; i++) {
System.out.printf("%6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f, %6.2f \n", pre2[i][0], pre2[i][1], pre2[i][2],
pre2[i][3], pre2[i][4], pre2[i][5], pre2[i][6]);
}
//        System.out.printf("%6d, %6d, %6d, %6d, %6d, %6d, %6.2f \n", 37, 0, 0, 0, 0, 0, pre2[values.length][3]);
//        System.out.printf("%6d, %6d, %6d, %6d, %6d, %6d, %6.2f \n", 38, 0, 0, 0, 0, 0, pre2[35][1] + pre2[35][2] * 2);
//  error analysis
double MSE = 0;
double MAPE = 0;
double temp;
//        System.out.println("pre2.length = "+pre2.length);
for (int i = 2; i < pre2.length-1; i++) {
MSE += (pre2[i][1]-pre2[i][6])*(pre2[i][1]-pre2[i][6])/(pre2.length-2);
temp = (pre2[i][1]-pre2[i][6])/pre2[i][1];
if (temp < 0) {
MAPE -= temp/(pre2.length-2);
}else {
MAPE += temp/(pre2.length-2);
}
//            System.out.printf("iter: %d, y = %6.2f, F = %6.2f, MSE = %6.2f, MAPE = %6.5f\n", i, pre2[i][1], pre2[i][6], MSE, MAPE);
}
System.out.printf("MSE = %6.2f, MAPE = %6.5f\n", MSE, MAPE);
if (MAPE < 0.05) {
System.out.println(" The percent error is less than 0.05, The prediction accuracy is high ");
}else {
System.out.println(" The prediction error is more than 0.05");
}
}

}```

Execution results ：

In fact, you can also use Java Time series image rendering , After all Java Run away python or matlab Drawing is a lot of trouble ,Java It can also be realized , It's just for the convenience of writing a huge API, In the future, I'll write a blog about it in detail .

Scroll to Top