⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arf.m

📁 计量工具箱
💻 M
字号:
function ylevf = arf(y,nlag,nfor,begf,transf);% PURPOSE: estimates a autoregression of order nlag%          and produces f-step-ahead forecasts%-------------------------------------------------------------% USAGE: yfor = arf(y,nlag,nfor,begf,transf)% where:    y    = an (nobs * 1) vector in levels%           nlag = the lag length%           nfor = the forecast horizon%           begf = the beginning date of the forecast%                  (defaults to length(x) + 1)%         transf = 0, no data transformation%                = 1, 1st differences used to estimate the model%                = freq, seasonal differences used to estimate%                = cal-structure growth rates used to estimate%                  e.g., cal(1982,1,12) [see cal() function]              %-------------------------------------------------------------% NOTE: constant term included automatically%-------------------------------------------------------------% RETURNS: %      yfor = an nfor x 1 vector of level forecasts %-------------------------------------------------------------% SEE ALSO: ar, plt_ar, prt_ar%-------------------------------------------------------------% written by:% James P. LeSage, Dept of Economics% University of Toledo% 2801 W. Bancroft St,% Toledo, OH 43606% jpl@spatial-econometrics.comif nargin == 5 % user wants us to transform the data if isstruct(transf) % a growth rates transform   tform = 2;   freq = transf.freq;   elseif transf == 0  % no transform   tform = 0;   elseif transf == 1  % 1st difference transform   tform = 1;   elseif (transf == 1) | (transf == 4) | (transf == 12)   tform = 3;          % seasonal differences transform   freq = transf;   end;elseif nargin == 4tform = 0;elseerror('Wrong # of arguments to arf');end;[nobs neqs] = size(y);% adjust nobs to feed the lagsnmin = min(nobs,begf-1);% adjust nvar for constant termk = nlag+1;switch tformcase 1 % 1st differences transform% transform datady = y - mlag(y,1);% generate lagged rhs matrixxlag = mlag(dy,nlag);% constant termiota = ones(nobs,1);% truncate variables to feed lags and 1st diff and end at begf-1iota = trimr(iota,nlag+1,nobs-begf+1);dys =  trimr(dy,nlag+1,nobs-begf+1);xlag = trimr(xlag,nlag+1,nobs-begf+1);% add constant to x-matrixxmat = [xlag iota];% end of 1st difference transformation casecase 2 % growth rates transformation% transform datady = growthr(y,freq);% generate lagged rhs matrixxlag = mlag(dy,nlag);% constant termiota = ones(nobs,1);% truncate variables to feed lags and freq diff's and end at begf-1iota = trimr(iota,nlag+freq,nobs-begf+1);dys = trimr(dy,nlag+freq,nobs-begf+1);xlag = trimr(xlag,nlag+freq,nobs-begf+1);% add constant to x-matrix xmat = [xlag iota];% end of growth-rates transform casecase 3 % seasonal differences transform% transform datady = y - lag(y,freq);% generate lagged rhs matrixxlag = mlag(dy,nlag);% constant termiota = ones(nobs,1);% truncate variables to feed lags and freq diff's and end at begf-1iota = trimr(iota,nlag+freq,nobs-begf+1);dys = trimr(dy,nlag+freq,nobs-begf+1);xlag = trimr(xlag,nlag+freq,nobs-begf+1);% add constant to x-matrix xmat = [xlag iota];otherwise  % case of no transformation% generate lagged rhs matrixxlag = mlag(y,nlag);% constant termiota = ones(nobs,1);% truncate to feed lags and to end at begf-1 for estimationdys  = trimr(y,nlag,nobs-begf+1);dy   = y;xlag = trimr(xlag,nlag,nobs-begf+1);iota = trimr(iota,nlag,nobs-begf+1);% add constant to x-matrix xmat = [xlag iota];end; % end of data transformation cases% dimension some result matricesneqs = 1;bmat = zeros(k,neqs);yfor = zeros(nfor,neqs);% ----- get bhat estimates% save time by computing xpx only oncexpx = xmat'*xmat;% pull out each y-vector and run regressionsfor j=1:neqs; yvec = dys(:,j); bhat = (xpx)\(xmat'*yvec);  % save bhat bmat(:,j) = bhat;end; % end of loop over equations% given bhat estimates, generate future forecasts % These may be levels, 1st-differences, growth rates or seas diff's% we worry transforming back to levels later% 1-step-ahead forecast xtrunc = [dy(nmin-nlag:nmin,:)          zeros(1,neqs)];xfor = mlag(xtrunc,nlag);[xend junk] = size(xfor);xobs = xfor(xend,:);xvec = [xobs 1];% loop over equationsfor i=1:neqs;bhat = bmat(:,i);yfor(1,i) = xvec*bhat;end;xnew = zeros(nlag+1,neqs);% 2 through nlag-step-ahead forecastsfor step=2:nlag;if step <= nforxnew(1:nlag-step+1,:) = dy(nmin-nlag+step:nmin,:);xnew(nlag-step+2:nlag,:) = yfor(1:step-1,:);xnew(nlag+1,:) = zeros(1,neqs);xfor = mlag(xnew,nlag);[xend junk] = size(xfor);xobs = xfor(xend,:);xvec = [xobs 1];% loop over equationsfor i=1:neqs;bhat = bmat(:,i);yfor(step,i) = xvec*bhat;end;end;end;% nlag through nfore-step-ahead forecastsfor step=nlag:nfor-1;if step <= nforcnt = step-(nlag-1); for i=1:nlag;  xnew(i,:) = yfor(cnt,:);  cnt = cnt+1; end; xfor = mlag(xnew,nlag);[xend junk] = size(xfor);xobs = xfor(xend,:);xvec = [xobs 1];% loop over equationsfor i=1:neqs;bhat = bmat(:,i);yfor(step+1,i) = xvec*bhat;end;end;end;% we now worry about transforming the forecasts back% to levelsswitch tformcase 1 % 1st differences forecasts% convert 1st difference forecasts to levelsylevf = zeros(nfor,neqs);% 1-step-ahead forecastylevf(1,:) = yfor(1,:) + y(begf-1,:); % add change to actual from time t;% 2-nfor-step-ahead forecastsfor i=2:nfor % ylevf(i,:) = yfor(i,:) + ylevf(i-1,:);end;% end of 1st differences casecase 2 % growth rates forecasts% convert growth rate forecasts to levelsylevf = zeros(nfor,neqs);yfor = yfor/100.0; % growth-rates are mutiliplied by 100for step=1:nfor;if freq < step, % here we can use past level forecasts   ylevf(step,:) = (1 + yfor(step,:)).*ylevf(step-freq,:);else % case of freq > step, use past actual levels   ylevf(step,:) = (1 + yfor(step,:)).*y(begf+step-freq-1,:);end; % end of if freq <= stepend; % end of for step loopcase 3 % seasonal difference forecasts% convert seasonal difference forecasts to levelsfor step=1:nfor;if freq < step, % here we use past level forecasts   ylevf(step,:) = yfor(step,:) + ylevf(step-freq,:);else % case of freq > step, use past actual levels   ylevf(step,:) = yfor(step,:) + y(begf+step-freq-1,:);end; % end of if freq <= stepend; % end of for step loopotherwise % no transformation, so we have level forecasts alreadyylevf = yfor;end;  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -