ARIMA实现(亲测可用)

这篇具有很好参考价值的文章主要介绍了ARIMA实现(亲测可用)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

需要jar包Jama-1.0.2.jar,数据:时序数据的值 下载连接
https://download.csdn.net/download/dongyang1124/86265504文章来源地址https://www.toymoban.com/news/detail-598103.html

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().computeMACoe(this.data, this.q);
		
		vec.add(maCoe);
		
		return vec;
	}
}

package arima;

import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;

public class ARIMAModel
{
	double [] originalData = {};
	double [] dataFirDiff = {};
	
	Vector<double []>arimaCoe = new Vector<>();
	
	public ARIMAModel()
	{
		
	}
	
	public ARIMAModel(double [] originalData)
	{
		this.originalData = originalData;
	}
	
	public double [] preFirDiff(double [] preData)		//一阶差分(1)
	{	
		double [] tmpData = new double[preData.length - 1];
		for (int i = 0; i < preData.length - 1; ++i)
		{
			tmpData[i] = preData[i + 1] - preData[i];
		}
		return tmpData;
	}
	
	public double [] preSeasonDiff(double [] preData)	//季节性差分(6, 7)
	{	
		double [] tmpData = new double[preData.length - 7];
		for (int i = 0; i < preData.length - 7; ++i)
		{
			tmpData[i] = preData[i + 7] - preData[i];
		}
		return tmpData;
	}
	
	public double [] preDealDiff(int period)
	{
		if (period >= originalData.length - 1)		// 将6也归为季节性差分
		{
			period = 0;
		}
		switch (period)
		{
		case 0:
			return this.originalData;
		case 1:		 
			this.dataFirDiff = this.preFirDiff(this.originalData);
			return this.dataFirDiff;
		default:	
			return preSeasonDiff(originalData);
		}
	}
	
	public int [] getARIMAModel(int period, ArrayList<int []>notModel, boolean needNot)
	{
		double [] data = this.preDealDiff(period);
		
		double minAIC = Double.MAX_VALUE;
		int [] bestModel = new int[3];
		int type = 0;
		Vector<double []>coe = new Vector<>();
		
		// model产生, 即产生相应的p, q参数
		int len = data.length;
		if (len > 5)
		{
			len = 5;
		}
		int size = ((len + 2) * (len + 1)) / 2 - 1;
		int [][] model = new int[size][2];
		int cnt = 0;
		for (int i = 0; i <= len; ++i)
		{
			for (int j = 0; j <= len - i; ++j)
			{
				if (i == 0 && j == 0)
					continue;
				model[cnt][0] = i;
				model[cnt++][1] = j;
			}
		}

		for (int i = 0; i < model.length; ++i)
		{
			// 控制选择的参数
			boolean token = false;
			if (needNot)
			{
				for (int k = 0; k < notModel.size(); ++k)
				{
					if (model[i][0] == notModel.get(k)[0] && model[i][1] == notModel.get(k)[1])
					{
						token = true;
						break;
					}
				}
			}
			if (token)
			{
				continue;
			}
			
			if (model[i][0] == 0)
			{
				MAModel ma = new MAModel(data, model[i][1]);
				coe = ma.solveCoeOfMA();
				type = 1;
			}
			else if (model[i][1] == 0)
			{
				ARModel ar = new ARModel(data, model[i][0]);
				coe = ar.solveCoeOfAR();
				type = 2;
			}
			else
			{
				ARMAModel arma = new ARMAModel(data, model[i][0], model[i][1]);
				coe = arma.solveCoeOfARMA();
				type = 3;
			}				
			double aic = new ARMAMethod().getModelAIC(coe, data, type);
			// 在求解过程中如果阶数选取过长,可能会出现NAN或者无穷大的情况
			if (Double.isFinite(aic) && !Double.isNaN(aic) && aic < minAIC)
			{
				minAIC = aic;
				bestModel[0] = model[i][0];
				bestModel[1] = model[i][1];
				bestModel[2] = (int)Math.round(minAIC);
				this.arimaCoe = coe;
			}
		}
		return bestModel;
	}
	
