| 1 |
function [phi,YtY] = mformd(z,nn) |
| 2 |
% mformd: arrange matrix form data: [Y,YtY] = mformd(z,nn) as structural form |
| 3 |
% YA = E, Y: T*(nvar*lags+nvar+1) |
| 4 |
% |
| 5 |
% where z is the (T+lags)-by-(nvar+1) raw data matrix (nvar of variables + constant); |
| 6 |
% nn is the numbers of inputs [nvar,lags,sample period (total)]; |
| 7 |
% phi: Y as in the structural form YA = E, Y: T*(nvar*lags+nvar+1) |
| 8 |
% YtY: Y'Y: (nvar*lags+nvar+1)*(nvar*lags+nvar+1) |
| 9 |
% |
| 10 |
% Copyright (C) 1997-2012 Tao Zha |
| 11 |
% |
| 12 |
% This free software: you can redistribute it and/or modify |
| 13 |
% it under the terms of the GNU General Public License as published by |
| 14 |
% the Free Software Foundation, either version 3 of the License, or |
| 15 |
% (at your option) any later version. |
| 16 |
% |
| 17 |
% It is distributed in the hope that it will be useful, |
| 18 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
% GNU General Public License for more details. |
| 21 |
% |
| 22 |
% If you did not received a copy of the GNU General Public License |
| 23 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 24 |
% |
| 25 |
|
| 26 |
% ** setup of orders and lengths ** |
| 27 |
nvar = nn(1); |
| 28 |
lags = nn(2); |
| 29 |
sp = nn(3); % sample period |
| 30 |
|
| 31 |
ess = sp-lags; % effective sample size |
| 32 |
sb = lags+1; % sample beginning |
| 33 |
sl = sp; % sample last period |
| 34 |
ncoe = nvar*lags + nvar + 1; % with constant and contemporaneous data |
| 35 |
|
| 36 |
% ** construct Y as in YA = E where phi = Y ** |
| 37 |
x = z(:,1:nvar); |
| 38 |
C = z(:,nvar+1); |
| 39 |
phi = zeros(ess,ncoe); |
| 40 |
phi(:,1:nvar) = x(sb:sl,:); |
| 41 |
phi(:,ncoe) = C(1:ess); |
| 42 |
for k=1:lags, phi(:,nvar*k+1:nvar*(k+1)) = x(sb-k:sl-k,:); end |
| 43 |
% column: T; row: [nvar for 0th lag, nvar for 1st lag, |
| 44 |
% ..., nvar for last lag, const] |
| 45 |
% Thus, # of columns is nvar*lags+nvar+1 = ncoef. |
| 46 |
% ** YtY, residuals ** |
| 47 |
[u d v]=svd(phi,0); %trial |
| 48 |
vd=v.*(ones(size(v,2),1)*diag(d)'); %trial |
| 49 |
YtY=vd*vd'; % YtY = phi'*phi; % X'X, k*k (ncoe*ncoe) |