| 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_counterfactual.h" |
| 19 |
#include "dw_MSStateSpace.h" |
| 20 |
|
| 21 |
/* |
| 22 |
Assumes: |
| 23 |
data : (nobs+1-tau) x (ny+1) matrix or null pointer |
| 24 |
tau : initial time. |
| 25 |
z : ? x (nz+1) matrix state variables z[t]. |
| 26 |
shocks : ? x (nepsilon+1) matrix of fundamental shocks (epsilon[t]). |
| 27 |
prob : ? x (nbasestates+1) matrix of marginal regime probabilities. |
| 28 |
model : pointer to valid TStateModel structure. |
| 29 |
|
| 30 |
Results: |
| 31 |
Computes counterfactual data |
| 32 |
|
| 33 |
Returns: |
| 34 |
The matrix data upon success and null upon failure. If data is null, then |
| 35 |
it is created. |
| 36 |
|
| 37 |
Notes: |
| 38 |
The first column of z, shocks, and prob contain the time and must be |
| 39 |
consecutive integers. The matrix z must contain time tau, and shocks and |
| 40 |
prob must contain times tau+1 through nobs. |
| 41 |
|
| 42 |
ASSUMES THAT ONLY CONSTANT TERMS OR VARIANCES ARE TIME VARYING. |
| 43 |
*/ |
| 44 |
TMatrix dw_state_space_counterfactual(TMatrix data, int tau, TMatrix states, TMatrix shocks, TMatrix prob, TStateModel *model) |
| 45 |
{ |
| 46 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 47 |
TVector z1, z2, y, e=(TVector)NULL, tmp; |
| 48 |
int i, s, t, tt, iz=ElementM(states,0,0), is=ElementM(shocks,0,0), ip=ElementM(prob,0,0); |
| 49 |
|
| 50 |
// check inputs |
| 51 |
if ((iz > tau) || (ElementM(states,RowM(states)-1,0) < tau) || (ColM(states) != statespace->nz+1) |
| 52 |
|| (is > tau+1) || (ElementM(shocks,RowM(shocks)-1,0) < model->nobs) || (ColM(shocks) != statespace->nepsilon+1) |
| 53 |
|| (ip > tau+1) || (ElementM(prob,RowM(prob)-1,0) < model->nobs)|| (ColM(prob) != statespace->nbasestates+1)) |
| 54 |
return (TMatrix)NULL; |
| 55 |
|
| 56 |
// create forecast if necessary |
| 57 |
if (!data && !(data=CreateMatrix(statespace->nobs+1-tau,statespace->ny+1))) return (TMatrix)NULL; |
| 58 |
|
| 59 |
// debug |
| 60 |
if (!Smooth_MSStateSpace(model) || !ComputeSmoothedProbabilities(model)) |
| 61 |
{ |
| 62 |
printf("Unable to smooth\n"); |
| 63 |
return (TMatrix)NULL; |
| 64 |
} |
| 65 |
|
| 66 |
// forecast |
| 67 |
z1=CreateVector(statespace->nz); |
| 68 |
z2=CreateVector(statespace->nz); |
| 69 |
y=CreateVector(statespace->ny); |
| 70 |
e=CreateVector(statespace->nepsilon); |
| 71 |
for (i=DimV(z1)-1; i >= 0; i--) |
| 72 |
ElementV(z1,i)=ElementM(states,tau-iz,i+1); |
| 73 |
ProductMV(y,statespace->H[0],z1); |
| 74 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 75 |
LinearCombinationV(y,1.0,y,ElementM(prob,tau-ip,s+1),statespace->a[s]); |
| 76 |
ElementM(data,0,0)=tau; |
| 77 |
for (i=statespace->ny-1; i >= 0; i--) |
| 78 |
ElementM(data,0,i+1)=ElementV(y,i); |
| 79 |
for (tt=1, t=tau+1; t <= statespace->nobs; tt++, t++) |
| 80 |
{ |
| 81 |
ProductMV(z2,statespace->F[0],z1); |
| 82 |
for (i=DimV(e)-1; i >= 0; i--) |
| 83 |
ElementV(e,i)=ElementM(shocks,t-is,i+1); |
| 84 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 85 |
{ |
| 86 |
LinearCombinationV(z2,1.0,z2,ElementM(prob,t-ip,s+1),statespace->b[s]); |
| 87 |
UpdateProductMV(1.0,z2,ElementM(prob,t-ip,s+1),statespace->Phiz[s],e); |
| 88 |
} |
| 89 |
ProductMV(y,statespace->H[0],z2); |
| 90 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 91 |
LinearCombinationV(y,1.0,y,ElementM(prob,t-ip,s+1),statespace->a[s]); |
| 92 |
ElementM(data,tt,0)=t; |
| 93 |
for (i=statespace->ny-1; i >= 0; i--) |
| 94 |
ElementM(data,tt,i+1)=ElementV(y,i); |
| 95 |
tmp=z1; |
| 96 |
z1=z2; |
| 97 |
z2=tmp; |
| 98 |
} |
| 99 |
FreeVector(y); |
| 100 |
FreeVector(z2); |
| 101 |
FreeVector(z1); |
| 102 |
FreeVector(e); |
| 103 |
|
| 104 |
return data; |
| 105 |
} |