	public int aftDeal(int predictValue, int period)
	{
		if (period >= originalData.length)
		{
			period = 0;
		}
		
		switch (period)
		{
		case 0:
			return (int)predictValue;
		case 1:
			return (int)(predictValue + originalData[originalData.length - 1]);
		case 2:
		default:	
			return (int)(predictValue + originalData[originalData.length - 7]);
		}
	}
	
	public int predictValue(int p, int q, int period)
	{
		double [] data = this.preDealDiff(period);
		int n = data.length;
		int predict = 0;
		double tmpAR = 0.0, tmpMA = 0.0;
		double [] errData = new double[q + 1];
		
		Random random = new Random();
		
		if (p == 0)
		{
			if(null!=this.arimaCoe&&!this.arimaCoe.isEmpty()){
				double [] maCoe = this.arimaCoe.get(0);
				for(int k = q; k < n; ++k)
				{
					tmpMA = 0;
					for(int i = 1; i <= q; ++i)
					{
						tmpMA += maCoe[i] * errData[i];
					}
					//产生各个时刻的噪声
					for(int j = q; j > 0; --j)
					{
						errData[j] = errData[j - 1];
					}
					errData[0] = random.nextGaussian()*Math.sqrt(maCoe[0]);
				}
				
			}
				predict = (int)(tmpMA); 
			
			
		}
		else if (q == 0)
		{
			double [] arCoe = this.arimaCoe.get(0);
			
			for(int k = p; k < n; ++k)
			{
				tmpAR = 0;
				for(int i = 0; i < p; ++i)
				{
					tmpAR += arCoe[i] * data[k - i - 1];
				}
			}
			predict = (int)(tmpAR);
		}
		else
		{
			double [] arCoe = this.arimaCoe.get(0);
			double [] maCoe = this.arimaCoe.get(1);
			
			for(int k = p; k < n; ++k)
			{
				tmpAR = 0;
				tmpMA = 0;
				for(int i = 0; i < p; ++i)
				{
					tmpAR += arCoe[i] * data[k- i - 1];
				}
				for(int i = 1; i <= q; ++i)
				{
					tmpMA += maCoe[i] * errData[i];
				}
			
				//产生各个时刻的噪声
				for(int j = q; j > 0; --j)
				{
					errData[j] = errData[j-1];
				}
				
				errData[0] = random.nextGaussian() * Math.sqrt(maCoe[0]);
			}
			
			predict = (int)(tmpAR + tmpMA);
		}
		
		return predict;
	}
}

package arima;

import java.util.Random;
import java.util.Vector;

import Jama.Matrix;

public class ARMAMethod
{
	public ARMAMethod()
	{
		
	}
	
	/**
	 * @param originalData
	 * @return 均值
	 */
	public double avgData(double [] originalData)
	{
		return this.sumData(originalData) / originalData.length;
	}
	
	/**
	 * @param originalData
	 * @return 求和
	 */
	public double sumData(double [] originalData)
	{
		double sum = 0.0;
		
		for (int i = 0; i < originalData.length; ++i)
		{
			sum += originalData[i];
		}
		return sum;
	}
	
	/**
	 * 计算标准差 sigma = sqrt(var);
	 * @param originalData
	 * @return 标准差
	 */
	public double stdErrData(double [] originalData)
	{
		return Math.sqrt(this.varErrData(originalData));
	}
	
	
	/**
	 * 计算方差 var = sum(x - mu) ^2 / N;
	 * @param originalData
	 * @return 方差
	 */
	public double varErrData(double [] originalData)
	{
		if (originalData.length <= 1)
			return 0.0;
		
		double var = 0.0;
		double mu = this.avgData(originalData);
		
		for (int i = 0; i < originalData.length; ++i)
		{
			var += (originalData[i] - mu) * (originalData[i] - mu);
		}
		var /= (originalData.length - 1);		//方差的无偏估计
		
		return var;
	}
	
