使用AR模型来预测轨迹

来源:互联网 发布:aws s3 php 上传 编辑:程序博客网 时间:2024/06/05 07:52
自回归模型(Autoregressive Model)是用自身做回归变量的过程,即利用前期若干时刻的随机变量的线性组合来描述以后某时刻随机变量的线性回归模型,它是时间序列中的一种常见形式。

简单地说就是预测第n+1个时间序列时,使用前n个时间序列,AR模型则假设第n+1个时间序列与前n个时间序列存在着线性关系, 如下图所示。更多信息请参考维基百科。

显然使用AR模型时,前n个时间序列前的参数的估计是这个模型的核心问题。

这里简略地说明参数估计方法:

联想线性回归中的参数估计中,给出的一组训练序列,已知其中存在线性关系,则我们会使用最小二乘法来估计y=theta'*x中的参数矩阵theta。

theta = (x'*x)^-1 * x' * y

但是这里显然不是完全相同的情况,不是线性回归的问题,而是一个预测问题。为此构造训练序列来求前n个时间序列前的参数。

构造训练序列:取若干组n+1个相连的时间序列中的前n个时间序列作为input,第n+1个时间序列作为output。(算法核心)

得到了前n个时间序列前的参数后,就可以用他们来预测了。

为了验证算法,使用matlab编程实现整个算法。

首先,生成一个被高斯噪声污染了的线性时间序列,然后对它们使用AR模型来分析,最后来预测并将预测结果和原始数据进行对比。

结果如下图所示:

黑实线是生成的线性时间序列,红圈是被高斯噪声污染了的线性时间序列,蓝色三角形是使用AR模型和高斯噪声污染了的线性时间序列得到的预测结果,绿色星形是从某时间开始单纯使用AR模型和其AR模型得到的历史预测时间序列的预测结果,可以看到随着时间推移,预测误差越来越大。


附源代码

close all;clear all;N = 100;nVerctor = (1:N)';realHeight = nVerctor * 0.1;plot(nVerctor, realHeight, 'k-');title('Graph of realHeight series');grid on;%add gaussian noisenoise = randn(size(nVerctor));realHeightAddNoise = realHeight + noise;hold on;plot(nVerctor, realHeightAddNoise, 'ro');title('Graph of realHeight series with gaussian noise');grid on;p = 24;fai = zeros(p, 1);%为了估计参数,取50对input & output, 借用梯度下降算法学习参数。y = realHeightAddNoise((p+1):N);%x = zeros(size(y, 1), p);for n = 1:size(x,1)x(n, :) = realHeightAddNoise(n:(p+n-1));end%theta=(x'x)^-1x'yfai = inv(x'*x)*(x'*y);reconstructSeries = ones(size(realHeightAddNoise));reconstructSeries(1:p) = realHeightAddNoise(1:p);reconstructSeries2 = ones(size(realHeightAddNoise));reconstructSeries2(1:p) = realHeightAddNoise(1:p);for n = (p+1):NreconstructSeries(n) = reconstructSeries((n-p):(n-1))' * fai;reconstructSeries2(n) = realHeightAddNoise((n-p):(n-1))' * fai;endhold on;plot(nVerctor, reconstructSeries, 'g*');title('Graph of reconstructSeries');grid on;hold on;plot(nVerctor, reconstructSeries2, 'b^');title('Graph of reconstructSeries2');grid on;


0 0
原创粉丝点击