我正在尝试为以下1维AR模型实现卡尔曼滤波器的基本方程式:
x(t) = a_1x(t-1) + a_2x(t-2) + w(t)
y(t) = Cx(t) + v(t);
KF状态空间模型:
x(t+1) = Ax(t) + w(t)
y(t) = Cx(t) + v(t)
w(t) = N(0,Q)
v(t) = N(0,R)
,其中
% A - state transition matrix
% C - observation (output) matrix
% Q - state noise covariance
% R - observation noise covariance
% x0 - initial state mean
% P0 - initial state covariance
预测和更新的代码是:
function [xpred, Ppred] = predict(x, P, F, Q)
xpred = A*x;
Ppred = A*P*A’ + Q;
function [nu, S] = innovation(xpred, Ppred, z, H, R)
nu = y - C*xpred; %% innovation
S = R + C*Ppred*C’; %% innovation covariance
function [xnew, Pnew] = innovation_update(xpred, Ppred, n
u, S, C)
K = Ppred*C’*inv(S); %% Kalman gain
xnew = xpred + K*nu; %% new state
Pnew = Ppred - K*S*K’; %% new covariance
我正在尝试遵循实现https://github.com/cswetenham/pmr/blob/master/toolboxes/lds/kalmansmooth.m但是因为参数A,C作为矩阵给出而感到困惑。
我是否必须将A,C表示为矩阵?我如何为我的案件代表A,C,Q,R?
如何初始化A,C,Q,R?因为,我只有一个变量,Q,R应该是1乘1.但是,其他变量呢? 请帮忙。
更新:这是完整的实施
%%% Matlab script to simulate data and process usiung Kalman for the state
%%% estimation of AR(2) time series.
% Linear system representation
% x_n+1 = A x_n + Bw_n
% y_n = Cx_n + v_n
% w = N(0,Q); v = N(0,R)
clc
clear all
T = 100; % number of data samples
order = 2;
% True coefficients of AR model
a1 = 0.195;
a2 = -0.95;
A = [ a1 a2;
0 1 ];
C = [ 1 0 ];
B = [1;
0];
x =[ rand(order,1) zeros(order,T-1)];
sigma_2_w =1; % variance of the excitation signal for driving the AR model(process noise)
sigma_2_v = 0.01; % variance of measure noise
Q=eye(order);
P=Q;
%Simulate AR model time series, x;
sqrtW=sqrtm(sigma_2_w);
%simulation of the system
for t = 1:T-1
x(:,t+1) = A*x(:,t) + B*sqrtW*randn(1,1);
end
%noisy observation
y = C*x + sqrt(sigma_2_v)*randn(1,T);
R=sigma_2_v*diag(diag(x));
R = diag(R);
z = zeros(1,length(y));
z = y;
x0=mean(y);
for i=1:T-1
[xpred, Ppred] = predict(x0,P, A, Q);
[nu, S] = innovation(xpred, Ppred, z(i), C, R);
[xnew, P] = innovation_update(xpred, Ppred, nu, S, C);
end
%plot
xhat = xnew';
plot(xhat(:,1),'red');
hold on;
plot(x(:,1));
function [xpred, Ppred] = predict(x0,P, A, Q)
xpred = A*x0;
Ppred = A*P*A' + Q;
end
function [nu, S] = innovation(xpred, Ppred, y, C, R)
nu = y - C*xpred; %% innovation
S = R + C*Ppred*C'; %% innovation covariance
end
function [xnew, Pnew] = innovation_update(xpred, Ppred, nu, S, C)
K = Ppred*C'*inv(S); %% Kalman gain
xnew = xpred + K*nu; %% new state
Pnew = Ppred - Ppred*K*C; %% new covariance
end
我有3个问题: (1)矩阵R的初始化在尺寸和值方面是否正确?
R=sigma_2_v*diag(diag(x));
R = diag(R);
其中x是清洁时间系列。 (2)当创建时间序列的噪声观察时,我使用了除R之外的不同方差。我使用了sigma_2_v。我应该使用R还是sigma_2_v?这是我创建嘈杂时间序列的行
y = C*x + sqrt(sigma_2_v)*randn(1,T);
(3)代码抛出错误
Error using inv
Matrix must be square.
Error in innovation_update (line 2)
K = Ppred*C'*inv(S); %% Kalman gain
Error in Kalman_run (line 65)
[xnew, P] = innovation_update(xpred, Ppred, nu, S, C);
答案 0 :(得分:2)
有不同的方法将给定的AR流程表示为状态空间模型,但我相信如果它是AR(2)流程(您的模型是),则必须具有二维状态表示。所以A,C和Q需要是矩阵。
你有标量观测,因此C将是1x2矩阵。
A,Q,C和R可以从数据中学习(例如,如here所述)或使用您系统的先验知识进行设置。
仅仅因为你在每个时间步都有一个观察变量并不意味着Q和R都是1. Q是系统噪声协方差,R是观察噪声协方差 - 将它们设置为1只是指定协方差而不是做你认为它做的事情。