	/**
	 * 计算自相关函数(系数) rou(k) = C(k) / C(0);
	 * 其中 C(k) = sum((x(t) - mu)*(x(t - k) - mu)) / (N - k),
	 * C(0) = var =  sum(x(t) - mu) ^2 / N;
	 * @param originalData
	 * @param order
	 * @return 自相关函数(rou(k))
	 */
	public double [] autoCorrData(double [] originalData, int order)
	{
		double [] autoCov = this.autoCovData(originalData, order);
		double [] autoCorr = new double[order + 1];		//默认初始化为0
		double var = this.varErrData(originalData);
		
		if (var != 0)
		{
			for (int i = 0; i < autoCorr.length; ++i)
			{
				autoCorr[i] = autoCov[i] / var;
			}
		}
		
		return autoCorr;
	}
	
	/**
	 * @param dataFir
	 * @param dataSec
	 * @return 皮尔逊相关系数(互相关)
	 */
	public double mutalCorr(double [] dataFir, double [] dataSec)
	{
		double sumX = 0.0;
		double sumY = 0.0;
		double sumXY = 0.0;
		double sumXSq = 0.0;
		double sumYSq = 0.0;
		int len = 0;
		
		if (dataFir.length != dataSec.length)
		{
			len = Math.min(dataFir.length, dataSec.length);
		}
		else
		{
			len = dataFir.length;
		}
		for (int i = 0; i < len; ++i)
		{
			sumX += dataFir[i];
			sumY += dataSec[i];
			sumXY += dataFir[i] * dataSec[i];
			sumXSq += dataFir[i] * dataFir[i];
			sumYSq += dataSec[i] * dataSec[i];
		}		
		
		double numerator = sumXY - sumX * sumY / len;
		double denominator = Math.sqrt((sumXSq - sumX * sumX / len) * (sumYSq - sumY * sumY / len));
		
		if (denominator == 0)
		{
			return 0.0;
		}
		
		return numerator/ denominator;
	}
	
	
	/**
	 * @param data
	 * @return		互相关矩阵
	 */
	public double [][] computeMutalCorrMatrix(double [][] data)
	{
		double [][] result = new double[data.length][data.length];
		for (int i = 0; i < data.length; ++i)
		{
			for (int j = 0; j < data.length; ++j)
			{
				result[i][j] = this.mutalCorr(data[i], data[j]);
			}
		}
		
		return result;
	}
	
