


Computes the sample moments of a VAR model.
The VAR(p) model is defined by:
y_t = \sum_{k=1}^p y_{t-k} A_k + z_t C + e_t for t = 1,...,T
where y_t is a 1*m vector of observed endogenous variables, p is the
number of lags, A_k is an m*m real matrix, z_t is a 1*q vector of
exogenous (deterministic) variables, C is a q*m real matrix and
e_t is a vector of exogenous stochastic shocks. T is the number
of observations. The deterministic exogenous variables are assumed to
be a polynomial trend of order q = "var_trend_order".
We define:
<> Y = (y_1',y_2',...,y_T')' a T*m matrix,
<> x_t = (y_{t-1},y_{t-2},...,y_{t-p},z_t) a 1*(mp+q) row vector,
<> X = (x_1',x_2',...,x_T')' a T*(mp+q) matrix,
<> E = (e_1',e_2',...,e_T')' a T*m matrix and
<> A = (A_1',A_2',...,A_p',C')' an (mp+q)*m matrix of coefficients.
So that we can equivalently write the VAR(p) model using the following
matrix representation:
Y = X * A +E
INPUTS
o FirstObservation [integer] First observation.
o LastObservation [integer] Last observation.
o qlag [integer] Number of lags in the VAR model.
o var_trend_order [integer] Order of the polynomial exogenous trend:
= -1 no constant and no linear trend,
= 0 constant and no linear trend,
= 1 constant and linear trend.
OUTPUTS
o YtY [double] Y'*Y an m*m matrix.
o XtY [double] X'*Y an (mp+q)*m matrix.
o YtX [double] Y'*X an m*(mp+q) matrix.
o XtX [double] X'*X an (mp+q)*(mp+q) matrix.
o Y [double] Y a T*m matrix.
o X [double] X a T*(mp+q) matrix.
SPECIAL REQUIREMENTS
None.

0001 function [YtY,XtY,YtX,XtX,Y,X] = ... 0002 var_sample_moments(FirstObservation,LastObservation,qlag,var_trend_order,datafile,varobs,xls_sheet,xls_range) 0003 % Computes the sample moments of a VAR model. 0004 % 0005 % The VAR(p) model is defined by: 0006 % 0007 % y_t = \sum_{k=1}^p y_{t-k} A_k + z_t C + e_t for t = 1,...,T 0008 % 0009 % where y_t is a 1*m vector of observed endogenous variables, p is the 0010 % number of lags, A_k is an m*m real matrix, z_t is a 1*q vector of 0011 % exogenous (deterministic) variables, C is a q*m real matrix and 0012 % e_t is a vector of exogenous stochastic shocks. T is the number 0013 % of observations. The deterministic exogenous variables are assumed to 0014 % be a polynomial trend of order q = "var_trend_order". 0015 % 0016 % We define: 0017 % 0018 % <> Y = (y_1',y_2',...,y_T')' a T*m matrix, 0019 % 0020 % <> x_t = (y_{t-1},y_{t-2},...,y_{t-p},z_t) a 1*(mp+q) row vector, 0021 % 0022 % <> X = (x_1',x_2',...,x_T')' a T*(mp+q) matrix, 0023 % 0024 % <> E = (e_1',e_2',...,e_T')' a T*m matrix and 0025 % 0026 % <> A = (A_1',A_2',...,A_p',C')' an (mp+q)*m matrix of coefficients. 0027 % 0028 % So that we can equivalently write the VAR(p) model using the following 0029 % matrix representation: 0030 % 0031 % Y = X * A +E 0032 % 0033 % 0034 % INPUTS 0035 % o FirstObservation [integer] First observation. 0036 % o LastObservation [integer] Last observation. 0037 % o qlag [integer] Number of lags in the VAR model. 0038 % o var_trend_order [integer] Order of the polynomial exogenous trend: 0039 % = -1 no constant and no linear trend, 0040 % = 0 constant and no linear trend, 0041 % = 1 constant and linear trend. 0042 % 0043 % OUTPUTS 0044 % o YtY [double] Y'*Y an m*m matrix. 0045 % o XtY [double] X'*Y an (mp+q)*m matrix. 0046 % o YtX [double] Y'*X an m*(mp+q) matrix. 0047 % o XtX [double] X'*X an (mp+q)*(mp+q) matrix. 0048 % o Y [double] Y a T*m matrix. 0049 % o X [double] X a T*(mp+q) matrix. 0050 % 0051 % SPECIAL REQUIREMENTS 0052 % None. 0053 0054 % Copyright (C) 2007-2009 Dynare Team 0055 % 0056 % This file is part of Dynare. 0057 % 0058 % Dynare is free software: you can redistribute it and/or modify 0059 % it under the terms of the GNU General Public License as published by 0060 % the Free Software Foundation, either version 3 of the License, or 0061 % (at your option) any later version. 0062 % 0063 % Dynare is distributed in the hope that it will be useful, 0064 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0065 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0066 % GNU General Public License for more details. 0067 % 0068 % You should have received a copy of the GNU General Public License 0069 % along with Dynare. If not, see <http://www.gnu.org/licenses/>. 0070 0071 X = []; 0072 Y = []; 0073 YtY = []; 0074 YtX = []; 0075 XtY = []; 0076 XtX = []; 0077 0078 data = read_variables(datafile,varobs,[],xls_sheet,xls_range); 0079 0080 if qlag > FirstObservation 0081 disp('VarSampleMoments :: not enough data to initialize! Try to increase FirstObservation.') 0082 return 0083 end 0084 0085 NumberOfObservations = LastObservation-FirstObservation+1;% This is T. 0086 NumberOfVariables = size(varobs,1);% This is m. 0087 if var_trend_order == -1% No constant no linear trend case. 0088 X = zeros(NumberOfObservations,NumberOfVariables*qlag); 0089 elseif var_trend_order == 0% Constant and no linear trend case. 0090 X = ones(NumberOfObservations,NumberOfVariables*qlag+1); 0091 indx = NumberOfVariables*qlag+1; 0092 elseif var_trend_order == 1;% Constant and linear trend case. 0093 X = ones(NumberOfObservations,NumberOfVariables*qlag+2); 0094 indx = NumberOfVariables*qlag+1:NumberOfVariables*qlag+2; 0095 else 0096 disp('var_sample_moments :: trend must be equal to -1,0 or 1!') 0097 return 0098 end 0099 0100 % I build matrices Y and X 0101 Y = data(FirstObservation:LastObservation,:); 0102 0103 for t=1:NumberOfObservations 0104 line = t + FirstObservation-1; 0105 for lag = 1:qlag 0106 X(t,(lag-1)*NumberOfVariables+1:lag*NumberOfVariables) = data(line-lag,:); 0107 end 0108 end 0109 0110 if (var_trend_order == 1) 0111 X(:,end) = transpose(1:NumberOfObservations) 0112 end 0113 0114 YtY = Y'*Y; 0115 YtX = Y'*X; 0116 XtY = X'*Y; 0117 XtX = X'*X;