MATLAB Answers

Manually write code for a Vector Autoregression

20 views (last 30 days)
matlabuser
matlabuser on 19 Oct 2020
Commented: matlabuser on 11 Nov 2020 at 19:31
I am trying to manually write the code to get VAR(1) coefficients without using the built-in function from the Econometrics toolbox. There are three variables in the "beta" matrix, and I created the first lags of each of these three. Then, I applied OLS to each equation separately using a for loop, and stored the estimates in a matrix A.
I am also trying to compute the variance-covariance matrix of residuals. So, I thought of collecting the residuals first but when I do that inside the for loop, I received an error message: "unable to perform assignment because brace indexing is not supported of variables of this type"
In general, I am not sure if this is an efficient way to code VAR and get coefficient estimates and variance. So, if you have any suggestions on how to improve the code, I'd be very grateful for your help.
% create lagged values of beta
lag1_beta = lagmatrix(beta,1);
% apply OLS to each equation separately
for i = 1:size(beta,2) % counts the # of columns in the beta matrix
for j =['level', 'slope', 'curv']
VAR_{j} = fitlm(lag1_beta, beta(:, i), 'Intercept', false);
a{i} = VAR_{j}.Coefficients.Estimate';
resid{j} = VAR_{j}.Residuals.Raw';
end
end
% stack the coefficients of the individual OLS estimates
A =[a1; a2; a3]

  0 Comments

Sign in to comment.

Accepted Answer

Dana
Dana on 19 Oct 2020
First off, the interior (j) part of your loop doesn't make any sense, for several reasons:
(1) Everything inside that loop is based on the first line
VAR_{j} = fitlm(lag1_beta, beta(:, i), 'Intercept', false);
but there is no j on the right-hand side, so nothing would change as you interate through different values of j. I don't even have a guess as to what you want to change as j iterates.
(2) Note that ['level', 'slope', 'curv']='levelslopecurv'. Thus, when you write
for j =['level', 'slope', 'curv']
MATLAB interprets this as "cycle through the letters of 'levelslopecurv' one at a time, so that first j='l', then j='e', etc.". Hopefully you can see that this makes no sense.
(3) When j is a character (as (2) just established), and you use j as an index (e.g., as you did with VAR_{j}), MATLAB first converts the character to its ASCII code (e.g., 'l' becomes 108, since char(108)='l', etc.), and then uses that ASCII code as the index. There is no chance that's actually what you want here.
(4) The line
a{i} = VAR_{j}.Coefficients.Estimate';
overwrites a{i} (the i-th element of the cell array a) for each value of j. Again, this makes no sense.
Leaving aside the j business since I don't understand what you're trying to do with that, you don't need to do this for one equation at a time. If is your regression equation, with the identifying assumption , then substituing into the latter, you can solve for
.
Assuming you have T periods of observed data, letting be the matrix whose t-th row is , and the matrix whose t-th row is , and estimator of A is then
Thus, all you need to do in your case is
Y0 = beta(1:end-1,:);
Y1 = beta(2:end,:);
A = (Y1'*Y0)/(Y0'*Y0);

  5 Comments

Show 2 older comments
matlabuser
matlabuser on 10 Nov 2020 at 2:30
Suppose we would also like to estimate a vector of contants besides the parameters on the lags, how would the estimation process change?
Dana
Dana on 11 Nov 2020 at 16:18
To run the regression , where C is the vector of constants, you just need to add a vector of ones to the matrix of regressors Y0:
T = size(beta,1);
Y0 = [ones(T-1,1) , beta(1:end-1,:)];
Y1 = beta(2:end,:);
B = (Y1'*Y0)/(Y0'*Y0); % all regression coefficients
C = B(:,1); % vector of constant terms
A = B(:,2:end); % matrix of coefficients on lagged Y

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!