	/**
	 * 计算自协方差,C(k) = sum((x(t) - mu)*(x(t - k) - mu)) / (N - k);
	 * @param originalData
	 * @param order
	 * @return 自协方差(gama(k))-->认为是自相关系数
	 */
	public double [] autoCovData(double [] originalData, int order)
	{
		double mu = this.avgData(originalData);
		double [] autoCov = new double[order + 1];
		
		for (int i = 0; i <= order; ++i)
		{
			autoCov[i] = 0.0;
			for (int j = 0; j < originalData.length - i; ++j)
			{
				autoCov[i] += (originalData[i + j] - mu) * (originalData[j] - mu);
			}
			autoCov[i] /= (originalData.length - i);
		}
		return autoCov;
	}
	

	
	/**
	 * @param vec		模型的系数
	 * @param data		数据
	 * @param type		选定的模型
	 * @return
	 */
	public double getModelAIC(Vector<double []>vec, double [] data, int type)
	{
		int n = data.length;
		int p = 0, q = 0;
		double tmpAR = 0.0, tmpMA = 0.0;
		double sumErr = 0.0;
		Random random = new Random();
		
		/* MA */	
		if (type == 1)
		{
			double [] maCoe = vec.get(0);
			q = maCoe.length;
			double [] errData = new double[q];
			
			for (int i = q - 1; i < n; ++i)
			{
				tmpMA = 0.0;
				for (int j = 1; j < q; ++j)
				{
					tmpMA += maCoe[j] * errData[j];
				}
				
				for (int j = q - 1; j > 0; --j)
				{
					errData[j] = errData[j - 1];
				}
				errData[0] = random.nextGaussian() * Math.sqrt(maCoe[0]);
				sumErr += (data[i] - tmpMA) * (data[i] - tmpMA);
			}
		    // return Math.log(sumErr) + (q + 1) * 2 / n;
			return (n - (q - 1)) * Math.log(sumErr / (n - (q - 1))) + (q + 1) * 2;
			// return  (n-(q-1))*Math.log(sumErr/(n-(q-1)))+(q)*Math.log(n-(q-1));		//AIC 最小二乘估计
		}
		/* AR */
		else if (type == 2)
		{
			double [] arCoe = vec.get(0);
			p = arCoe.length;
			
			for (int i = p - 1; i < n; ++i)
			{
				tmpAR = 0.0;
				for (int j = 0; j < p - 1; ++j)
				{
					tmpAR += arCoe[j] * data[i - j - 1];
				}
				sumErr += (data[i] - tmpAR) * (data[i] - tmpAR);
			}
//			return Math.log(sumErr) + (p + 1) * 2 / n;
			return (n - (p - 1)) * Math.log(sumErr / (n - (p - 1))) + (p + 1) * 2;
			// return (n-(p-1))*Math.log(sumErr/(n-(p-1)))+(p)*Math.log(n-(p-1));		//AIC 最小二乘估计
		}
		/* ARMA */
		else
		{
			double [] arCoe = vec.get(0);
			double [] maCoe = vec.get(1);
			p = arCoe.length;
			q = maCoe.length;
			double [] errData = new double[q];
			
			for (int i = p - 1; i < n; ++i)
			{
				tmpAR = 0.0;
				for (int j = 0; j < p - 1; ++j)
				{
					tmpAR += arCoe[j] * data[i - j - 1];
				}
				tmpMA = 0.0;
				for (int j = 1; j < q; ++j)
				{
					tmpMA += maCoe[j] * errData[j];
				}
				
				for (int j = q - 1; j > 0; --j)
				{
					errData[j] = errData[j - 1];
				}
				errData[0] = random.nextGaussian() * Math.sqrt(maCoe[0]);
				
				sumErr += (data[i] - tmpAR - tmpMA) * (data[i] - tmpAR - tmpMA);
			}
//			return Math.log(sumErr) + (q + p + 1) * 2 / n;
			return (n - (q + p - 1)) * Math.log(sumErr / (n - (q + p - 1))) + (p + q) * 2;
			// return (n-(p-1))*Math.log(sumErr/(n-(p-1)))+(p+q-1)*Math.log(n-(p-1));		//AIC 最小二乘估计
		}
	}

	// Y-W方程求解
		/**
		 * @param garma	代表的是数据的协方差
		 * @return 返回经由Y-W方程求解的结果,其中最后数组的最后一个元素存储的是模型中的噪声方差
		 */
	public double [] YWSolve(double [] garma)
	{
		int order = garma.length - 1;
		double [] garmaPart = new double[order];
		System.arraycopy(garma, 1, garmaPart, 0, order);
		
		// 将协方差转换为矩阵的形式
		double [][] garmaArray = new double[order][order];
		for (int i = 0; i < order; ++i)
		{
			// 对角线
			garmaArray[i][i] = garma[0];
			
			//下三角
			int subIndex = i;
			for (int j = 0; j < i; ++j)
			{
				garmaArray[i][j] = garma[subIndex--];
			}
			
			//上三角
			int topIndex = i;
			for (int j = i + 1; j < order; ++j)
			{
				garmaArray[i][j] = garma[++topIndex];
			}
		}
		
		/* 调用了juma包,其实现了大部分对矩阵的操作 */
		/* 可能会存在矩阵不可逆的情况,在矩阵不可逆时可以通过将对角线元素全部增加1e-6做修正 */
		Matrix garmaMatrix = new Matrix(garmaArray);				
		Matrix garmaMatrixInverse = garmaMatrix.inverse();
		Matrix autoReg = garmaMatrixInverse.times(new Matrix(garmaPart, order));
		
		double [] result = new double[autoReg.getRowDimension() + 1];
		for (int i = 0; i < autoReg.getRowDimension(); ++i)
		{
			result[i] = autoReg.get(i, 0);
		}
		
		double sum = 0.0;
		for (int i = 0; i < order; ++i)
		{
			sum += result[i] * garma[i];
		}
		result[result.length - 1] = garma[0] - sum;
		return result;
	}

