ARIMA实现(亲测可用)

需要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
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值