Debian Codegraph @

tests/kalman/likelihood/compare_kalman_mex.m

raw
1 function [flag] = compare_kalman_mex(experience)
2
3
4 rng(experience.Seed);
5
6 N = experience.NumberOfSimulations;
7
8 pp = experience.Number0fObservedVariables*experience.Scale;
9 mm = experience.SizeOfTheStateVector*experience.Scale;
10 rr = experience.NumberOfStructuralShocks*experience.Scale;
11 measurement_error_flag = experience.MeasurementErrors;
12 gend = experience.NumberOfPeriods;
13
14 %% SET VARIOUS PARAMETERS
15 kalman_tol = 1e-12;
16 riccati_tol =1e-9;
17
18 %% SET THE STATE SPACE MODEL:
19
20 % I randomly choose the mm eigenvalues of the transition matrix.
21 TransitionEigenvalues = rand(mm, 1)*2-1;
22
23 % I randomly choose the mm eigenvectors of the transition matrix
24 TransitionEigenvectors = rand(mm, mm);
25
26 % I build the transition matrix
27 T = TransitionEigenvectors*diag(TransitionEigenvalues)/TransitionEigenvectors;
28
29 % I randomly choose matrix R
30 R = randn(mm, rr);
31
32 % I randomly choose the covariance matrix of the structural innovations
33 E = randn(rr, 20*rr);
34 Q = E*transpose(E)/(20*rr);
35
36 % If needed I randomly choose the covariance matrix of the measurement errors
37 if measurement_error_flag == 0
38 H = zeros(pp,1);
39 elseif measurement_error_flag == 1
40 H = rand(pp,1);
41 elseif measurement_error_flag == 2
42 E = randn(pp,20*pp);
43 H = E*transpose(E)/(20*pp);
44 H = (.1*eye(pp))*H*(.1*eye(pp));
45 else
46 disp('compare_kalman_mex: unknown option!')
47 end
48
49 % Set the selection vector (mf)
50 MF = transpose(randperm(mm));
51 mf = MF(1:pp);
52
53 % Compute ergodic variance of the state equation
54 P = lyapunov_symm(T,R*Q*R',riccati_tol,1.000001, riccati_tol);
55
56 if measurement_error_flag==0
57 HH = [];
58 elseif measurement_error_flag==1
59 HH = diag(H);
60 elseif measurement_error_flag==2
61 HH = H;
62 end
63
64 rng(experience.Seed*1938);
65
66 % Build datasets
67 Y = simul_state_space_model(T,R,Q,mf,gend*N, HH);
68
69 if isempty(HH)
70 HH = 0;
71 end
72
73 %
74 % Evaluate likelihoods
75 %
76
77 LIK_matlab_0 = NaN(N,1);
78 LIK_mex_0 = NaN(N,1);
79
80 flag = 0;
81 Zflag = 0;
82 tic;
83 for i=1:N
84 [LIK_matlab_0(i), ~] = kalman_filter(Y(:,(i-1)*gend+1:i*gend),1,gend,zeros(mm,1),P,kalman_tol,riccati_tol,0,0,T,Q,R,HH,mf,mm,pp,rr,Zflag,0,0);
85 end
86 T_matlab_0 = toc;
87
88 tic;
89 for i=1:N
90 [LIK_mex_0(i), ~] = kalman_filter_mex(Y(:,(i-1)*gend+1:i*gend),zeros(mm,1),P,kalman_tol,riccati_tol,T,Q,R,mf,Zflag,HH);
91 end
92 T_mex_0 = toc;
93
94 dprintf('Zflag = 0: Fortran Kalman filter is %5.2f times faster than its Matlab counterpart.', T_matlab_0/T_mex_0)
95
96 if max(abs((LIK_mex_0-LIK_matlab_0)./LIK_matlab_0))>1e-6
97 dprintf("Zflag = 0: discrepancy between Matlab and Fortran Kalman filter results!")
98 flag = 1;
99 end
100
101 LIK_matlab_1 = NaN(N,1);
102 LIK_mex_1 = NaN(N,1);
103
104 Zflag = 1;
105 Z = eye(mm);
106 Z = Z(mf,:);
107 tic;
108 for i=1:N
109 [LIK_matlab_1(i), ~] = kalman_filter(Y(:,(i-1)*gend+1:i*gend),1,gend,zeros(mm,1),P,kalman_tol,riccati_tol,0,0,T,Q,R,HH,Z,mm,pp,rr,Zflag,0,0);
110 end
111 T_matlab_1 = toc;
112
113 tic;
114 for i=1:N
115 [LIK_mex_1(i), ~] = kalman_filter_mex(Y(:,(i-1)*gend+1:i*gend),zeros(mm,1),P,kalman_tol,riccati_tol,T,Q,R,Z,Zflag,HH);
116 end
117 T_mex_1 = toc;
118
119 dprintf('Zflag = 1: Fortran Kalman filter is %5.2f times faster than its Matlab counterpart.', T_matlab_1/T_mex_1)
120
121 if max(abs((LIK_mex_1-LIK_matlab_1)./LIK_matlab_1))>1e-6
122 dprintf("Zflag = 1: discrepancy between Matlab and Fortran Kalman filter results!")
123 flag = 1;
124 end
125
126 if max(abs((LIK_mex_0 - LIK_mex_1)./LIK_mex_1))>1e-6
127 dprintf("Zflag = 1: discrepancy between results with and without the Zflag!")
128 flag = 1;
129 end
130
131 dprintf('Cost of Zflag==1 is %5.2f%% (matlab)', 100*(T_matlab_1-T_matlab_0)/T_matlab_0)
132 dprintf('Cost of Zflag==1 is %5.2f%% (mex)', 100*(T_mex_1-T_mex_0)/T_mex_0)