	// Levinson 方法求解
	
		/**
		 * @param garma  代表的是数据的协方差
		 * @return	返回结果的第一行元素代表的是在迭代过程中的方差,
		 * 其余的元素代表的是迭代过程中存储的系数
		 */
	public double [][] LevinsonSolve(double [] garma)
	{
		int order = garma.length - 1;
		double [][] result = new double[order + 1][order + 1];
		double [] sigmaSq = new double[order + 1];
		
		sigmaSq[0] = garma[0];
		result[1][1] = garma[1] / sigmaSq[0];
		sigmaSq[1] = sigmaSq[0] * (1.0 - result[1][1] * result[1][1]);
		for (int k = 1; k < order; ++k)
		{
			double sumTop = 0.0;
			double sumSub = 0.0;
			for (int j = 1; j <= k; ++j)
			{
				sumTop += garma[k + 1 - j] * result[k][j];
				sumSub += garma[j] * result[k][j];
			}
			result[k + 1][k + 1] = (garma[k + 1] - sumTop) / (garma[0] - sumSub);
			for (int j = 1; j <= k; ++j)
			{
				result[k + 1][j] = result[k][j] - result[k + 1][k + 1] * result[k][k + 1 - j];
			} 
			sigmaSq[k + 1] = sigmaSq[k] * (1.0 - result[k + 1][k + 1] * result[k + 1][k + 1]);
		}
		result[0] = sigmaSq;
		
		return result;
	}

	// 求解AR(p)的系数
	
	/**
	 * @param originalData	原始数据
	 * @param p		模型的阶数
	 * @return		AR模型的系数
	 */
	public double [] computeARCoe(double [] originalData, int p)
	{
		double [] garma = this.autoCovData(originalData, p);		//p+1
		
		double [][] result = this.LevinsonSolve(garma);		//(p + 1) * (p + 1)
		double [] ARCoe = new double[p + 1];
		for (int i = 0; i < p; ++i)
		{
			ARCoe[i] = result[p][i + 1];
		}
		ARCoe[p] = result[0][p];		//噪声参数
		
//		 return this.YWSolve(garma);
		return ARCoe;
	}

	// 求解MA(q)的系数
	
	/**
	 * @param originalData   原始数据
	 * @param q			模型阶数
	 * @return			MA系数
	 */
	public double [] computeMACoe(double [] originalData, int q)
	{
		// 确定最佳的p
//		int p = 0;
//		double minAIC = Double.MAX_VALUE;
//		int len = originalData.length;
//		for (int i = 1; i < len; ++i)
//		{
//			double [] garma = this.autoCovData(originalData, i);
//			double [][] result = this.LevinsonSolve(garma);
//			
//			double [] ARCoe = new double[i + 1];
//			for (int k = 0; k < i; ++k)
//			{
//				ARCoe[k] = result[i][k + 1];
//			}
//			ARCoe[i] = result[0][i];
			double [] ARCoe = this.YWSolve(garma);
//			
//			Vector<double []> vec = new Vector<>();
//			vec.add(ARCoe);
//			double aic = this.getModelAIC(vec, originalData, 2);
//			if (aic < minAIC)
//			{
//				minAIC = aic;
//				p = i;
//			}	
//		}
		
		int p = (int)Math.log(originalData.length);
		
//		System.out.println("The best p is " + p);
		// 求取系数
		double [] bestGarma = this.autoCovData(originalData, p);
		double [][] bestResult = this.LevinsonSolve(bestGarma);
		
		double [] alpha = new double[p + 1];
		alpha[0] = -1;
		for (int i = 1; i <= p; ++i)
		{
			alpha[i] = bestResult[p][i];
		}

//		double [] result = this.YWSolve(bestGarma);
//		double [] alpha = new double[p + 1];
//		alpha[0] = -1;
//		for (int i = 1; i <= p; ++i)
//		{
//			alpha[i] = result[i - 1];
//		}
		double [] paraGarma = new double[q + 1];
		for (int k = 0; k <= q; ++k)
		{
			double sum = 0.0;
			for (int j = 0; j <= p - k; ++j)
			{
				sum += alpha[j] * alpha[k + j];
			}
			paraGarma[k] = sum / bestResult[0][p];
		}
		
		double [][] tmp = this.LevinsonSolve(paraGarma);
		double [] MACoe = new double[q + 1];
		for (int i = 1; i < MACoe.length; ++i)
		{
			MACoe[i] = -tmp[q][i];
		}
		MACoe[0] = 1 / tmp[0][q];		//噪声参数
		
//		double [] tmp = this.YWSolve(paraGarma);
//		double [] MACoe = new double[q + 1];
//		System.arraycopy(tmp, 0, MACoe, 1, tmp.length - 1);
//		MACoe[0] = tmp[tmp.length - 1];
		
		return MACoe;
	}

