-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathget_crosscovariance.m
136 lines (117 loc) · 3.58 KB
/
get_crosscovariance.m
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
function [ACF,CCV] = get_crosscovariance(pars,lagmax)
%-------------------------------------------------------------------------%
%
% Purpose: calculate stationary cross-covariance function in switching state-space model
% y(t) = C(S(t)) x(t) + w(t)
% x(t) = A(1,S(t)) x(t-1) + ... + A(p,S(t)) x(t-p) + v(t)
%
% Usage: [ACF,CCV] = get_crosscovariance(pars,lagmax)
%
% Inputs: pars - struct with fields
% A - state transition matrices ([r,r,p,M])
% C - observation matrices ([N,r], [N,r,M], or [])
% Q - state noise covariance matrices ([N,N,M])
% R - observation noise covariance matrix ([N,N] or [])
% lagmax - maximum lag for autocorrelation function (optional,
% default = 5*p)
%
% Outputs: ACF - autocorrelation functions ([N,lagmax+1])
% CCV - cross-covariance functions ([N,N,lagmax+1,M])
%
%-------------------------------------------------------------------------%
narginchk(1,2);
% Model parameters
A = pars.A;
if isfield(pars,'C')
C = pars.C;
else
C = [];
end
Q = pars.Q;
if isfield(pars,'R')
R = pars.R;
else
R = [];
end
% Model dimensions
[r,~,p,M] = size(A);
if ~exist('lagmax','var') || isempty(lagmax)
lagmax = 5*p;
end
lagmax = round(lagmax);
assert(lagmax >= 0)
% Check arguments C and R
if ~isempty(C) || ~isempty(R)
assert(~isempty(C) && ~isempty(R))
assert(ismatrix(R) && size(R,1) == size(R,2))
assert(size(C,1) == size(R,1))
N = size(C,1);
if ismatrix(C)
C = repmat(C,1,1,M);
end
else
N = r;
end
% Initialize various quantities
ACF = NaN(N,lagmax+1,M); % auto-correlation diag(cor(x(t),x(t-l)|S(t)=j)
CCV = NaN(N,N,lagmax+1,M); % cross-correlation (cov(x(t),x(t-l)|S(t)=j)
Abig = zeros(p*r); % container for A
if p > 1
Abig(r+1:end,1:end-r) = eye((p-1)*r);
end
Qbig = zeros(p*r); % container for Q
idx_acf = (repmat(eye(N),1,1,lagmax+1) == 1);
% Calculations
for j = 1:M
Aj = A(:,:,:,j);
Abig(1:r,:) = reshape(Aj,r,p*r);
Qbig(1:r,1:r) = Q(:,:,j);
if all(Aj(:) == 0)
Vbig = Qbig;
else
eigA = abs(eig(Abig));
if any(eigA >= 1)
continue
elseif min(eigA) <= 1e-10 * max(eigA) % case: Abig numerically singular
Vbig = get_covariance_aux(Aj,Q(:,:,j)); % Cov(X(t)|S(t)=j)
else % case: Abig full rank
B = (Abig.' * Abig)\(Abig.');
Vbig = sylvester(B,-Abig.',B*Qbig);
end
end
Vbig = 0.5 * (Vbig + Vbig.');
if isempty(C)
COV = Vbig(1:r,1:r);
else
COV = (C(:,:,j) * Vbig(1:r,1:r) * C(:,:,j).') + R;
end
COV = 0.5 * (COV + COV');
if lagmax == 0
CCV(:,:,:,j) = COV;
ACF(:,:,j) = ones(N,1);
continue
end
CCV_tmp = Vbig; % Cov(X(t),X(t-l)|S(t)=j)
CCVj = zeros(r,r,lagmax+1); % Cov(x(t),x(t-l)|S(t)=j)
CCVj(:,:,1) = Vbig(1:r,1:r);
A_up = Abig(1:r,:);
for l = 1:lagmax
B = A_up * CCV_tmp;
CCV_tmp(r+1:end,:) = CCV_tmp(1:end-r,:);
CCV_tmp(1:r,:) = B;
CCVj(:,:,l+1) = CCV_tmp(1:r,1:r);
end
if ~isempty(C) % Cov(y(t),y(t-l)|S(t)=j)
CCVj = C(:,:,j) * reshape(CCVj,r,r*(lagmax+1));
CCVj = reshape(CCVj,N,r,lagmax+1);
CCVj = permute(CCVj,[1,3,2]);
CCVj = reshape(CCVj,N*(lagmax+1),r) * C(:,:,j).';
CCVj = reshape(CCVj,N,lagmax+1,N);
CCVj = permute(CCVj,[1,3,2]);
CCVj(:,:,1) = COV;
end
CCV(:,:,:,j) = CCVj;
% Autocorrelation
ACF(:,:,j) = reshape(CCVj(idx_acf),N,lagmax+1);
ACF(:,:,j) = ACF(:,:,j) ./ diag(COV);
end