1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
function [F,H,Q,R,initx, initV] = AR_to_SS(coef, C, y)
%
% Convert a vector auto-regressive model of order k to state-space form.
% [F,H,Q,R] = AR_to_SS(coef, C, y)
%
% X(i) = A(1) X(i-1) + ... + A(k) X(i-k+1) + v, where v ~ N(0, C)
% and A(i) = coef(:,:,i) is the weight matrix for i steps ago.
% We initialize the state vector with [y(:,k)' ... y(:,1)']', since
% the state vector stores [X(i) ... X(i-k+1)]' in order.
[s s2 k] = size(coef); % s is the size of the state vector
bs = s * ones(1,k); % size of each block
F = zeros(s*k);
for i=1:k
F(block(1,bs), block(i,bs)) = coef(:,:,i);
end
for i=1:k-1
F(block(i+1,bs), block(i,bs)) = eye(s);
end
H = zeros(1*s, k*s);
% we get to see the most recent component of the state vector
H(block(1,bs), block(1,bs)) = eye(s);
%for i=1:k
% H(block(1,bs), block(i,bs)) = eye(s);
%end
Q = zeros(k*s);
Q(block(1,bs), block(1,bs)) = C;
R = zeros(s);
initx = zeros(k*s, 1);
for i=1:k
initx(block(i,bs)) = y(:, k-i+1); % concatenate the first k observation vectors
end
initV = zeros(k*s); % no uncertainty about the state (since perfectly observable)
|