	// 求解ARMA(p, q)的系数
	
	/**
	 * @param originalData		原始数据
	 * @param p			AR模型阶数
	 * @param q			MA模型阶数
	 * @return			ARMA模型系数
	 */
	public double [] computeARMACoe(double [] originalData, int p, int q)
	{
		double [] allGarma = this.autoCovData(originalData, p + q);
		double [] garma = new double[p + 1];
		for (int i = 0; i < garma.length; ++i)
		{
			garma[i] = allGarma[q + i];
		}
		double [][] arResult = this.LevinsonSolve(garma);
		
		// AR
		double [] ARCoe = new double[p + 1];
		for (int i = 0; i < p; ++i)
		{
			ARCoe[i] = arResult[p][i + 1];
		}
		ARCoe[p] = arResult[0][p];
//		double [] ARCoe = this.YWSolve(garma);
		
		// MA
		double [] alpha = new double[p + 1];
		alpha[0] = -1;
		for (int i = 1; i <= p; ++i)
		{
			alpha[i] = ARCoe[i - 1];
		}
		
		double [] paraGarma = new double[q + 1];
		for (int k = 0; k <= q; ++k)
		{
			double sum = 0.0;
			for (int i = 0; i <= p; ++i)
			{
				for (int j = 0; j <= p; ++j)
				{
					sum += alpha[i] * alpha[j] * allGarma[Math.abs(k + i - j)];
				}
			}
			paraGarma[k] = sum;
		}
		double [][] maResult = this.LevinsonSolve(paraGarma);
		double [] MACoe = new double[q + 1];
		for (int i = 1; i <= q; ++i)
		{
			MACoe[i] = maResult[q][i];
		}
		MACoe[0] = maResult[0][q];
		
//		double [] tmp = this.YWSolve(paraGarma);
//		double [] MACoe = new double[q + 1];
//		System.arraycopy(tmp, 0, MACoe, 1, tmp.length - 1);
//		MACoe[0] = tmp[tmp.length - 1];
		
		double [] ARMACoe = new double[p + q + 2];
		for (int i = 0; i < ARMACoe.length; ++i)
		{
			if (i < ARCoe.length)
			{
				ARMACoe[i] = ARCoe[i];
			}
			else
			{
				ARMACoe[i] = MACoe[i - ARCoe.length];
			}
		}
		return ARMACoe;
	}
}

