1function var_sample_moments(nlag, var_trend_order, dataset_)%datafile,varobs,xls_sheet,xls_range) 2% Computes the sample moments of a VAR model. 3% 4% The VAR(p) model is defined by: 5% 6% y_t = \sum_{k=1}^p y_{t-k} A_k + z_t C + e_t for t = 1,...,T 7% 8% where y_t is a 1*m vector of observed endogenous variables, p is the 9% number of lags, A_k is an m*m real matrix, z_t is a 1*q vector of 10% exogenous (deterministic) variables, C is a q*m real matrix and 11% e_t is a vector of exogenous stochastic shocks. T is the number 12% of observations. The deterministic exogenous variables are assumed to 13% be a polynomial trend of order q = "var_trend_order". 14% 15% We define: 16% 17% <> Y = (y_1',y_2',...,y_T')' a T*m matrix, 18% 19% <> x_t = (y_{t-1},y_{t-2},...,y_{t-p},z_t) a 1*(mp+q) row vector, 20% 21% <> X = (x_1',x_2',...,x_T')' a T*(mp+q) matrix, 22% 23% <> E = (e_1',e_2',...,e_T')' a T*m matrix and 24% 25% <> A = (A_1',A_2',...,A_p',C')' an (mp+q)*m matrix of coefficients. 26% 27% So that we can equivalently write the VAR(p) model using the following 28% matrix representation: 29% 30% Y = X * A +E 31% 32% 33% INPUTS 34% o nlag [integer] Number of lags in the VAR model. 35% o var_trend_order [integer] Order of the polynomial exogenous trend: 36% = -1 no constant and no linear trend, 37% = 0 constant and no linear trend, 38% = 1 constant and linear trend. 39% o dataset_ [dseries] The sample. 40% 41% OUTPUTS 42% o YtY [double] Y'*Y an m*m matrix. 43% o XtY [double] X'*Y an (mp+q)*m matrix. 44% o YtX [double] Y'*X an m*(mp+q) matrix. 45% o XtX [double] X'*X an (mp+q)*(mp+q) matrix. 46% o Y [double] Y a T*m matrix. 47% o X [double] X a T*(mp+q) matrix. 48% 49% SPECIAL REQUIREMENTS 50% None. 51% 52% REMARKS 53% Outputs are saved in the base workspace (not returned by the function). 54 55% Copyright (C) 2007-2014 Dynare Team 56% 57% This file is part of Dynare. 58% 59% Dynare is free software: you can redistribute it and/or modify 60% it under the terms of the GNU General Public License as published by 61% the Free Software Foundation, either version 3 of the License, or 62% (at your option) any later version. 63% 64% Dynare is distributed in the hope that it will be useful, 65% but WITHOUT ANY WARRANTY; without even the implied warranty of 66% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 67% GNU General Public License for more details. 68% 69% You should have received a copy of the GNU General Public License 70% along with Dynare. If not, see <http://www.gnu.org/licenses/>. 71 72LastObservation = dataset_.dates(end); 73FirstObservation = dataset_.dates(1)+nlag; 74 75NumberOfObservations = LastObservation-FirstObservation+1; 76NumberOfVariables = dataset_.vobs; 77 78if isequal(var_trend_order,-1) 79 % No constant no linear trend case. 80 X = zeros(NumberOfObservations,NumberOfVariables*nlag); 81elseif isequal(var_trend_order,0) 82 % Constant and no linear trend case. 83 X = ones(NumberOfObservations,NumberOfVariables*nlag+1); 84 indx = NumberOfVariables*nlag+1; 85elseif isequal(var_trend_order,1) 86 % Constant and linear trend case. 87 X = ones(NumberOfObservations,NumberOfVariables*nlag+2); 88 indx = NumberOfVariables*nlag+1:NumberOfVariables*nlag+2; 89else 90 error('Estimation::var_sample_moments: trend must be equal to -1,0 or 1!') 91 return 92end 93 94% I build matrices Y and X 95Y = dataset_(FirstObservation:LastObservation).data; 96 97for t=1:NumberOfObservations 98 currentdate = FirstObservation+(t-1); 99 for lag = 1:nlag 100 X(t,(lag-1)*NumberOfVariables+1:lag*NumberOfVariables) = dataset_(currentdate-lag).data; 101 end 102end 103 104if (var_trend_order == 1) 105 X(:,end) = transpose(1:NumberOfObservations) 106end 107 108assignin('base', 'mYY', Y'*Y); 109assignin('base', 'mYX', Y'*X); 110assignin('base', 'mXY', X'*Y); 111assignin('base', 'mXX', X'*X); 112assignin('base', 'Ydata', Y); 113assignin('base', 'Xdata', X);