需要jar包Jama-1.0.2.jar,数据:时序数据的值 下载连接
https://download.csdn.net/download/dongyang1124/86265504
package arima;
import java.util.Vector;
public class ARMAModel
{
private double [] data = {};
private int p; //AR阶数
private int q; //MA阶数
public ARMAModel(double [] data, int p, int q)
{
this.data = data;
this.p = p;
this.q = q;
}
/**
* 在ARMA模型中,首先根据原始数据求得AR模型的自回归系数(AR系数)
* 利用AR系数与原始数据,求解的残差序列,根据残差序列的自协方差最终求得ARMA中MA系数
* @return ar, ma
*/
public Vector<double []> solveCoeOfARMA()
{
Vector<double []>vec = new Vector<>();
//ARMA模型
double [] armaCoe = new ARMAMethod().computeARMACoe(this.data, this.p, this.q);
//AR系数
double [] arCoe = new double[this.p + 1];
System.arraycopy(armaCoe, 0, arCoe, 0, arCoe.length);
//MA系数
double [] maCoe = new double[this.q + 1];
System.arraycopy(armaCoe, (this.p + 1), maCoe, 0, maCoe.length);
vec.add(arCoe);
vec.add(maCoe);
return vec;
}
}
package arima;
import java.util.Vector;
public class ARModel
{
private double [] data;
private int p;
public ARModel(double [] data, int p)
{
this.data = data;
this.p = p;
}
public Vector<double []> solveCoeOfAR()
{
Vector<double []>vec = new Vector<>();
double [] arCoe = new ARMAMethod().computeARCoe(this.data, this.p);
vec.add(arCoe);
return vec;
}
}
package arima;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class MainTest {
private static final SimpleDateFormat sdfWhole = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String args[]) {
Path path = Paths.get("./data/", "data_20220621.txt");
File file = path.toFile();
try (BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)))) {
String line = null;
ArrayList<Double> al = new ArrayList<Double>();
while ((line = br.readLine()) != null) {
al.add(Double.parseDouble(line));
}
ArrayList<Double> samplingDataList = new ArrayList<>();
ArrayList<Double> comparisonDataList = new ArrayList<>();
System.out.println("开始时间"+sdfWhole.format(new Date()));
for (int i = 0; i < al.size(); i++) {
if (i < al.size() / 3 * 2) {
samplingDataList.add(al.get(i));
} else {
comparisonDataList.add(al.get(i));
}
}
for (double comData : comparisonDataList) {
double predict = predect(samplingDataList);
//System.out.println("Predict value=" + predict);
samplingDataList.add(predict);
/*System.out.println("Predict error=" + (predict - comData)
/ comData * 100 + "%");*/
}
System.out.println("结束时间"+sdfWhole.format(new Date()));
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* 获取预测结果的值
*
* @param al
* @return
*/
public static double predect(ArrayList<Double> al) {
double[] data = null;
data = al.stream().mapToDouble(i -> i).toArray();
ARIMAModel arima = new ARIMAModel(data);
ArrayList<int[]> list = new ArrayList<>();
int period = 7;
int modelCnt = 3, cnt = 0;// 通过多次预测的平均值作为预测值
int[] tmpPredict = new int[modelCnt];
for (int k = 0; k < modelCnt; ++k) {// 控制通过多少组参数进行计算最终的结果
int[] bestModel = arima.getARIMAModel(period, list,
(k == 0) ? false : true);
if (bestModel.length == 0) {
tmpPredict[k] = (int) data[data.length - period];
cnt++;
break;
} else {
int predictDiff = arima.predictValue(bestModel[0],
bestModel[1], period);
tmpPredict[k] = arima.aftDeal(predictDiff, period);
cnt++;
}
list.add(bestModel);
}
double sumPredict = 0.0;
for (int k = 0; k < cnt; ++k) {
sumPredict += (double) tmpPredict[k] / (double) cnt;
}
double predict = (double) Math.round(sumPredict);
return predict;
}
}
package arima;
import java.util.Vector;
public class MAModel
{
private double [] data;
private int q;
public MAModel(double [] data, int q)
{
this.data = data;
this.q = q;
}
public Vector<double []> solveCoeOfMA()
{
Vector<double []>vec = new Vector<>();
double [] maCoe = new ARMAMethod().comput