到了这里,关于ARIMA实现(亲测可用)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 时序预测 | MATLAB实现AR、ARMA、ARIMA时间序列预测模型答疑

    基本介绍 AR 自回归模型(Autoregressive Model),通常简称为AR模型,是一种用于时间序列分析和预测的统计模型。它基于时间序列自身的历史值来预测未来值,通过将当前时刻的观测值与前一时刻的观测值之间的关系进行建模。 AR模型的基本思想是,当前时刻的值可以由之前时

    2024年02月09日
    浏览(63)
  • 时间序列ARIMA模型

    ARIMA模型的全称叫做自回归移动平均模型,全称是(ARIMA,Autoregressive Integrated Moving Average Model)。也记作ARIMA(p,d,q),是统计模型(statisticmodel)中最常见的一种用来进行时间序列预测的模型。 将非平稳时间序列转化为平稳时间序列然后将因变量仅对它的滞后值以及随机误差项的现值和

    2024年02月03日
    浏览(47)
  • 时间序列模型-ARIMA

    描述当前值与历史值之间的关系,用变量自身的历史数据对自身进行预测; 自回归模型必须满足平稳性的要求;(何为平稳性:见时间序列数据分析基本概念) p阶自回归过程的公式定义:         其中是当前值,是常数项,p是阶数,是自相关系数,是误差。 1.1.1 自回归

    2023年04月08日
    浏览(88)
  • 使用Python构造ARIMA模型

    基于统计的方法是经典的时间序列预测模型,也是财务时间序列预测的主要方法。他们假设时间序列是由随机冲击的线性集合产生的。一种有代表性的方法是ARMA模型,它是AR和MA模型的组合。它被扩展到非平稳时间序列预测,称为自回归综合移动平均(ARIMA),它结合了差分技

    2024年02月07日
    浏览(34)
  • 【ARIMA-LSTM】合差分自回归移动平均方法-长短期记忆神经网络研究(Python代码实现)

      💥💥💞💞 欢迎来到本博客 ❤️❤️💥💥 🏆博主优势: 🌞🌞🌞 博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。 ⛳️ 座右铭: 行百里者,半于九十。 📋📋📋 本文目录如下: 🎁🎁🎁 目录 💥1 概述 1.1 ARIMA模型 1.2 LSTM 模型 📚2 运行结果 🎉3 参考文献

    2024年02月10日
    浏览(61)
  • 【时间序列数据挖掘】ARIMA模型

    目录 0、前言 一、移动平均模型MA 二、自回归模型AR 三、自回归移动平均模型ARMA 四、自回归移动平均模型ARIMA 【总结】 传统时间序列分析模型: ARIMA模型是一个非常灵活的模型,对于时间序列的好多特征都能够进行描述,比如说平稳性特征,季节性特征,趋势性特征。 AR

    2024年02月07日
    浏览(48)
  • 机器学习——时间序列ARIMA模型(四):自相关函数ACF和偏自相关函数PACF用于判断ARIMA模型中p、q参数取值

    自相关函数反映了同一序列在不同时序的取值之间的相关性。 公式: A C F ( k ) = ρ k = C o v ( y t , y t − k ) V a r ( y t ) ACF(k)=rho_{k}=frac{Cov(y_{t},y_{t-k})}{Var(y_{t})} A CF ( k ) = ρ k ​ = Va r ( y t ​ ) C o v ( y t ​ , y t − k ​ ) ​ 其中分子用于求协方差矩阵,分母用于计算样本方差。求出

    2023年04月24日
    浏览(57)
  • 时序预测 | Python实现ARIMA-LSTM差分自回归移动平均模型结合长短期记忆神经网络时间序列预测

    预测效果 基本介绍 时序预测 | Python实现ARIMA-LSTM差分自回归移动平均模型结合长短期记忆神经网络时间序列预测 直接替换数据即可用 适合新手小白 附赠案例数据 可直接运行 程序设计 完整程序和数据下载方式私信博主回复: Python实现ARIMA-LSTM差分自回归移动平均模型结合长

    2024年02月07日
    浏览(51)
  • Python时间序列分析--ARIMA模型实战案例

    **本文将介绍使用Python来完成时间序列分析ARIMA模型的完整步骤与流程,绘制时序图,平稳性检验,单位根检验,白噪声检验,模型定阶,参数估计,模型检验等完整步骤。Python建立时间序列分析–ARIMA模型实战案例 时间序列指的是将带有同一指标单位的数值按照产生时间的先

    2024年01月17日
    浏览(50)
  • ARIMA模型——非平稳序列的随机性分析

    一、拟合 ARIMA 模型 ARIMA 是先运用差分运算得到平稳序列,再对平稳序列建立 ARMA 模型。差分运算可用 diff 函数完成,命令格式为: diff(x,lag=,differences=) 其中: x: 序列名       lag: 差分的步长(默认 lag=1 )       differences: 差分次数(默认 differences=1 ) 常用命令有: 1 阶

    2023年04月17日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包