Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 1996-2011 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_state_space_forecast.h"
19 #include "dw_MSStateSpace.h"
20 #include "dw_matrix_array.h"
21 #include "dw_histogram.h"
22 #include "dw_error.h"
23 #include "dw_metropolis_simulation.h"
24 #include "dw_matrix_rand.h"
25 #include "dw_parse_cmd.h"
26 #include "dw_math.h"
27 #include "dw_std.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 /*
34 Assumes:
35 forecast : horizon x ny matrix or null pointer
36 horizon : positive integer - forecast horizon
37 z_initial : initial value of state vector z[T].
38 shocks : array of length horizon of fundamental shocks or null pointer.
39 If null pointer, then the shocks are all zero. Each vector is
40 of length nepsilon.
41 S : array of length horizon. S[t] is the base regime at time T+1+t.
42 model : pointer to valid TStateModel structure.
43
44 Results:
45 Computes forecast
46
47 Returns:
48 The matrix forecast upon success and null upon failure. If forecast is
49 null, then it created.
50 */
51 TMatrix dw_state_space_forecast(TMatrix forecast, int horizon, TVector z_initial, TVector *shocks, int *S, TStateModel *model)
52 {
53 T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta);
54 TVector z1, z2, y;
55 int i, t;
56
57 // create forecast if necessary
58 if (!forecast && !(forecast=CreateMatrix(horizon,statespace->ny)))
59 return (TMatrix)NULL;
60
61 // forecast
62 z1=CreateVector(statespace->nz);
63 z2=CreateVector(statespace->nz);
64 y=CreateVector(statespace->ny);
65 EquateVector(z1,z_initial);
66 for (t=0; t < horizon; t++)
67 {
68 ProductMV(z2,statespace->F[S[t]],z1);
69 AddVV(z1,statespace->b[S[t]],z2);
70 if (shocks)
71 {
72 ProductMV(z2,statespace->Phiz[S[t]],shocks[t]);
73 AddVV(z1,z1,z2);
74 }
75 ProductMV(y,statespace->H[S[t]],z1);
76 AddVV(y,statespace->a[S[t]],y);
77 for (i=statespace->ny-1; i >= 0; i--)
78 ElementM(forecast,t,i)=ElementV(y,i);
79 }
80 FreeVector(y);
81 FreeVector(z2);
82 FreeVector(z1);
83
84 return forecast;
85 }
86
87 /*
88 For 1 <= k < h, y[k][i] is null if the ith coordinate of y(t0+1+k) is
89 unrestricted and is its value otherwise. In general, t0 is the last index for
90 which we have full information. It must be the case that t0 <= nobs.
91 */
92 TVector* dw_state_space_mean_conditional_forecast(TVector *F, PRECISION ***y, int h, int t0, TStateModel *model)
93 {
94 T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta);
95 TVector *Pxi, *Pxi1, *SPxi, **IEz, **Ez1, **Ez, **SEz, **ISEz, SPzeta, SPs, u, *z;
96 TMatrix **IEzz, **Ezz1, **Ezz;
97 int i, k, s;
98
99 if ((t0 > statespace->t0) && !Filter(t0,model)) return (TVector*)NULL;
100 if ((t0 > model->t0) && !ForwardRecursion(t0,model)) return (TVector*)NULL;
101 if (((t0 < model->sv->t0) || (model->sv->t1 < t0)) && !sv_ComputeTransitionMatrix(t0,model->sv,model)) return (TVector*)NULL;
102
103 Pxi=dw_CreateArray_vector(h);
104 Pxi1=dw_CreateArray_vector(h);
105 SPxi=dw_CreateArray_vector(h);
106 IEz=dw_CreateRectangularArray_vector(h,statespace->zeta_modulus);
107 IEzz=dw_CreateRectangularArray_matrix(h,statespace->zeta_modulus);
108 Ez1=dw_CreateRectangularArray_vector(h,statespace->nstates);
109 Ezz1=dw_CreateRectangularArray_matrix(h,statespace->nstates);
110 Ez=dw_CreateRectangularArray_vector(h,statespace->nstates);
111 Ezz=dw_CreateRectangularArray_matrix(h,statespace->nstates);
112 SEz=dw_CreateRectangularArray_vector(h,statespace->nstates);
113 ISEz=dw_CreateRectangularArray_vector(h,statespace->zeta_modulus);
114 for (k=h-1; k >= 0; k--)
115 {
116 Pxi[k]=CreateVector(statespace->nstates);
117 Pxi1[k]=CreateVector(statespace->nstates);
118 SPxi[k]=CreateVector(statespace->nstates);
119 for (i=statespace->zeta_modulus-1; i >= 0; i--)
120 {
121 IEz[k][i]=CreateVector(statespace->nz);
122 IEzz[k][i]=CreateMatrix(statespace->nz,statespace->nz);
123 ISEz[k][i]=CreateVector(statespace->nz);
124 }
125 for (i=statespace->nstates-1; i >= 0; i--)
126 {
127 Ez1[k][i]=CreateVector(statespace->nz);
128 Ezz1[k][i]=CreateMatrix(statespace->nz,statespace->nz);
129 Ez[k][i]=CreateVector(statespace->nz);
130 Ezz[k][i]=CreateMatrix(statespace->nz,statespace->nz);
131 SEz[k][i]=CreateVector(statespace->nz);
132 }
133 }
134
135 ConditionalFilter(0,h-1,y,statespace->Ez[t0],statespace->Ezz[t0],model->V[t0],model->sv->Q,
136 Pxi,Pxi1,IEz,IEzz,Ez1,Ezz1,Ez,Ezz,statespace);
137
138 SmoothProbabilities_MSStateSpace(0,h-1,SPxi,Pxi,Pxi1,model->sv->Q);
139
140 SmoothMean_MSStateSpace(0,h-1,SEz,ISEz,Ez1,Ezz1,IEz,IEzz,SPxi,statespace);
141
142 SPzeta=CreateVector(statespace->zeta_modulus);
143 SPs=CreateVector(statespace->nbasestates);
144 u=CreateVector(statespace->ny);
145 z=dw_CreateArray_vector(statespace->nbasestates);
146 for (s=statespace->nbasestates-1; s >= 0; s--) z[s]=CreateVector(statespace->nz);
147
148 if (!F)
149 {
150 F=dw_CreateArray_vector(h);
151 for (k=h-1; k >= 0; k--)
152 F[k]=CreateVector(statespace->ny);
153 }
154
155 for (k=h-1; k >= 0; k--)
156 {
157 InitializeVector(F[k],0.0);
158 if (statespace->zeta_modulus > statespace->nbasestates)
159 {
160 IntegrateStatesSingle(SPzeta,SPxi[k],statespace->zeta_modulus,statespace->nbasestates,2);
161 IntegrateStatesSingleV(z,SPzeta,ISEz[k],statespace->nbasestates,statespace->zeta_modulus/statespace->nbasestates,2);
162 IntegrateStatesSingle(SPs,SPzeta,statespace->nbasestates,statespace->zeta_modulus/statespace->nbasestates,2);
163 for (s=statespace->nbasestates-1; s >= 0; s--)
164 {
165 ProductMV(u,statespace->H[s],z[s]);
166 AddVV(u,statespace->a[s],u);
167 LinearCombinationV(F[k],1.0,F[k],ElementV(SPs,s),u);
168 }
169 }
170 else
171 {
172 IntegrateStatesSingle(SPs,SPxi[k],statespace->nbasestates,statespace->zeta_modulus,2);
173 for (s=statespace->nbasestates-1; s >= 0; s--)
174 {
175 ProductMV(u,statespace->H[s],ISEz[k][s]);
176 AddVV(u,statespace->a[s],u);
177 LinearCombinationV(F[k],1.0,F[k],ElementV(SPs,s),u);
178 }
179 }
180 }
181
182 // Clean up
183 dw_FreeArray(z);
184 FreeVector(u);
185 FreeVector(SPs);
186 FreeVector(SPzeta);
187 dw_FreeArray(ISEz);
188 dw_FreeArray(SEz);
189 dw_FreeArray(Ezz);
190 dw_FreeArray(Ez);
191 dw_FreeArray(Ezz1);
192 dw_FreeArray(Ez1);
193 dw_FreeArray(IEzz);
194 dw_FreeArray(IEz);
195 dw_FreeArray(SPxi);
196 dw_FreeArray(Pxi1);
197 dw_FreeArray(Pxi);
198
199 return F;
200 }
201
202 /*
203
204 */
205 TVector* dw_state_space_mean_unconditional_forecast(TVector *F, int h, int t0, TStateModel *model)
206 {
207 T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta);
208 PRECISION ***y=(PRECISION***)dw_CreateMultidimensionalArrayList_scalar(3,h,statespace->ny,1);
209 int i, j;
210 for (i=h-1; i >= 0; i--)
211 for (j=statespace->ny-1; j >= 0; j--)
212 { dw_FreeArray(y[i][j]); y[i][j]=(PRECISION*)NULL; }
213 F=dw_state_space_mean_conditional_forecast(F,y,h,t0,model);
214 dw_FreeArray(y);
215 return F;
216 }
217
218 /*
219 Assumes
220 f_out : valid FILE pointer
221 percentiles : vector of numbers between 0 and 1 inclusive
222 draws : number of draws of shocks and regimes to make for each posterior draw
223 posterior_draws : each row contains log posterior, log likelihood, and
224 posterior draw. If null, current parameters are used.
225 thin : thinning factor
226 T : last observation to treat as data. Usually equals model->nobs.
227 h : non-negative integer
228 model : point to valid TStateModel/T_MSStateSpace structure
229 nobs : the number of actual observations to print before the forecast
230
231 Results:
232 Computes and prints to the file f_out the requested percentiles for forecasts
233 of the observables.
234
235 Returns:
236 One upon success and zero otherwise.
237
238 */
239 int dw_state_space_forecast_percentile(FILE *f_out, TVector percentiles, int draws, TMatrix posterior_draws,
240 int thin, int T, int h, TStateModel *model, int nobs)
241 {
242 T_MSStateSpace *statespace;
243 int rtrn=0, ntheta, nq, *S, i, j, k, m, n,t;
244 TVector x, init_prob, prob, *shocks, z_initial;
245 TMatrix forecast, z_scale;
246 TMatrixHistogram *histogram;
247
248 // quick check of passed parameters
249 if (!f_out || !percentiles || (draws <= 0) || (thin <= 0) || (T < 0) || (h < 0) || !model) return 0;
250
251 statespace=(T_MSStateSpace*)(model->theta);
252 ntheta=NumberFreeParametersTheta(model);
253 nq=NumberFreeParametersQ(model);
254
255 if (T > model->nobs) return 0;
256
257 // allocate memory
258 S=(int*)dw_malloc(h*sizeof(int));
259 forecast=CreateMatrix(h,statespace->ny);
260 histogram=CreateMatrixHistogram(h,statespace->ny,100,HISTOGRAM_VARIABLE);
261 z_initial=CreateVector(statespace->nz);
262 shocks=dw_CreateArray_vector(h);
263 for (i=h-1; i >= 0; i--) shocks[i]=CreateVector(statespace->nepsilon);
264 init_prob=CreateVector(model->sv->nbasestates);
265 prob=CreateVector(model->sv->nbasestates);
266 z_scale=CreateMatrix(statespace->nz,statespace->nz);
267
268 if (posterior_draws)
269 {
270 x=CreateVector(ColM(posterior_draws));
271 i=RowM(posterior_draws)-1;
272 }
273 else
274 {
275 x=(TVector)NULL;
276 i=0;
277 }
278 for (n=i-1000*thin; i >= 0; i-=thin)
279 {
280 // Get free parameters and push them into model
281 if (posterior_draws)
282 {
283 RowVector(x,posterior_draws,i);
284 if (ntheta > 0) ConvertFreeParametersToTheta(model,pElementV(x)+2);
285 if (nq > 0) ConvertFreeParametersToQ(model,pElementV(x)+2+ntheta);
286
287 if (i <= n)
288 {
289 printf("%d/%d posterior draws processed\n",(RowM(posterior_draws)-i)/thin,RowM(posterior_draws)/thin);
290 n-=1000*thin;
291 }
292 }
293
294 // Ensure that the filter has been run through time T
295 if ((statespace->t0 < T) && !Filter(T,model))
296 goto ERROR_EXIT;
297
298 // Get transition matrix -- this must be modified if transition matrix is time varying
299 if (((T < model->sv->t0) || (model->sv->t1 < T)) && !sv_ComputeTransitionMatrix(T,model->sv,model))
300 goto ERROR_EXIT;
301
302 // Get filtered probability at time T
303 for (j=model->sv->nbasestates-1; j >= 0; j--)
304 ElementV(init_prob,j)=ProbabilityBaseStateConditionalCurrent(j,T,model);
305
306 for (k=draws; k > 0; k--)
307 {
308 // Draw time T regime
309 m=DrawDiscrete(init_prob);
310
311 // Draw initial z
312 dw_NormalVector(z_initial); //InitializeVector(z_initial,0.0);
313 MatrixSquareRoot(z_scale,statespace->Ezz[T][m]);
314 ProductMV(z_initial,z_scale,z_initial);
315 AddVV(z_initial,z_initial,statespace->Ez[T][m]);
316
317 // Draw regimes from time T+1 through T+h inclusive
318 for (j=0; j < h; j++)
319 {
320 ColumnVector(prob,model->sv->baseQ,m);
321 m=DrawDiscrete(prob);
322 S[j]=model->sv->lag_index[m][0];
323 }
324
325 // Draw shocks
326 for (j=h-1; j >= 0; j--) dw_NormalVector(shocks[j]); // InitializeVector(shocks[i],0.0);
327
328 // Compute forecast
329 if (!dw_state_space_forecast(forecast,h,z_initial,shocks,S,model))
330 goto ERROR_EXIT;
331
332 // Accumulate impulse response
333 AddMatrixObservation(forecast,histogram);
334 }
335 }
336
337 for (i=0; i < DimV(percentiles); i++)
338 {
339 MatrixPercentile(forecast,ElementV(percentiles,i),histogram);
340 for (t=statespace->nobs-nobs+1; t <= statespace->nobs; t++)
341 {
342 fprintf(f_out,"%d ",t);
343 dw_PrintVector(f_out,statespace->y[t],"%le ");
344 }
345 for (j=0; j < h; j++)
346 {
347 fprintf(f_out,"%d ",t++);
348 for (k=0; k < ColM(forecast); k++) fprintf(f_out,"%le ",ElementM(forecast,j,k));
349 fprintf(f_out,"\n");
350 }
351 fprintf(f_out,"\n");
352 }
353
354 rtrn=1;
355
356 ERROR_EXIT:
357 FreeMatrixHistogram(histogram);
358 FreeMatrix(forecast);
359 dw_free(S);
360 FreeVector(prob);
361 FreeVector(init_prob);
362 FreeMatrix(z_scale);
363 dw_FreeArray(shocks);
364 FreeVector(x);
365
366 return rtrn;
367 }
368
369 /*
370 Assumes
371 f_out : valid FILE pointer
372 percentiles : vector of numbers between 0 and 1 inclusive
373 draws : number of draws of shocks and regimes to make for each posterior draw
374 posterior_draws : each row contains log posterior, log likelihood, and
375 posterior draw. If null, current parameters are used.
376 thin : thinning factor
377 s : base state
378 T : last observation to treat as data. Usually equals model->nobs.
379 h : non-negative integer
380 model : point to valid TStateModel/T_MSStateSpace structure
381 nobs : the number of actual observations to print before the forecast
382
383 Results:
384 Computes and prints to the file f_out the requested percentiles for forecasts
385 of the observables.
386
387 Returns:
388 One upon success and zero otherwise.
389
390 Notes:
391 The regime at time T is drawn from the filtered probabilities at time t, and
392 is set to s there after.
393
394 */
395 int dw_state_space_forecast_percentile_regime(FILE *f_out, TVector percentiles, int draws, TMatrix posterior_draws,
396 int thin, int s, int T, int h, TStateModel *model, int nobs)
397 {
398 T_MSStateSpace *statespace;
399 int rtrn=0, ntheta, nq, *S, i, j, k, m, n,t;
400 TVector x, init_prob, prob, *shocks, z_initial;
401 TMatrix forecast, z_scale;
402 TMatrixHistogram *histogram;
403
404 // quick check of passed parameters
405 if (!f_out || !percentiles || (draws <= 0) || (thin <= 0) || (T < 0) || (h < 0) || !model) return 0;
406
407 statespace=(T_MSStateSpace*)(model->theta);
408 ntheta=NumberFreeParametersTheta(model);
409 nq=NumberFreeParametersQ(model);
410
411 if (T > model->nobs) return 0;
412
413 // allocate memory
414 S=(int*)dw_malloc(h*sizeof(int));
415 for (i=0; i < h; i++) S[i]=s;
416 forecast=CreateMatrix(h,statespace->ny);
417 histogram=CreateMatrixHistogram(h,statespace->ny,100,HISTOGRAM_VARIABLE);
418 z_initial=CreateVector(statespace->nz);
419 shocks=dw_CreateArray_vector(h);
420 for (i=h-1; i >= 0; i--) shocks[i]=CreateVector(statespace->nepsilon);
421 init_prob=CreateVector(model->sv->nbasestates);
422 prob=CreateVector(model->sv->nbasestates);
423 z_scale=CreateMatrix(statespace->nz,statespace->nz);
424
425 if (posterior_draws)
426 {
427 x=CreateVector(ColM(posterior_draws));
428 i=RowM(posterior_draws)-1;
429 }
430 else
431 {
432 x=(TVector)NULL;
433 i=0;
434 }
435 for (n=i-1000*thin; i >= 0; i-=thin)
436 {
437 // Get free parameters and push them into model
438 if (posterior_draws)
439 {
440 RowVector(x,posterior_draws,i);
441 if (ntheta > 0) ConvertFreeParametersToTheta(model,pElementV(x)+2);
442 if (nq > 0) ConvertFreeParametersToQ(model,pElementV(x)+2+ntheta);
443
444 if (i <= n)
445 {
446 printf("%d/%d posterior draws processed\n",(RowM(posterior_draws)-i)/thin,RowM(posterior_draws)/thin);
447 n-=1000*thin;
448 }
449 }
450
451 // Ensure that the filter has been run through time T
452 if ((statespace->t0 < T) && !Filter(T,model))
453 goto ERROR_EXIT;
454
455 // Get filtered probability at time T
456 for (j=model->sv->nbasestates-1; j >= 0; j--)
457 ElementV(init_prob,j)=ProbabilityBaseStateConditionalCurrent(j,T,model);
458
459 for (k=draws; k > 0; k--)
460 {
461 // Draw time T regime
462 m=DrawDiscrete(init_prob);
463
464 // Draw initial z
465 dw_NormalVector(z_initial); //InitializeVector(z_initial,0.0);
466 MatrixSquareRoot(z_scale,statespace->Ezz[T][m]);
467 ProductMV(z_initial,z_scale,z_initial);
468 AddVV(z_initial,z_initial,statespace->Ez[T][m]);
469
470 // Draw shocks
471 for (j=h-1; j >= 0; j--) dw_NormalVector(shocks[j]); // InitializeVector(shocks[j],0.0);
472
473 // Compute forecast
474 if (!dw_state_space_forecast(forecast,h,z_initial,shocks,S,model))
475 goto ERROR_EXIT;
476
477 // Accumulate impulse response
478 AddMatrixObservation(forecast,histogram);
479 }
480 }
481
482 for (i=0; i < DimV(percentiles); i++)
483 {
484 MatrixPercentile(forecast,ElementV(percentiles,i),histogram);
485 for (t=statespace->nobs-nobs+1; t <= statespace->nobs; t++)
486 {
487 fprintf(f_out,"%d ",t);
488 dw_PrintVector(f_out,statespace->y[t],"%le ");
489 }
490 for (j=0; j < h; j++)
491 {
492 fprintf(f_out,"%d ",t++);
493 for (k=0; k < ColM(forecast); k++) fprintf(f_out,"%le ",ElementM(forecast,j,k));
494 fprintf(f_out,"\n");
495 }
496 fprintf(f_out,"\n");
497 }
498
499 rtrn=1;
500
501 ERROR_EXIT:
502 FreeMatrixHistogram(histogram);
503 FreeMatrix(forecast);
504 dw_free(S);
505 FreeVector(prob);
506 FreeVector(init_prob);
507 FreeMatrix(z_scale);
508 dw_FreeArray(shocks);
509 FreeVector(x);
510
511 return rtrn;
512 }
513
514 // #define SHOCKS
515 //#define DATA
516 #define DATA_SHOCKS
517 TMatrix Forecast_ConditionalShocks_Special(int horizon, TStateModel *model)
518 {
519
520 #ifdef SHOCKS
521 int forecast_periods=horizon;
522
523 T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta);
524 TMatrix forecast;
525 TVector *e=dw_CreateArray_vector(forecast_periods), z_initial;
526 int *S, t;
527
528 /************ Forecast Shocks *************/
529 if (statespace->nepsilon > 0)
530 {
531 for (t=forecast_periods-1; t >= 0; t--)
532 InitializeVector(e[t]=CreateVector(statespace->nepsilon),0.0);
533 }
534
535 /* shock 0 - Patience *
536 ElementV(e[0],0)=-.80;
537 ElementV(e[1],0)=-.80;
538 ElementV(e[2],0)=-.80;
539 ElementV(e[3],0)=-.80;
540 /**/
541
542 /* Shock 1 - neutral technolgy *
543 ElementV(e[0],1)=-.80;
544 ElementV(e[1],1)=-.80;
545 ElementV(e[2],1)=-.80;
546 ElementV(e[3],1)=-.80;
547 /**/
548
549 /* Shock 5 - housing *
550 ElementV(e[0],5)=-0.49;
551 ElementV(e[1],5)=-0.50;
552 ElementV(e[2],5)=-0.50;
553 ElementV(e[3],5)=-0.50;
554 /**/
555 #define shock_value 0.0;
556 #define shock_increment -0.015
557 ElementV(e[0],5)=0.00;
558 ElementV(e[1],5)=-0.05;
559 ElementV(e[2],5)=ElementV(e[1],5)+shock_increment;
560 ElementV(e[3],5)=ElementV(e[2],5)+shock_increment;
561 ElementV(e[4],5)=ElementV(e[3],5)+shock_increment;
562 ElementV(e[5],5)=ElementV(e[4],5)+shock_increment;
563 ElementV(e[6],5)=ElementV(e[5],5)+shock_increment;
564 ElementV(e[7],5)=ElementV(e[6],5)+shock_increment + 0.02;
565 ElementV(e[8],5)=ElementV(e[7],5)+shock_increment;
566 ElementV(e[9],5)=ElementV(e[8],5)+shock_increment;
567 ElementV(e[10],5)=ElementV(e[9],5)+shock_increment;
568 ElementV(e[11],5)=ElementV(e[10],5)+shock_increment;
569 int i;
570 for (i=0; i < 12; i++) printf("ElementV(e[%d],5) = %lf\n",i,ElementV(e[i],5));
571 /**/
572
573 /* Shock 6 - labor *
574 ElementV(e[0],6)=.80;
575 ElementV(e[1],6)=.80;
576 ElementV(e[2],6)=.80;
577 ElementV(e[3],6)=.80;
578 /**/
579 /*****************************************/
580
581 printf("Computing special forecasts\n");
582 dw_InitializeArray_int(S=dw_CreateArray_int(forecast_periods),0);
583 z_initial=EquateVector((TVector)NULL,statespace->Ez[statespace->nobs][0]);
584 forecast=dw_state_space_forecast((TMatrix)NULL,forecast_periods,z_initial,e,S,model);
585
586 dw_FreeArray(e);
587 FreeVector(z_initial);
588 dw_FreeArray(S);
589
590 return forecast;
591 #endif
592
593 #ifdef DATA
594 int i, j;
595 PRECISION ***y;
596 TVector *F;
597 TMatrix forecast;
598 T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta);
599
600 y=(PRECISION***)dw_CreateMultidimensionalArrayList_scalar(3,horizon,statespace->ny,1);
601
602 //=============================================================================
603 // credit frictions model - constrain land price growth
604 for (i=horizon-1; i >= 0; i--)
605 for (j=statespace->ny-1; j >= 0; j--)
606 if ((j == 0) && (i < 12))
607 y[i][j][0] = -0.03/4.0;
608 else
609 {
610 dw_FreeArray(y[i][j]);
611 y[i][j]=(PRECISION*)NULL;
612 }
613 //=============================================================================
614
615 printf("Constructing constrained forecast\n");
616 if (F=dw_state_space_mean_conditional_forecast((TVector*)NULL,y,horizon,statespace->nobs,model))
617 {
618 forecast=CreateMatrix(horizon,statespace->ny);
619 for (i=horizon-1; i >= 0; i--)
620 for (j=statespace->ny-1; j >= 0; j--)
621 ElementM(forecast,i,j)=ElementV(F[i],j);
622 dw_FreeArray(F);
623 }
624
625 dw_FreeArray(y);
626
627 return forecast;
628 #endif
629
630 #ifdef DATA_SHOCKS
631 T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta);
632 TVector z1, z2, y, e;
633 TMatrix forecast, H_Phi;
634 int *S, i, t;
635
636
637 // create forecast
638 if (!(forecast=CreateMatrix(horizon,statespace->ny))) return (TMatrix)NULL;
639
640 // allocate and initialize memory
641 z1=CreateVector(statespace->nz);
642 z2=CreateVector(statespace->nz);
643 y=CreateVector(statespace->ny);
644 InitializeVector(e=CreateVector(statespace->nepsilon),0.0);
645 EquateVector(z1,statespace->Ez[statespace->nobs][0]);
646 dw_InitializeArray_int(S=dw_CreateArray_int(horizon),0);
647 H_Phi=ProductMM((TMatrix)NULL,statespace->H[0],statespace->Phiz[0]);
648
649 // forecast
650 for (t=0; t < horizon; t++)
651 {
652 ProductMV(z2,statespace->F[S[t]],z1);
653 AddVV(z1,statespace->b[S[t]],z2);
654
655 ProductMV(y,statespace->H[S[t]],z1);
656 AddVV(y,statespace->a[S[t]],y);
657
658 if (t < 12)
659 {
660 if (t < 4)
661 ElementV(e,5)=(-5.0/400.0 - ElementV(y,0))/ElementM(H_Phi,0,5); // land prices series in position 0, land price shock in position 5
662 else if (t < 8)
663 ElementV(e,5)=(-5.0/400.0 - ElementV(y,0))/ElementM(H_Phi,0,5);
664 else
665 ElementV(e,5)=(-2.5/400.0 - ElementV(y,0))/ElementM(H_Phi,0,5);
666
667 ProductMV(z2,statespace->Phiz[S[t]],e);
668 AddVV(z1,z1,z2);
669 ProductMV(y,statespace->H[S[t]],z1);
670 AddVV(y,statespace->a[S[t]],y);
671 }
672
673 for (i=statespace->ny-1; i >= 0; i--)
674 ElementM(forecast,t,i)=ElementV(y,i);
675 }
676
677 FreeMatrix(H_Phi);
678 dw_FreeArray(S);
679 FreeVector(e);
680 FreeVector(y);
681 FreeVector(z2);
682 FreeVector(z1);
683
684 return forecast;
685
686 #endif
687
688 }
689 #undef SHOCKS
690 #undef DATA
691 #undef DATA_SHOCKS
692
693