Debian Codegraph @

contrib/ms-sbvar/switch_dw/state_space/fwz_msv_msre.c

raw
1 /*
2 * Copyright (C) 1996-2011 Roger Farmer, Daniel Waggoner and Tao Zha
3 *
4 * This free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * If you did not received a copy of the GNU General Public License
15 * with this software, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "dw_matrix.h"
19 #include "dw_matrix_array.h"
20 #include "dw_matrix_rand.h"
21 #include "bmatrix.h"
22 #include "dw_error.h"
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 /*
28 Computes MSV solution of
29
30 A(s(t) y(t) = B(s(t) y(t-1) + Psi(s(t)) epsilon(t) + Pi eta(t)
31
32 using Newton's Method. Assumes that Pi' = [zeros(s,n-s) I(s)] and
33 that the first n-s rows of A[i] are linearly independent. is invertible. P is the transition matrix and P(i,j) is the
34 probability that s(t+1)=i given that s(t)=j. Note that the columns of P
35 must sum to one. x is the initial value. max_iteration is the maximum
36 number of iterations of Newton's method before failing and tolerance is
37 the convergence criterion.
38
39 The solution is of the form
40
41 y(t) = V{s(t)}*F1{s(t)}*y(t-1) + V{s(t)}*G1{s(t)}*epsilon(t)
42
43 eta(t) = F2{s(t)}*x(t-1) + G2{s(t)}*epsilont(t)
44
45 A positive value of err is the number of iterations needed to obtain
46 convergence and indicates success. A negitive value of err is the number of
47 iterations before the method terminated without convergence and indicates
48 failure.
49 */
50 int fwz_msv_msre(TMatrix *F1, TMatrix *F2, TMatrix *G1, TMatrix *G2, TMatrix *V, TMatrix P, TMatrix *A, TMatrix *B, TMatrix *Psi, int s, TVector z, int max_iteration, PRECISION tolerance)
51 {
52 int i, j, k, count, h, n, r, ns, verbose_errors, terminal_errors;
53 TVector g;
54 TMatrix *ZI, *IZ, **BU, **BU_row, **BU_col, *C1, *C2, *U, W0, W1, W2, W3, W4, W5, W6, D, Q, R, G;
55 PRECISION norm;
56
57 n=RowM(A[0]);
58 r=Psi[0] ? ColM(Psi[0]) : 0;
59 h=DimV(z)/(s*(n-s));
60 ns=n-s;
61
62 W0=CreateMatrix(s,ns);
63 W1=CreateMatrix(ns,ns);
64 W2=CreateMatrix(s*ns,s*ns);
65 W3=CreateMatrix(s,s);
66 W4=CreateMatrix(s*ns,s*ns);
67 W5=CreateMatrix(s,n);
68 W6=CreateMatrix(n,ns);
69
70 g=CreateVector(h*s*ns);
71 D=CreateMatrix(h*s*ns,h*s*ns);
72 IZ=dw_CreateArray_matrix(h);
73 ZI=dw_CreateArray_matrix(h);
74 C1=dw_CreateArray_matrix(h);
75 C2=dw_CreateArray_matrix(h);
76 BU=dw_CreateRectangularArray_matrix(h,h);
77 BU_row=dw_CreateRectangularArray_matrix(h,h);
78 BU_col=dw_CreateRectangularArray_matrix(h,h);
79 U=dw_CreateArray_matrix(h);
80
81 Q=CreateMatrix(n,n);
82 R=CreateMatrix(n,n);
83
84 verbose_errors=dw_SetVerboseErrors(NO_ERR);
85 terminal_errors=dw_SetTerminalErrors(NO_ERR);
86 for (j=h-1; j >= 0; j--)
87 {
88 if (!(U[j]=Inverse_LU((TMatrix)NULL,A[j])))
89 {
90 Transpose(R,A[j]);
91 QR(Q,R,R);
92 Transpose(R,R);
93 C1[j]=SubMatrix((TMatrix)NULL,R,ns,0,s,ns);
94 SubMatrix(W1,R,0,0,ns,ns);
95 if (!Inverse_LU(W1,W1))
96 {
97 dw_SetVerboseErrors(verbose_errors);
98 dw_SetTerminalErrors(terminal_errors);
99 printf("fwz_msv_msre(): First %d rows of A[%d] not linearly independent.\n",ns,j);
100 dw_exit(0);
101 }
102 ProductMM(C1[j],C1[j],W1);
103 C2[j]=SubMatrix((TMatrix)NULL,R,ns,ns,s,s);
104 InitializeMatrix(U[j]=CreateMatrix(n,n),0.0);
105 for (k=ns; k < n; k++) ElementM(U[j],k,k)=1.0;
106 InsertSubMatrix(U[j],W1,0,0,0,0,ns,ns);
107 ProductMM(U[j],Q,U[j]);
108 }
109 else
110 C2[j]=IdentityMatrix((TMatrix)NULL,s);
111
112 InitializeMatrix(IZ[j]=CreateMatrix(n,ns),0.0);
113 for (k=n-s-1; k >= 0; k--) ElementM(IZ[j],k,k)=-1.0;
114
115 InitializeMatrix(ZI[j]=CreateMatrix(s,n),0.0);
116 for (k=s-1; k >= 0; k--) ElementM(ZI[j],k,ns+k)=1.0;
117
118 for (i=h-1; i >= 0; i--)
119 {
120 BU[i][j]=ProductMM((TMatrix)NULL,B[i],U[j]);
121 ProductMS(BU[i][j],BU[i][j],-ElementM(P,i,j));
122 BU_row[i][j]=SubMatrix((TMatrix)NULL,BU[i][j],0,0,ns,n);
123 BU_col[i][j]=SubMatrix((TMatrix)NULL,BU[i][j],0,ns,n,s);
124 }
125 }
126 dw_SetVerboseErrors(verbose_errors);
127 dw_SetTerminalErrors(terminal_errors);
128
129 for (count=max_iteration; count > 0; count--)
130 {
131 // divides and reshapes z into h matrices
132 for (k=h-1; k >= 0; k--)
133 {
134 if (MajorForm(W0))
135 memcpy(pElementM(W0),pElementV(z)+k*s*(ns),s*(ns)*sizeof(PRECISION));
136 else
137 bTranspose(pElementM(W0),pElementV(z)+k*s*(ns),s,ns,1);
138
139 InsertSubMatrix(IZ[k],W0,ns,0,0,0,s,ns);
140 if (C1[k])
141 {
142 ProductMM(W0,C2[k],W0);
143 SubtractMM(W0,W0,C1[k]);
144 }
145 InsertSubMatrix(ZI[k],W0,0,0,0,0,s,ns);
146 }
147
148 for (j=h-1; j >= 0; j--)
149 {
150 for (i=h-1; i >= 0; i--)
151 {
152 ProductMM(W1,BU_row[i][j],IZ[j]);
153 Transpose(W1,W1);
154 KroneckerProduct(W2,W1,C2[i]);
155 if (i == j)
156 {
157 ProductMM(W3,ZI[0],BU_col[0][j]);
158 for (k=h-1; k > 0; k--) UpdateProductMM(1.0,W3,1.0,ZI[k],BU_col[k][j]);
159 for (k=ns-1; k >= 0; k--)
160 InsertSubMatrix(W4,W3,k*s,k*s,0,0,s,s);
161 AddMM(W2,W2,W4);
162 }
163 InsertSubMatrix(D,W2,j*s*(n-s),i*s*(n-s),0,0,s*(n-s),s*(n-s));
164 }
165 }
166
167 for (j=h-1; j >= 0; j--)
168 {
169 InitializeMatrix(W5,0.0);
170 for (i=h-1; i >= 0; i--) UpdateProductMM(1.0,W5,-ElementM(P,i,j),ZI[i],B[i]);
171 ProductMM(W6,U[j],IZ[j]);
172 ProductMM(W0,W5,W6);
173
174 // vectorize
175 if (MajorForm(W0))
176 memcpy(pElementV(g)+j*s*(n-s),pElementM(W0),s*(n-s)*sizeof(PRECISION));
177 else
178 bTranspose(pElementV(g)+j*s*(n-s),pElementM(W0),s,n-s,0);
179 }
180
181 norm=Norm(g);
182 InverseProductMV(g,D,g);
183 SubtractVV(z,z,g);
184
185 if (norm < tolerance) break;
186 }
187
188 // Convert solution
189 InitializeMatrix(R,0.0);
190 for (k=n-1; k >= 0; k--) ElementM(R,k,k)=1.0;
191 G=CreateMatrix(n,r);
192 for (k=h-1; k >= 0; k--)
193 {
194 if (MajorForm(W0))
195 memcpy(pElementM(W0),pElementV(z)+k*s*(ns),s*(ns)*sizeof(PRECISION));
196 else
197 bTranspose(pElementM(W0),pElementV(z)+k*s*(ns),s,ns,1);
198 InsertSubMatrix(IZ[k],W0,ns,0,0,0,s,ns);
199
200 if (C1[k])
201 {
202 ProductMM(W0,C2[k],W0);
203 SubtractMM(W0,W0,C1[k]);
204 }
205 InsertSubMatrix(R,W0,ns,0,0,0,s,ns);
206
207 ProductMM(V[k],U[k],IZ[k]);
208 MinusM(V[k],V[k]);
209
210 ProductMM(Q,R,B[k]);
211 SubMatrix(F1[k],Q,0,0,ns,n);
212 SubMatrix(F2[k],Q,ns,0,s,n);
213 ProductMM(G,R,Psi[k]);
214 SubMatrix(G1[k],G,0,0,ns,r);
215 SubMatrix(G2[k],G,ns,0,s,r);
216 }
217
218 // Cleanup
219 FreeMatrix(G);
220 FreeMatrix(Q);
221 FreeMatrix(R);
222 dw_FreeArray(U);
223 dw_FreeArray(BU_col);
224 dw_FreeArray(BU_row);
225 dw_FreeArray(BU);
226 dw_FreeArray(C2);
227 dw_FreeArray(C1);
228 dw_FreeArray(ZI);
229 dw_FreeArray(IZ);
230 FreeMatrix(D);
231 FreeVector(g);
232 FreeMatrix(W6);
233 FreeMatrix(W5);
234 FreeMatrix(W4);
235 FreeMatrix(W3);
236 FreeMatrix(W2);
237 FreeMatrix(W1);
238 FreeMatrix(W0);
239
240 return (count) ? max_iteration-count+1 : -max_iteration;
241 }
242
243 /*
244 Verifies that
245
246 x(t) = V(s(t))(F1(s(t))x(t-1) + G1(s(t))epsilon(t)
247 eta(t) = F2(s(t))x(t-1) + G2(s(t))epsilon(t)
248
249 is a solution of
250
251 A(s(t)) x(t) = B(s(t)) x(t-1) + Psi(s(t) epsilon(t) + Pi eta(t)
252
253 where Pi' = [zeros(s,n-s) eye(s)] by verifying that
254
255 [A(j)*V(j) Pi] [ F1(j)
256 F2(j) ] = B(j)
257
258 [A(j)*V(j) Pi] [ G1(j)
259 G2(j) ] = Psi(j)
260
261 (P(i,1)*F2(1) + ... + P(i,h)*F2(h))*V(i) = 0
262 */
263 PRECISION verify_solution(TMatrix P, TMatrix *A, TMatrix *B, TMatrix *Psi, int s, TMatrix *F1, TMatrix *F2, TMatrix *G1, TMatrix *G2, TMatrix *V)
264 {
265 int i, j, n, r, h;
266 PRECISION diff=0.0, tmp;
267 TMatrix W, X, Y, Z, F, G;
268
269 n=RowM(A[0]);
270 r=Psi[0] ? ColM(Psi[0]) : 0;
271 h=RowM(P);
272
273 InitializeMatrix(X=CreateMatrix(n,n),0.0);
274 for (i=n-s; i < n; i++) ElementM(X,i,i)=1.0;
275
276 Y=CreateMatrix(n,n-s);
277 F=CreateMatrix(n,n);
278 G=CreateMatrix(n,r);
279 Z=CreateMatrix(s,n);
280 W=CreateMatrix(s,n-s);
281
282 for (j=0; j < h; j++)
283 {
284 ProductMM(Y,A[j],V[j]);
285 InitializeMatrix(X,0.0);
286 for (i=n-s; i < n; i++) ElementM(X,i,i)=1.0;
287 InsertSubMatrix(X,Y,0,0,0,0,n,n-s);
288 Inverse_LU(X,X);
289
290 InsertSubMatrix(F,F1[j],0,0,0,0,n-s,n);
291 InsertSubMatrix(F,F2[j],n-s,0,0,0,s,n);
292 UpdateProductMM(1.0,F,-1.0,X,B[j]);
293 tmp=MatrixNormEuclidean(F);
294 if (tmp > diff) diff=tmp;
295
296 InsertSubMatrix(G,G1[j],0,0,0,0,n-s,r);
297 InsertSubMatrix(G,G2[j],n-s,0,0,0,s,r);
298 UpdateProductMM(1.0,G,-1.0,X,Psi[j]);
299 tmp=MatrixNormEuclidean(G);
300 if (tmp > diff) diff=tmp;
301 }
302
303 for (i=0; i < h; i++)
304 {
305 InitializeMatrix(Z,0.0);
306 for (j=0; j < h; j++) UpdateM(1.0,Z,ElementM(P,j,i),F2[j]);
307 ProductMM(W,Z,V[i]);
308 tmp=MatrixNormEuclidean(W);
309 if (tmp > diff) diff=tmp;
310 }
311
312 FreeMatrix(W);
313 FreeMatrix(Z);
314 FreeMatrix(G);
315 FreeMatrix(F);
316 FreeMatrix(Y);
317 FreeMatrix(X);
318
319 return diff;
320 }
321
322 /*******************************************************************************/
323 /******************************** Test MSV Code ********************************/
324 /*******************************************************************************/
325 //#define FWZ_Example
326 void TestMSVCode(void)
327 {
328 TMatrix P, *A, *B, *Psi, *F1, *F2, *G1, *G2, *V;
329 TVector x;
330 int err, i, j;
331 PRECISION error, scale;
332
333 #ifdef FWZ_Example
334 int r=3, s=2, n=7, h=2;
335 TVector tau, kappa, beta, gamma1, gamma2, rhoD, rhoS, rhoR, sigmaD, sigmaS, sigmaR;
336 #else
337 int r=1, s=2, n=4, h=3;
338 #endif
339
340 P=CreateMatrix(h,h);
341 if (h == 1)
342 ElementM(P,0,0)=1.0;
343 else if (h == 2)
344 {
345 ElementM(P,0,0)=0.9872; ElementM(P,0,1)=0.0;
346 ElementM(P,1,0)=0.0128; ElementM(P,1,1)=1.0;
347 }
348 else
349 {
350 dw_UniformMatrix(P);
351 for (j=h-1; j >= 0; j--)
352 {
353 scale=ElementM(P,0,j);
354 for (i=h-1; i > 0; i--) scale+=ElementM(P,i,j);
355 for (i=h-1; i >= 0; i--) ElementM(P,i,j)/=scale;
356 }
357 }
358
359 A=dw_CreateArray_matrix(h);
360 B=dw_CreateArray_matrix(h);
361 Psi=dw_CreateArray_matrix(h);
362 for (i=0; i < h; i++)
363 {
364 A[i]=CreateMatrix(n,n);
365 B[i]=CreateMatrix(n,n);
366 Psi[i]=CreateMatrix(n,r);
367 }
368
369 x=CreateVector(s*(n-s)*h);
370
371 F1=dw_CreateArray_matrix(h);
372 F2=dw_CreateArray_matrix(h);
373 G1=dw_CreateArray_matrix(h);
374 G2=dw_CreateArray_matrix(h);
375 V=dw_CreateArray_matrix(h);
376 for (i=0; i < h; i++)
377 {
378 F1[i]=CreateMatrix(n-s,n);
379 F2[i]=CreateMatrix(s,n);
380 G1[i]=CreateMatrix(n-s,r);
381 G2[i]=CreateMatrix(s,r);
382 V[i]=CreateMatrix(n,n-s);
383 }
384
385 while (1)
386 {
387 #ifdef FWZ_Example
388 tau=CreateVector(h); ElementV(tau,0)=0.69; ElementV(tau,1)=0.54;
389 kappa=CreateVector(h); ElementV(kappa,0)=0.77; ElementV(kappa,1)=0.58;
390 beta=CreateVector(h); ElementV(beta,0)=0.997; ElementV(beta,1)=0.993;
391 gamma1=CreateVector(h); ElementV(gamma1,0)=0.77; ElementV(gamma1,1)=2.19;
392 gamma2=CreateVector(h); ElementV(gamma2,0)=0.17; ElementV(gamma2,1)=0.30;
393 rhoD=CreateVector(h); ElementV(rhoD,0)=0.68; ElementV(rhoD,1)=0.83;
394 rhoS=CreateVector(h); ElementV(rhoS,0)=0.82; ElementV(rhoS,1)=0.85;
395 rhoR=CreateVector(h); ElementV(rhoR,0)=0.60; ElementV(rhoR,1)=0.84;
396 sigmaD=CreateVector(h); ElementV(sigmaD,0)=0.27; ElementV(sigmaD,1)=0.18;
397 sigmaS=CreateVector(h); ElementV(sigmaS,0)=0.87; ElementV(sigmaS,1)=0.37;
398 sigmaR=CreateVector(h); ElementV(sigmaR,0)=0.23; ElementV(sigmaR,1)=0.18;
399
400 for (i=0; i < h; i++)
401 {
402 InitializeMatrix(A[i],0.0);
403
404 ElementM(A[i],0,0)=1.0; ElementM(A[i],0,1)=-1.0; ElementM(A[i],0,3)=-ElementV(tau,i); ElementM(A[i],0,4)=ElementV(tau,i); ElementM(A[i],0,5)=-1.0;
405 ElementM(A[i],1,0)=-ElementV(kappa,i); ElementM(A[i],1,2)=1.0; ElementM(A[i],1,3)=-ElementV(beta,i); ElementM(A[i],1,6)=-1.0;
406 ElementM(A[i],2,0)=-(1-ElementV(rhoR,i))*ElementV(gamma2,i); ElementM(A[i],2,2)=-(1-ElementV(rhoR,i))*ElementV(gamma1,i); ElementM(A[i],2,4)=1.0;
407 ElementM(A[i],3,5)=1.0;
408 ElementM(A[i],4,6)=1.0;
409 ElementM(A[i],5,0)=1.0;
410 ElementM(A[i],6,2)=1.0;
411
412 // Test A singular
413 if (i == 0) ElementM(A[i],6,2)=0.0;
414
415 InitializeMatrix(B[i],0.0);
416 ElementM(B[i],2,4)=ElementV(rhoR,i);
417 ElementM(B[i],3,5)=ElementV(rhoD,i);
418 ElementM(B[i],4,6)=ElementV(rhoS,i);
419 ElementM(B[i],5,1)=1.0;
420 ElementM(B[i],6,3)=1.0;
421
422 InitializeMatrix(Psi[i],0.0);
423 ElementM(Psi[i],2,2)=ElementV(sigmaR,i);
424 ElementM(Psi[i],3,0)=ElementV(sigmaD,i);
425 ElementM(Psi[i],4,1)=ElementV(sigmaS,i);
426 }
427 #else
428 for (i=0; i < h; i++)
429 {
430 dw_NormalMatrix(A[i]);
431 dw_NormalMatrix(B[i]); ProductMS(B[i],B[i],0.001);
432 dw_NormalMatrix(Psi[i]);
433 }
434 #endif
435
436 dw_NormalVector(x);
437 //InitializeVector(x,0.0);
438
439 err=fwz_msv_msre(F1,F2,G1,G2,V,P,A,B,Psi,s,x,10000,1.0e-8);
440
441 for (i=0; i < h; i++)
442 {
443 printf("F1[%d]\n",i);
444 dw_PrintMatrix(stdout,F1[i],"%lf ");
445
446 printf("F2[%d]\n",i);
447 dw_PrintMatrix(stdout,F2[i],"%lf ");
448
449 printf("G1[%d]\n",i);
450 dw_PrintMatrix(stdout,G1[i],"%lf ");
451
452 printf("G2[%d]\n",i);
453 dw_PrintMatrix(stdout,G2[i],"%lf ");
454
455 printf("V[%d]\n",i);
456 dw_PrintMatrix(stdout,V[i],"%lf ");
457 }
458
459 error=verify_solution(P,A,B,Psi,s,F1,F2,G1,G2,V);
460 printf("return code (should be positive): %d\n",err);
461 printf("error (should be approximately zero): %le\n",error);
462 //getchar();
463 }
464 }
465
466 int main(void)
467 {
468 TestMSVCode();
469 return 0;
470 }
471
472 /*******************************************************************************/
473 /*******************************************************************************/
474 /*******************************************************************************/