| 1 |
// --+ options: stochastic,json=compute +-- |
| 2 |
|
| 3 |
var foo z x y; |
| 4 |
varexo e_x e_y e_z; |
| 5 |
parameters a b c d e f beta ; |
| 6 |
|
| 7 |
a = .9; |
| 8 |
b = -.2; |
| 9 |
c = .3; |
| 10 |
f = .8; |
| 11 |
d = .5; |
| 12 |
e = .4; |
| 13 |
|
| 14 |
beta = 1/(1+.02); |
| 15 |
|
| 16 |
// Define a VAR model from a subset of equations in the model block. |
| 17 |
var_model(structural, model_name = toto, eqtags = [ 'X' 'Y' 'Z' ]); |
| 18 |
|
| 19 |
// Define a VAR_EXPECTATION_MODEL |
| 20 |
var_expectation_model(model_name = varexp, expression = diff(log(x)), auxiliary_model_name = toto, horizon = 1, discount = beta) ; |
| 21 |
|
| 22 |
model; |
| 23 |
[ name = 'X' ] |
| 24 |
diff(log(x)) = b*diff(z) + a*diff(log(x(-1))) + b*(1-a)*diff(log(x(-2))) + c*diff(z(-2)) + e_x; |
| 25 |
[ name = 'Z' ] |
| 26 |
diff(z) = .1 + f*(diff(z(-1))-diff(log(x)))+c*diff(z(-2)) + e_z; |
| 27 |
[ name = 'Y' ] |
| 28 |
log(y) = diff(log(x)) + d*log(y(-2)) + e*diff(z(-1)) + e_y; |
| 29 |
|
| 30 |
foo = var_expectation(varexp); |
| 31 |
end; |
| 32 |
|
| 33 |
% Evaluate strutural VAR matrices |
| 34 |
[ar, a0, const] = example1.varmatrices('toto', M_.params); |
| 35 |
|
| 36 |
assert(isequal(diag(a0), ones(3,1)), 'Diagonal of a0 is wrong.') |
| 37 |
|
| 38 |
assert(a0(1,3)==-b, 'Element (1,3) in A0 is wrong.') |
| 39 |
assert(a0(1,2)==0, 'Element (1,2) in A0 is wrong.') |
| 40 |
assert(a0(2,1)==-1, 'Element (2,1) in A0 is wrong.') |
| 41 |
assert(a0(2,3)==0, 'Element (2,3) in A0 is wrong.') |
| 42 |
assert(a0(3,1)==f, 'Element (3,1) in A0 is wrong.') |
| 43 |
assert(a0(3,2)==0, 'Element (3,1) in A0 is wrong.') |
| 44 |
|
| 45 |
assert(isequal(ar(:,:,1), [a 0 0; 0 0 e; 0 0 f]), 'First autoregressive matrix is wrong') |
| 46 |
assert(isequal(ar(:,:,2), [(1-a)*b 0 c; 0 d 0; 0 0 c]), 'Second autoregressive matrix is wrong') |
| 47 |
|
| 48 |
assert(isequal(const, [.0;.0;.1]), 'Constant vector is wrong.') |
| 49 |
|
| 50 |
% Evaluate reduced form VAR matrices |
| 51 |
[AR, ~, CONST] = example1.varmatrices('toto', M_.params, true); |
| 52 |
|
| 53 |
assert(all(all(abs(AR(:,:,1)-a0\ar(:,:,1))<1e-9)), 'Reduced form is wrong (first lag)') |
| 54 |
assert(all(all(abs(AR(:,:,2)-a0\ar(:,:,2))<1e-9)), 'Reduced form is wrong (second lag)') |
| 55 |
assert(all(abs(CONST-a0\const)<1e-9), 'Reduced form is wrong (constant)') |
| 56 |
|
| 57 |
% Test get_companion_matrix when the VAR model has a constant. |
| 58 |
get_companion_matrix('toto', 'var'); |
| 59 |
|
| 60 |
assert(all(all(abs(oo_.var.toto.CompanionMatrix-[1, zeros(1, 6); CONST, AR(:,:,1), AR(:,:,2); zeros(3,1), eye(3), zeros(3)])<1e-9)), 'Companion matrix is wrong') |